Maham Dsa Lab 2
Maham Dsa Lab 2
Code:
#include<iostream>
#include<queue>
struct bstnode{
int id;
string name;
bstnode *left,*right;
};
class bst{
bstnode *root;
temp->id=num;
temp->name=name;
temp->left=temp->right=NULL;
return temp;
if(root==NULL)
root = getbstnode(num,name);
else if(root->id<num)
root->right=addnode(num,name,root->right);
else
root->left=addnode(num,name,root->left);
return root;
if(root==NULL)
return false;
else if(root->id==num)
{
cout<<"\n\t"<<root->id<<" , "<<root->name;
return true;
else if(root->id>num)
return check(num,root->left);
else
return check(num,root->right);
if(num<root->id)
root->left=deletinbst(num,root->left);
else if(num>root->id)
root->right=deletinbst(num,root->right);
}
if(root->left==NULL)
return root->right;
else if(root->right==NULL)
return root->right;
bstnode* tmp=inor(root->right);
root->id=tmp->id;
root->name=tmp->name;
root->right=deletinbst(tmp->id,root->right);
bstnode* temp=root;
while(temp!=NULL&&temp->left!=NULL)
temp=temp->left;
return temp;
public:
bst()
root=NULL;
root=deletinbst(num,root);
root=addnode(num,name,root);
return check(num,root);
void printlevelorder()
{bstnode * root=this->root;
int level=0;
if(root==NULL)
return ;
queue<bstnode*> q;
q.push(root);
q.push(NULL);
bstnode* node=q.front();
q.pop();
if(node!=NULL)
if(node->left)
q.push(node->left);
if(node->right)
q.push(node->right);
else if(!q.empty())
level++;
q.push(NULL);
};
int main()
{
bst t1;
int id,num;
string name;
start:
system("cls");
cout<<"\n\t------------------------------------------------------";
cout<<"\n\t------------------------------------------------------";
cout<<endl;
cin>>num;
if(num==1)
cin>>id;
cin>>name;
t1.insert(id,name);
else if(num==2)
{
cin>>id;
t1.delet(id);
else if(num==3)
cin>>id;
(t1.search(id));//display itself
else if(num==4)
t1.printlevelorder();
else
exit(0);
system("pause");
goto start;
return 0;
Output:
input:
(inserting following)
(50 , frost) , (89 , ledge) , (34 , wanderlove) , (23 , roomies) , (90 , hunted) , (79 , captivate) ,
(2 , winter) , (09 , ingarnate) , (78 , shiver) , ( 10 , boomerang )
50
(l50)34 89(r50)
(l23)2 (l79)78
9(r2)
(l9)10
Adding
Printing
Deleting
Searching :