Computer Science 261

Exercise set #7

Due:  Tuesday, Nov. 8

 

Consider the following code:

#include <iostream.h>
#include <string.h>

const int NameLen = 21;

class Tree{
public:
	Tree();

	void PrintTree();
	void PrintName(char theName[]);
		//  The search function.  In this case, it simply
		//	prints a "found" / "not found" message.
	void DiagnosticInOrder();
	void InsertName(char theName[]);
	void DeleteNode(char theName[]);


private:

	struct Node{
		char Value [NameLen];
		Node *left, *right;
	};

	Node *root;

	// Helper functions

void DiagInOrderTraversal(Node* theTree, int level)
{
	if (theTree==NULL) 
	{
		for (int k=1; k <= level; ++k) cout << "\t";
		cout << "NULL\n";
		return;
	}
	DiagInOrderTraversal(theTree->left, level+1);
	for (int k=1; k <= level; ++k) cout << "\t";
	cout << theTree ->Value << endl;
	DiagInOrderTraversal(theTree ->right, level+1);
	return;
}


};

int main()
{
	char response;

	Tree MyTree;
	MyTree.InsertName("HOLMES");
	MyTree.InsertName("DEE");
	MyTree.InsertName("FANSLER");
	MyTree.InsertName("MAIGRET");
	MyTree.InsertName("LESTRADE");
	MyTree.InsertName("WATSON");
	MyTree.InsertName("DREW");
	MyTree.InsertName("KELLING");
	MyTree.InsertName("LI KAO");
	MyTree.InsertName("WOLFE");
	MyTree.InsertName("POIROT");

	cout << endl << "List of names as stored in the tree:\n" << endl;

	MyTree.DiagnosticInOrder();

	cout << "\nIn Order: \n\n";
	MyTree.PrintTree();

//	cout << "Continue? ";
//	cin >> response;

//  You may want to put these cin/cout statements back in to pause
//  before moving on to the next steps (so that you can check your
//  work), but please be sure to comment them out again before I
//  pick up your program

	cout << "\nSearching for Kelling ";
	MyTree.PrintName("KELLING");
	cout << "\nSearching for Maigret";
	MyTree.PrintName("MAIGRET");
	cout << endl;

	MyTree.DeleteNode("FANSLER");
	MyTree.DiagnosticInOrder();

//	cout << "Continue? ";
//	cin >> response;

	MyTree.DeleteNode("DREW");
	MyTree.DiagnosticInOrder();

//	cout << "Continue? ";
//	cin >> response;

	MyTree.DeleteNode("MAIGRET");
	cout << "\nMaigret should no longer be in the tree\n";
	cout << "Searching for Maigret: ";
	MyTree.PrintName("MAIGRET");

	cout << endl << "List of names as stored in the tree:\n" << endl;

	MyTree.DiagnosticInOrder();

//	cout << "Continue? ";
//	cin >> response;

	cout << "\nIn Order: \n\n";
	MyTree.PrintTree();


	return 0;
}
void Tree::DiagnosticInOrder()
{
	DiagInOrderTraversal(root, 0);
}

 

Include all this in a program (called tree.cpp), adding missing routines..