What is a Binary Search Tree
A graph without any loop is called a tree which has exactly n-1 connectors/edges for n nodes and always have a unique path between any two nodes. If in this type of arrangement each node has two connection to child nodes and one to the parent node then it is called binary tree and if in this type of binary tree each left subnode is less then then parent node and rigt subnode is greater then parent then it is called binary search tree.
What is Non-Recursive Implementation
Non-recursive implementation is the implementation where function doesn't call itself. The catch here is that you need a stack data-structure for traversal whereas in the case of recursion it is done by the compiler itself.
C++ implementation of Binary Search Tree
struct node
{
int data;
node *left;
node *right;
};
typedef struct node *nodeptr;
class stack
{
private:
struct snode
{
nodeptr data;
snode *next;
};
snode *top;
public:
stack()
{
top=NULL;
}
void push(nodeptr p)
{
snode *temp;
temp = new snode;
temp->data = p;
temp->next = top;
top=temp;
}
void pop()
{
if (top != NULL)
{
nodeptr t;
snode *temp;
temp = top;
top=temp->next;
delete temp;
}
}
nodeptr topele()
{
if (top !=NULL)
return top->data;
else
return NULL;
}
int isempty()
{
return ((top == NULL) ? 1 : 0);
}
};
class bstree
{
public:
void insert(int,nodeptr &);
void del(int,nodeptr &);
int deletemin(nodeptr &);
void find(int,nodeptr &);
nodeptr findmin(nodeptr);
nodeptr findmax(nodeptr);
void copy(nodeptr &,nodeptr &);
void makeempty(nodeptr &);
nodeptr nodecopy(nodeptr &);
void preorder(nodeptr);
void inorder(nodeptr);
void postorder(nodeptr);
void preordernr(nodeptr);
void inordernr(nodeptr);
void postordernr(nodeptr);
void leftchild(int,nodeptr &);
void rightchild(int,nodeptr &);
};
void bstree::insert(int x,nodeptr &p)
{
if (p==NULL)
{
p = new node;
p->data=x;
p->left=NULL;
p->right=NULL;
}
else
{
if (x < p->data)
insert(x,p->left);
else if (x>p->data)
insert(x,p->right);
else
cout<<"Element already Exits !";
}
}
void bstree:: del(int x,nodeptr &p)
{
nodeptr d;
if (p==NULL)
cout<<"Element not found ";
else if (x < p->data)
del(x,p->left);
else if (x > p->data)
del(x,p->right);
else if ((p->left == NULL) && (p->right ==NULL))
{
d=p;
free(d);
p=NULL;
}
else if (p->left == NULL)
{
d=p;
free(d);
p=p->right;
}
else if (p->right ==NULL)
{
d=p;
p=p->left;
free(d);
}
else
p->data=deletemin(p->right);
}
int bstree::deletemin(nodeptr &p)
{
int c;
if (p->left == NULL)
{
c=p->data;
p=p->right;
return c;
}
else
c=deletemin(p->left);
return c;
}
void bstree::copy(nodeptr &p,nodeptr &p1)
{
makeempty(p1);
p1=nodecopy(p);
}
void bstree::makeempty(nodeptr &p)
{
nodeptr d;
if (p!=NULL)
{
makeempty(p->left);
makeempty(p->right);
d=p;
free(d);
p=NULL;
}
}
nodeptr bstree::nodecopy(nodeptr &p)
{
nodeptr temp;
if (p == NULL)
return p;
else
{
temp = new node;
temp->data=p->data;
temp->left = nodecopy(p->left);
temp->right = nodecopy(p->right);
return temp;
}
}
nodeptr bstree::findmin(nodeptr p)
{
if (p==NULL)
{
cout<<"Tree is empty !";
return p;
}
else
{
while (p->left !=NULL)
p=p->left;
return p;
}
}
nodeptr bstree::findmax(nodeptr p)
{
if (p==NULL)
{
cout<<"Tree is empty !";
return p;
}
else
{
while (p->right !=NULL)
p=p->right;
return p;
}
}
void bstree::find(int x,nodeptr &p)
{
if (p==NULL)
cout<<"Element not found !";
else
{
if (x <p->data)
find(x,p->left);
else if ( x> p->data)
find(x,p->right);
else
cout<<"Element Found !";
}
}
void bstree::preorder(nodeptr p)
{
if (p!=NULL)
{
cout<<p->data<<"-->";
preorder(p->left);
preorder(p->right);
}
}
void bstree::inorder(nodeptr p)
{
if (p!=NULL)
{
inorder(p->left);
cout<<p->data<<"-->";
inorder(p->right);
}
}
void bstree::postorder(nodeptr p)
{
if (p!=NULL)
{
postorder(p->left);
postorder(p->right);
cout<<p->data<<"-->";
}
}
void bstree::preordernr(nodeptr p)
{
stack s;
while (1)
{
if (p != NULL)
{
cout<<p->data<<"-->";
s.push(p);
p=p->left;
}
else
if (s.isempty())
{
cout<<"Stack is empty";
return;
}
else
{
nodeptr t;
t=s.topele();
p=t->right;
s.pop();
}
}
}
void bstree::inordernr(nodeptr p)
{
stack s;
while (1)
{
if (p != NULL)
{
s.push(p);
p=p->left;
}
else
{
if (s.isempty())
{
cout<<"Stack is empty";
return;
}
else
{
p=s.topele();
cout<<p->data<<"-->";
}
s.pop();
p=p->right;
}
}
}
void bstree::postordernr(nodeptr p)
{
stack s;
while (1)
{
if (p != NULL)
{
s.push(p);
p=p->left;
}
else
{
if (s.isempty())
{
cout<<"Stack is empty";
return;
}
else
if (s.topele()->right == NULL)
{
p=s.topele();
s.pop();
cout<<p->data<<"-->";
if (p==s.topele()->right)
{
cout<<s.topele()->data<<"-->";
s.pop();
}
}
if (!s.isempty())
p=s.topele()->right;
else
p=NULL;
}
}
}
void bstree::leftchild(int q,nodeptr &p)
{
if (p==NULL)
cout<<"The node does not exists ";
else
if (q < p->data )
leftchild(q,p->left);
else
if (q > p->data)
leftchild(q,p->right);
else
if (q == p->data)
{
if (p->left != NULL)
cout<<"Left child of "<<q<<"is "<<p->left->data;
else
cout<<"No Left child !";
}
}
void bstree::rightchild(int q,nodeptr &p)
{
if (p==NULL)
cout<<"The node does not exists ";
else
if (q < p->data )
rightchild(q,p->left);
else
if (q > p->data)
rightchild(q,p->right);
else
if (q == p->data)
{
if (p->right != NULL)
cout<<"Right child of "<<q<<"is "<<p->right->data;
else
cout<<"No Right Child !";
}
}