Assignment 5_BinarySearchTree
Assignment 5_BinarySearchTree
#include <queue>
class BST
int data;
BST *left,*right;
public:
BST* insert(BST*);
void inorder(BST*);
void preorder(BST*);
void postorder(BST*);
void printLevelOrder(BST*);
void display(BST*,int);
BST* findParentChild(BST*);
void printLeafNodes(BST*);
};
class stack
public:
BST *t;
stack *link;
};
newnode->t=temp;
newnode->link=NULL;
if(top==NULL)
top=newnode;
else
newnode->link=top;
top=newnode;
return top;
{
stack *temp=top;
if(temp==NULL)
cout <<"empty";
else
top=top->link;
delete temp;
return top;
newnode->data=key;
newnode->left=NULL;
newnode->right=NULL;
return newnode;
BST *curr=root;
while(curr!=NULL && curr->data!=key)
if(key<curr->data)
curr=curr->left;
else
curr=curr->right;
if(curr==NULL)
return 0;
else
return 1;
int key;
cin>>key;
BST *curr,*parent;
BST *newnode=create(key);
if(root==NULL)
root=newnode;
else
curr=root;
while(curr!=NULL && curr->data!=key)
parent=curr;
if(key<curr->data)
curr=curr->left;
else
curr=curr->right;
if(curr==NULL)
if(key<parent->data)
parent->left=newnode;
else
parent->right=newnode;
else
return root;
{
if(root==NULL)
return;
else
inorder(root->left);
cout<<root->data<<" ";
inorder(root->right);
if(root==NULL)
return;
else
cout<<root->data<<" ";
preorder(root->left);
preorder(root->right);
}
void BST :: postorder(BST *root)
if(root==NULL)
return;
else
postorder(root->left);
postorder(root->right);
cout<<root->data<<" ";
BST *curr=root;
BST *parent=curr;
cin>>key;
while(curr->data!=key)
parent=curr;
if(key<curr->data)
curr=curr->left;
else
curr=curr->right;
// case 1:
if(curr!=root)
if(parent->left==curr)
parent->left=NULL;
else
parent->right=NULL;
else
root=NULL;
// case 2:
BST *gp=curr;
curr=curr->left;
parent=curr;
while(curr->right!=NULL)
{
parent=curr;
curr=curr->right;
gp->data=curr->data;
if(curr==parent)
curr=curr->left;
gp->left=curr;
delete parent;
else
parent->right=curr->left;
// case 3:
else if (curr->left!=NULL)
if(parent->right=curr)
parent->right=curr->left;
else
parent->left=curr->left;
else
if(parent->right=curr)
parent->right=curr->right;
else
parent->left=curr->right;
return root;
BST *curr;
if(root!=NULL)
display(root->right,level+1);
cout<<"\n";
if(root==curr)
cout<<"Root->:";
else
for(int i=0;i<level;i++)
cout<<" ";
cout<<root->data;
display(root->left,level+1);
}
void BST :: printLevelOrder(BST* root)
if(root==NULL)
return;
queue<BST *> q;
q.push(root);
while(q.empty()==false)
int nodecount=q.size();
while(nodecount>0)
BST *curr=q.front();
q.pop();
if(curr->left!=NULL)
q.push(curr->left);
if(curr->right!=NULL)
q.push(curr->right);
nodecount--;
cout<< "\n";
BST *curr=root;
stack *top=NULL ;
while(curr!=NULL)
if(curr->left!=NULL || curr->right!=NULL)
if(curr->right!=NULL)
top=top->push(top,curr->right);
if(curr->left!=NULL)
if(curr->right!=NULL)
else
curr=curr->left;
}
else
curr->right=NULL;
else
if(top!=NULL)
curr=top->t;
top=top->pop(top);
else
curr=NULL;
if(!root)
return;
cout<<root->data<<" ";
if(root->left)
printLeafNodes(root->left);
if(root->right)
printLeafNodes(root->right);
int main()
BST t;
BST *root=NULL;
int choice,key,valid;
while(1)
cout<<"\n\n-----------------";
cout<<"\nOperations on BST";
cout<<"\n-----------------";
cout<<"\n9.Clear Screen";
cout<<"\n10.Quit";
cin>>choice;
switch(choice)
case 1:
root=t.insert(root);
break;
case 2:
cin>>key;
valid=t.search(root,key);
if(valid!=0)
else
break;
case 3:
t.deleteNode(root,key);
break;
case 4:
t.inorder(root);
t.preorder(root);
t.postorder(root);
break;
case 5:
t.printLevelOrder(root);
break;
case 6:
cout<<"\nBST:\n";
t.display(root,1);
cout<<"\n\n";
break;
case 7:
t.findParentChild(root);
break;
case 8:
cout<<"\nLeaf nodes of the BST (from left to right) are --> ";
t.printLeafNodes(root);
break;
case 9:
system("cls");
break;
case 10:
exit(1);
default:
cout<<"Wrong choice"<<endl;
return 0;