Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

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)
4 views

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
You are on page 1/ 17

#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;

q.push(root);

while(q.empty()==false)

int nodecount=q.size();

while(nodecount>0)

BST *curr=q.front();

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

q.pop();

if(curr->left!=NULL)

q.push(curr->left);

if(curr->right!=NULL)

q.push(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<<"\n1.Insert Element ";

cout<<"\n2.Search Element ";

cout<<"\n3 Delete Element";

cout<<"\n4.Breadth First Traversals"; // in-order, pre-order,post-order

cout<<"\n5.Height First/Level Order Traversal"; //print nodes level wise

cout<<"\n6.Display BST"; //tree representation


cout<<"\n7.Parent and Child Nodes"; //get parent and child nodes

cout<<"\n8.Leaf Nodes"; //print all leaf nodes

cout<<"\n9.Clear Screen";

cout<<"\n10.Quit";

cout<<"\nEnter your choice : ";

cin>>choice;

switch(choice)

case 1:

root=t.insert(root);

break;

case 2:

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

cin>>key;

valid=t.search(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:
t.deleteNode(root,key);

break;

case 4:

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

t.inorder(root);

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

t.preorder(root);

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

t.postorder(root);

break;

case 5:

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

t.printLevelOrder(root);

break;

case 6:

cout<<"\nBST:\n";

t.display(root,1);

cout<<"\n\n";

break;

case 7:

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

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;

You might also like