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.
I wonder does something wrong with my code.
Thanks for your help.
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.