0% found this document useful (0 votes)
2 views

assignment 5

The document contains a C++ implementation of a Binary Search Tree (BST) with various functionalities including creation, insertion, traversal (inorder, preorder, postorder), finding minimum and maximum values, searching for a key, mirroring the tree, and calculating the height. The main function provides a menu-driven interface for users to interact with the BST. It demonstrates how to create a BST, add nodes, and perform different operations on it.

Uploaded by

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

assignment 5

The document contains a C++ implementation of a Binary Search Tree (BST) with various functionalities including creation, insertion, traversal (inorder, preorder, postorder), finding minimum and maximum values, searching for a key, mirroring the tree, and calculating the height. The main function provides a menu-driven interface for users to interact with the BST. It demonstrates how to create a BST, add nodes, and perform different operations on it.

Uploaded by

yojitwaghmare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

INPUT:

#include<iostream> using namespace std; class node { public: int data;

node *L,*R;

};

class BST

{ public:

void create(node *root); void inorder(node *root); void preorder(node *root); void postorder(node *root);

void min(node *root); void max(node *root); void insert(node *root); node *search(node *root,int key); void

mirror(node *root); int height(node *root);

};

void BST::create(node *root)

node *ne,*temp; char ans; do {

ne=new node; cout<<"Enter Data for

NewNode:"<<endl; cin>>ne->data; ne>L=NULL; ne->R=NULL; temp=root; while(1)

if(ne->data<temp->data) //le sub tree

if(temp->L==NULL)

temp->L=ne; break; } else

temp=temp->L;

} } else // Right subtree

if(temp->R==NULL)

temp->R=ne; break;

} else

temp=temp->R;

}
cout<<"Do You Want To Add More:"<<endl; cin>>ans;

}while(ans=='y'||ans=='Y');

void BST::insert(node*root)

node*ne,*temp; ne=new node; cout<<"enterdatafornewnodetoinsert:"<<endl; cin>>ne->data; ne->L=NULL;

ne>R=NULL; temp=root;

while(1)

if(ne->data<temp->data)

if(temp->L==NULL)

temp->L=ne;

break; } else

temp=temp->L;

} } else

if(temp->R==NULL)

{ temp->R=ne;

break; } else

temp=temp->R;

void BST::inorder(node*root)

if(root!=NULL)

inorder(root->L); cout<<root>data<<endl; inorder(root->R);


}

void BST::preorder(node*root)

if(root!=NULL)

cout<<root->data<<endl; preorder(root->L); preorder(root-

>R);

void BST::postorder(node*root)

if(root!=NULL)

{ postorder(root->L); postorder(root>R); cout<<root->data<<endl;

void BST::min(node*root)

node*temp; temp=root; while(temp>L!=NULL)

temp=temp->L;

cout<<"Minimum="<<temp->data<<endl;

void BST::max(node*root)

node*temp; temp=root; while(temp>R!=NULL)

temp=temp->R;

cout<<"Maximum="<<temp->data<<endl;

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

if(root==NULL||root->data==key)

return root;
}

if(root->data>key)

return search(root->L,key);

} else { return search(root->R,key);

void BST::mirror(node*root)

node*temp;

if(root!=NULL)

temp=root->L; root>L=root->R; root->R=temp; mirror(root->L);

mirror(root->R);

int BST::height(node*root)

{ int i,j,max=0; i=1,j=1; if(root!=NULL)

{ i=i+height(root->L); j=j+height(root-

>R);

if(i>j) max=i; else max=j;

} return(max);
}

int main() {

BST ob; node

*root,*t; int ch,key,i; while(1)

cout<<"1create"<<endl; cout<<"2inorder"<<endl; cout<<"3preorder"<<endl; cout<<"4postorder"<<endl;

cout<<"5insert"<<endl; cout<<"6min"<<endl; cout<<"7max"<<endl; cout<<"8search"<<endl;

cout<<"9mirrorimage"<<endl; cout<<"10search"<<endl; cout<<"enteryourchoice =

"<<endl; cin>>ch; switch(ch)


{

case 1:root=new node; cout<<"Enter

Data for Root Node:"<<endl; cin>>root-

>data; root->L=NULL; root>R=NULL; ob.create(root); break; case 2:ob.inorder(root);

break;

case 3:ob.preorder(root);

break;

case 4:ob.postorder(root); break; case 5:ob.insert(root); break; case 6:ob.min(root);

break; case 7:ob.max(root); break; case 8:

cout<<"\nEnter the Data to be Search in BST=:"; cin>>key; t=ob.search(root,key); if(t==NULL)

cout<<"\nKeyis Not Present in BST"<<endl;

else

cout<<"\nkeyisPresen nBST"<<endl;

} break; case 9:ob.mirror(root); ob.inorder(root); break; case 10:i=ob.height(root);

cout<<"height="<<i<<end

l; break;

} } return 0;

} output: enteryourchoice =

Enter Data for Root Node:

10

Enter Data for NewNode:

Do You Want To Add More:

Enter Data for NewNode:

15 Do You Want To Add More: y

Enter Data for NewNode:

Do You Want To Add More:

Enter Data for NewNode:

9
Do You Want To Add More:

Enter Data for NewNode:

12 Do You Want To Add More: y

Enter Data for NewNode:

18 Do You Want To Add More: n

1create

2inorder

3preorder

4postorder

5insert

6min

7max

8search 9mirrorimage 10search enteryourchoice =

10 12

15 18

1create

2inorder

3preorder

4postorder

5insert

6min

7max

8search 9mirrorimage 10search enteryourchoice =

You might also like