Binary Search Tree Verification Help

P

PeterAPIIT

Hello to all expect software engineer, i have create a binary search
tree.

I know to know what is your opinion about my program.

Code:
template <typename T>
class treeNode
{
public:

	T key;
	T value;

	treeNode<T> *leftNode;
	treeNode<T> *rightNode;


	treeNode() : key(T()), value(T()),
		leftNode(0),
		rightNode(0)
	{}

	treeNode(const T& aKey) : key(aKey),
		value(T()),
		leftNode(0),
		rightNode(0)
	{}

	treeNode(const T& aKey, const T& aValue)
		: key(aKey),
		value(aValue),
		leftNode(0),
		rightNode(0)
	{}

	~treeNode()
	{

	}
};



template <typename T>
class BSearchTree
{
private:

	treeNode<T>* root;

public:
	BSearchTree<T>()
		: root(0)
	{
	}

	~BSearchTree()
	{
		if (root != 0)
		{
			destroy();
		}
	}

	void Insert(const T& key, const T& value)
	{
		if (root != 0)
		{
			treeNode<T>* newNode = new treeNode<T>(key, value);
			treeNode<T>* iteratorNode = root;

			while(iteratorNode->leftNode != 0
				|| iteratorNode->rightNode != 0)
			{
				if (key < iteratorNode->key)
				{
					iteratorNode = iteratorNode->leftNode;
				}
				else
				{
					iteratorNode = iteratorNode->rightNode;
				}
			}

			if (key < iteratorNode->key)
			{
				iteratorNode->leftNode = newNode;
			}
			else
			{
				iteratorNode->rightNode = newNode;
			}
		}
		else
		{
			treeNode<T>* newNode = new treeNode<T>(key, value);
			treeNode<T>* iteratorNode = root;

			root = newNode;
		}

	}

	void Remove(T& key)
	{
	}

	void destroy()
	{
		treeNode<T>* iteratorNode = root;


	}

	void RecursivePreOrder(treeNode<T>* aTreeNode)const
	{
		if (aTreeNode != 0)
		{
			cout << "Key is " << aTreeNode->key << "\n";
			cout << "Value is " << aTreeNode->value << "\n";

			if (aTreeNode->leftNode != 0)
			{
				RecursivePreOrder(aTreeNode->leftNode);
			}

			if (aTreeNode->rightNode != 0)
			{
				RecursivePreOrder(aTreeNode->rightNode);
			}
		}

	}

	// Root->Left->Right
	void PreOrderDisplay()const
	{
		if (root != 0)
		{
			cout << "Pre Order Display \n";
			RecursivePreOrder(this->root);
		}
	}

	void RecursiveInOrder(treeNode<T>* aTreeNode)const
	{
		if (aTreeNode != 0)
		{
			if (aTreeNode->leftNode != 0)
			{
				RecursiveInOrder(aTreeNode->leftNode);
			}

			cout << "Key is " << aTreeNode->key << "\n";
			cout << "Value is " << aTreeNode->value << "\n";

			if (aTreeNode->rightNode != 0)
			{
				RecursiveInOrder(aTreeNode->rightNode);
			}
		}

	}

	// Left->Root->Right
	void InOrderDisplay()const
	{
		if (root != 0)
		{
			cout << "In Order Display \n";
			RecursiveInOrder(this->root);
		}
	}

	void RecursivePostOrder(treeNode<T>* aTreeNode)const
	{
		if (aTreeNode != 0)
		{
			if (aTreeNode->leftNode != 0)
			{
				RecursivePostOrder(aTreeNode->leftNode);
			}

			if (aTreeNode->rightNode != 0)
			{
				RecursivePostOrder(aTreeNode->rightNode);
			}

			cout << "Key is " << aTreeNode->key << "\n";
			cout << "Value is " << aTreeNode->value << "\n";

		}

	}

	// Left->Right->Root
	void PostOrderDisplay()const
	{
		if (root != 0)
		{
			cout << "Post Order Display \n";
			RecursivePostOrder(this->root);
		}
	}





};

I wonder does something wrong with my code.

Thanks for your help.
 
P

PeterAPIIT

Another question how to improve access level of treeNode data member
and access by BSearchTree ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top