Assignment 4
Assignment 4
Assignment no.2
Beginning with an empty binary search tree, Construct binary search tree by inserting the
values in the order given. After constructing a binary tree -
i. Insert new node
ii. Find number of nodes in longest path from root
iii. Minimum data value found in the tree
iv. Change a tree so that the roles of the left and right pointers are swapped at every node
v. Search a value.”
#include <iostream.h>
class node
{
public:
int data;
node *left;
node *right;
};
class bst
{
node *root;
public:
bst()
{
root=NULL;
}
node *create();
void insert(node *,node *);
void inorder(node *);
void smallest_value();
void largest_value();
1
void search(node *,int);
void swap(node *);
int longest_path(node *);
};
node *bst::create()
{
int n,value;
cout<<"\nEnter no. of nodes for BST: ";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"\nEnter value for node: ";
cin>>value;
node *p=new node;
p->data=value;
p->left=NULL;
p->right=NULL;
insert(root,p);
}
return root;
}
2
{
if(x->data<p->data)
{
if(x->right==NULL)
x->right=p;
else
insert(x->right,p);
}
else
{
if(x->left==NULL)
x->left=p;
else
insert(x->left,p);
}
}
}
void bst::inorder(node *x)
{
if(x!=NULL)
{ inorder(x->left);
cout<<x->data<<" ";
inorder(x->right);
}
}
void bst::search(node *x,int key)
{ if(x==NULL)
cout<<"\nKey Not Found..!!";
else
{
3
if(key<x->data)
search(x->left,key);
else
{
if(key>x->data)
search(x->right,key);
else
cout<<"\nKey Found";
}
}
}
void bst::largest_value()
{
if(root==NULL)
{
cout<<"\nTree is empty";
}
else
{
node *temp=root;
while(temp->right!=NULL)
{
temp=temp->right;
}
cout<<"\nLargest value is= "<<temp->data;
}
}
void bst::smallest_value()
{
4
if(root==NULL)
{
cout<<"\nTree is empty..!!";
}
else
{
node *temp=root;
while(temp->left!=NULL)
{
temp=temp->left;
}
cout<<"\nSmallest value is= "<<temp->data;
}
}
5
return(height_right+1);
}
}
void main()
{
bst b1;
int choice;
int height;
node *x;
cout<<"..**Binary Search Tree**..";
do
{
6
cout<<"\n1. Create BST: ";
cout<<"\n2. Traverse BST: ";
cout<<"\n3. Largest value from the tree: ";
cout<<"\n4. Minimum data fron the tree: ";
cout<<"\n5. Longest path from the root: ";
cout<<"\n6. Search Function: ";
cout<<"\n7. Swap Function: ";
cout<<"\n8. End of program: ";
cout<<"\nEnter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
x=b1.create();
break;
case 2:
b1.inorder(x);
break;
case 3:
b1.largest_value();
break;
case 4:
b1.smallest_value();
break;
case 5:
height=b1.longest_path(x);
cout<<"\nLongest Path: "<<height;
break;
case 6:
int key;
7
cout<<"\nEnter value to be searched: ";
cin>>key;
b1.search(x,key);
break;
case 7:
b1.swap(x);
b1.inorder(x);
break;
case 8:
cout<<"\n End of the Program. .. !!";
break;
}
}while(choice!=8);
}
Output:
..**Binary Search Tree**..
1. Create BST:
2. Traverse BST:
3. Largest value from the tree:
4. Minimum data fron the tree:
5. Longest path from the root:
6. Search Function:
7. Swap Function:
8. End of program:
Enter your choice: 1
8
Root is added..!!
Enter value for node: 3
1. Create BST:
2. Traverse BST:
3. Largest value from the tree:
4. Minimum data fron the tree:
5. Longest path from the root:
6. Search Function:
7. Swap Function:
8. End of program:
Enter your choice: 2
23678
1. Create BST:
2. Traverse BST:
3. Largest value from the tree:
4. Minimum data fron the tree:
5. Longest path from the root:
6. Search Function:
7. Swap Function:
8. End of program:
Enter your choice: 3
9
Largest value is= 8
1. Create BST:
2. Traverse BST:
3. Largest value from the tree:
4. Minimum data fron the tree:
5. Longest path from the root:
6. Search Function:
7. Swap Function:
8. End of program:
Enter your choice: 4
Longest Path: 5
1. Create BST:
2. Traverse BST:
3. Largest value from the tree:
4. Minimum data fron the tree:
5. Longest path from the root:
6. Search Function:
7. Swap Function:
10
Sanika Sachin Ekshette
Roll No: - SCC56
8. End of program:
Enter your choice: 6
Key Found
1. Create BST:
2. Traverse BST:
3. Largest value from the tree:
4. Minimum data fron the tree:
5. Longest path from the root:
6. Search Function:
7. Swap Function:
8. End of program:
Enter your choice: 8
11