rendered paste body#include "BinarySearchTree.h"
#include <cstdlib>
#include <iostream>
using namespace std;
BinarySearchTree::BinarySearchTree()
{
left = NULL;
right = NULL;
key = -1;
depth = -1;
pos = -1;
}
BinarySearchTree::BinarySearchTree(int k)
{
left = NULL;
right = NULL;
key = k;
depth = 0;
pos = 0;
}
void BinarySearchTree::display( tree t)
{
if(t!=NULL)
{
display(t->left);
cout << t->getKey() << "\t" << t->getDepth() << endl;
display(t->right);
}
}
void BinarySearchTree::insert(int k, tree &t, int depth)
{
if( t == NULL )
{
tree t = new BinarySearchTree(k);
t->setDepth(depth);
t->left = NULL;
t->right = NULL;
cout << "Creating new key " << t->key << " " << t->depth << endl;
}
else if( k < t->getKey() )
{
insert(k, t->left, t->getDepth()+1);
}
else if( k > t->getKey() )
{
insert(k, t->right, t->getDepth()+1);
}
else
{
cout << "Duplicate key" << endl;
}
}
int BinarySearchTree::getKey()
{
return key;
}
int BinarySearchTree::getPos()
{
return pos;
}
int BinarySearchTree::getDepth()
{
return depth;
}
void BinarySearchTree::setPos(int p)
{
pos = p;
}
void BinarySearchTree::setDepth(int d)
{
depth = d;
}