Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
14 views6 pages

Inorder Preorder and BST No 4

This document defines classes and functions to implement a binary search tree (BST). It includes functions to insert nodes, search for nodes, traverse the tree using inorder, postorder and preorder traversal, and find the depth of the tree. The main function provides a menu for the user to enter elements, display traversals, find depth, insert new nodes, and search for nodes in the BST.

Uploaded by

KUNAL NALE
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)
14 views6 pages

Inorder Preorder and BST No 4

This document defines classes and functions to implement a binary search tree (BST). It includes functions to insert nodes, search for nodes, traverse the tree using inorder, postorder and preorder traversal, and find the depth of the tree. The main function provides a menu for the user to enter elements, display traversals, find depth, insert new nodes, and search for nodes in the BST.

Uploaded by

KUNAL NALE
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/ 6

#include<iostream>

#include<math.h>

using namespace std;

class bstnode

public:

int data;

bstnode*left=NULL;

bstnode*right=NULL;

bstnode*getnewnode(int in_data);

}*root;

class btree

int n;

int x;

int flag=0;

public:

bstnode*root;

btree()

root=NULL;

bstnode*getnewnode(int in_data)

bstnode*ptr=new bstnode();

ptr->data=in_data;

ptr->left=NULL;

ptr->right=NULL;

return ptr;
}

bstnode*insert(bstnode*temp,int in_data)

if(temp==NULL)

temp=getnewnode(in_data);

else if(temp->data>in_data)

temp->left=insert(temp->left,in_data);

else

temp->right=insert(temp->right,in_data);

return temp;

void input()

int n,x;

cout<<"ENTER NUMBER OF ELEMENTS IN THE BST:";

cin>>n;

for(int i=0;i<n;i++)

cout<<"NUMBER=";

cin>>x;

root=insert(root,x);
}

void search(bstnode*temp,int in_data)

if(temp!=NULL)

if(temp->data==in_data)

cout<<"\nelement found\n";

else if(in_data<temp->data)

search(temp->left,in_data);

else if(in_data>temp->data)

search(temp->right,in_data);

else

cout<<"\nELEMENT NOT FOUND\n";

void inorder(bstnode*temp)

{
if(temp!=NULL)

inorder(temp->left);

cout<<temp->data<<"\t";

inorder(temp->right);

void postorder(bstnode*temp)

if(temp!=NULL)

postorder(temp->left);

postorder(temp->right);

cout<<temp->data<<"\t";

void preorder(bstnode*temp)

if(temp!=NULL)

cout<<temp->data<<"\t";

preorder(temp->left);

preorder(temp->right);

int depth(bstnode*temp)

if(temp==NULL)
return 0;

return(max((depth(temp->left)),(depth(temp->right)))+1);

void display()

cout<<endl<<"_INORDER TRAVERSAL_"<<endl;

inorder(root);

cout<<endl;

cout<<endl<<"_POSTORDER TRAVERSAL_"<<endl;

postorder(root);

cout<<endl;

cout<<endl<<"_PREORDER TRAVERSAL_"<<endl;

preorder(root);

cout<<endl;

};

int main()

btree obj;

int ele,choice;

while(choice!=6)

cout<<"\n_____"<<endl;

cout<<"1:enter element in an empty bst:"<<endl;

cout<<"2:dislay inorder,preorder and postorder traversal:"<<endl;

cout<<"3:find depth of tree"<<endl;

cout<<"4:insert a new node:"<<endl;

cout<<"5:search a node:"<<endl;

cout<<"6:quit"<<endl;

cout<<"enter your choice:";

cin>>choice;
switch(choice)

case 1:

obj.input();

break;

case 2:

obj.display();

break;

case 3:

cout<<"\ndepth of tree is:"<<obj.depth(obj.root);

break;

case 4:

cout<<"enter the element you want to insert:"<<endl;

cin>>ele;

root=obj.insert(obj.root,ele);

break;

case 5:

cout<<"\nenter element to be searched:";

cin>>ele;

obj.search(obj.root,ele);

break;

case 6:

exit;

default:

cout<<"wrong choice\n"<<endl;

return 0;

You might also like