0% found this document useful (0 votes)
17 views17 pages

Assignment 5 - BinarySearchTree

The document contains a C++ implementation of a Binary Search Tree (BST) with various functionalities including insertion, deletion, searching, and different traversal methods (in-order, pre-order, post-order, and level order). It also includes a stack implementation for managing nodes and features for displaying parent-child relationships and leaf nodes. The main function provides a menu-driven interface for users to interact with the BST operations.

Uploaded by

jobidad431
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views17 pages

Assignment 5 - BinarySearchTree

The document contains a C++ implementation of a Binary Search Tree (BST) with various functionalities including insertion, deletion, searching, and different traversal methods (in-order, pre-order, post-order, and level order). It also includes a stack implementation for managing nodes and features for displaying parent-child relationships and leaf nodes. The main function provides a menu-driven interface for users to interact with the BST operations.

Uploaded by

jobidad431
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#include <iostream>

#include <queue>

using namespace std;

class BST

int data;

BST *left,*right;

public:

BST* create(int key);

BST* insert(BST*);

int search(BST*, int);

BST* deleteNode(BST *,int );

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;

stack* push(stack *,BST *);

stack* pop(stack *);

};

stack* stack::push(stack *top,BST *temp)

stack *newnode=new stack;

newnode->t=temp;

newnode->link=NULL;

if(top==NULL)

top=newnode;

else

newnode->link=top;

top=newnode;

return top;

stack* stack::pop(stack *top)

{
stack *temp=top;

if(temp==NULL)

cout <<"empty";

else

top=top->link;

delete temp;

return top;

BST* BST :: create(int key)

BST *newnode=new BST;

newnode->data=key;

newnode->left=NULL;

newnode->right=NULL;

return newnode;

int BST :: search(BST *root,int key)

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;

BST* BST :: insert(BST *root)

int key;

cout<< "\nEnter the data to insert : ";

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

cout<<"\nDuplicate entry .Reinsert data .";

return root;

void BST :: inorder(BST *root)

{
if(root==NULL)

return;

else

inorder(root->left);

cout<<root->data<<" ";

inorder(root->right);

void BST :: preorder(BST *root)

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* BST :: deleteNode(BST *root,int key)

BST *curr=root;

BST *parent=curr;

cout<<"\nEnter the data you want to delete : ";

cin>>key;

while(curr->data!=key)

parent=curr;

if(key<curr->data)
curr=curr->left;

else

curr=curr->right;

// case 1:

if(curr->left==NULL && curr->right==NULL)

if(curr!=root)

if(parent->left==curr)

parent->left=NULL;

else

parent->right=NULL;

else

root=NULL;

// case 2:

else if(curr->left!=NULL && curr->right!=NULL)

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;

void BST :: display(BST* root, int level)

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;

[Link](root);

while([Link]()==false)

int nodecount=[Link]();

while(nodecount>0)

BST *curr=[Link]();

cout<<curr->data<< " ";

[Link]();

if(curr->left!=NULL)

[Link](curr->left);

if(curr->right!=NULL)

[Link](curr->right);

nodecount--;

cout<< "\n";

BST* BST :: findParentChild(BST *root)


{

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)

cout<< "\nParent : "<<curr->data;

cout<< " , Left child : "<<curr->left->data;

if(curr->right!=NULL)

cout<< " , Right child : "<<curr->right->data;

else

cout<< " , Right child : NULL";

curr=curr->left;

}
else

cout<< "\nParent : "<<curr->data;

cout<< " , Left child : NULL";

cout<< " , Right child : "<<curr->right->data;

curr->right=NULL;

else

if(top!=NULL)

curr=top->t;

top=top->pop(top);

else

curr=NULL;

void BST :: printLeafNodes(BST *root)

if(!root)
return;

if(!root->left && !root->right)

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<<"\[Link] Element ";

cout<<"\[Link] Element ";

cout<<"\n3 Delete Element";

cout<<"\[Link] First Traversals"; // in-order, pre-order,post-order

cout<<"\[Link] First/Level Order Traversal"; //print nodes level wise

cout<<"\[Link] BST"; //tree representation


cout<<"\[Link] and Child Nodes"; //get parent and child nodes

cout<<"\[Link] Nodes"; //print all leaf nodes

cout<<"\[Link] Screen";

cout<<"\[Link]";

cout<<"\nEnter your choice : ";

cin>>choice;

switch(choice)

case 1:

root=[Link](root);

break;

case 2:

cout<<"Enter the element you want to search : ";

cin>>key;

valid=[Link](root,key);

if(valid!=0)

cout<<"\nElement with key value '"<<key<<"' exists";

else

cout<<"\nElement with key value '"<<key<<"' doesn't exist.";

break;

case 3:
[Link](root,key);

break;

case 4:

cout<<"\nIn-order Traversal --> ";

[Link](root);

cout<<"\nPre-order Traversal --> ";

[Link](root);

cout<<"\nPost-order Traversal --> ";

[Link](root);

break;

case 5:

cout<<"\nLevel Order Traversal : \n";

[Link](root);

break;

case 6:

cout<<"\nBST:\n";

[Link](root,1);

cout<<"\n\n";

break;

case 7:

cout<<"\nParent and Child nodes relations : \n";

[Link](root);

break;

case 8:

cout<<"\nLeaf nodes of the BST (from left to right) are --> ";
[Link](root);

break;

case 9:

system("cls");

break;

case 10:

exit(1);

default:

cout<<"Wrong choice"<<endl;

return 0;

You might also like