Professional Documents
Culture Documents
roll no:
div:
Experiment No5:
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>
#define size 20
using namespace std;
class bintree
{
typedef struct bst
{
int data;
struct bst *left,*right;
} node;
node *root,*New, *temp, *parent;
public:
node *que[20];
int front,rear;
bintree()
{
root=NULL;
front=rear=-1;
}
void create();
void display();
void find();
void insert(node *, node *);
void inorder(node *);
void search(node**, int, node **);
void LongestPathNodes();
int Depth(node *);
void FindMinValue();
void minValue(node *,int*);
void Mirrorimg();
void mirror(node *);
void LevelWiseDisplay(node *root);
void enque(node *temp);
node *deque();
};
void bintree::create()
{
New = new node;
New->left = NULL;
New->right = NULL;
cout<<"\n Enter The Element : ";
cin>> New->data;
if (root == NULL)
root = New;
else
insert(root, New);
}
void bintree::display()
{
if (root == NULL)
cout<<"Tree Is Not Created";
else
{
cout<<"\n The Tree is : ";
inorder(root);
}
}
void bintree::FindMinValue()
{
int min = root->data;
minValue(root, &min);
cout<<"\n The minimum value node within a tree is: "<< min;
}
void bintree::find()
{
int key;
cout << "\n Enter The Element Which You Want To Search : ";
cin >> key;
temp=root;
search(&temp, key, &parent);
if (temp== NULL)
cout<< "\n Element is not present ";
else
cout<< "\n parent of node "<<temp->data<<" is "<<parent->data;
}
void bintree::LongestPathNodes()
{
if (root==NULL)
cout<<"\n Tree is Empty!!!";
else
{ /* compute the depth of each subtree */
int lDepth = Depth(root);
int rDepth = Depth(root);
/* use the larger one */
if (lDepth > rDepth)
cout << "\n The number of nodes on Longest Path = "<< lDepth + 1;
//retum(IDepth + 1),
else
cout << "\n The number of nodes on Longest Path = " << rDepth + 1;
}
}
bintree::node *bintree::deque()
{
node *temp;
if (front== rear)
{
cout << "Queue is empty";
return NULL;
}
front++;
temp= que[front];
return temp;
}
void bintree::Mirrorimg()
{
cout << "\n Original Tree",
LevelWiseDisplay(root);
mirror(root);
cout << "\n Tree with Swapped Nodes";
LevelWiseDisplay(root);
mirror(root); //bringing back the tree to original state
}
int main()
{
int choice;
char ans='N';
bintree tr;
cout<<"\n\t****Program for Binary Search Tree *****";
do
{
cout <<"\n1.Create";
cout <<"\n2.Display";
cout <<"\n3.Longest Path Nodes",
cout <<"\n4.Find Minimum Value",
cout <<"\n5.Change Tree by Swapping nodes";
cout <<"\n6.Search";
cout <<"\n\n Enter your choice :";
cin >> choice;
switch (choice)
{
case 1:do
{
tr.create();
cout <<"Do u Want To enter More elements?(y/n)"
<< endl;
cin >> ans;
} while (ans == 'y');
break;
case 2:tr.display();
break;
case 3:tr.LongestPathNodes();
break;
case 4:tr.FindMinValue();
break;
case 5:tr. Mirrorimg();
break;
case 6:tr. find();
break;
}
}while(choice!=7);
return 0;
}
1.Create
2.Display
3.Longest Path Nodes
4.Find Minimum Value
5.Change Tree by Swapping nodes
6.Search
The Tree is : 4 7 8 9 10 11 12 13
1.Create
2.Display
3.Longest Path Nodes
4.Find Minimum Value
5.Change Tree by Swapping nodes
6.Search
Original Tree
10
8 12
7 9 11 13
4
Tree with Swapped Nodes
10
12 8
13 11 9 7
4
1.Create
2.Display
3.Longest Path Nodes
4.Find Minimum Value
5.Change Tree by Swapping nodes
6.Search
Enter your choice :6
Element 4 is Present
parent of node 4 is 7
1.Create
2.Display
3.Longest Path Nodes
4.Find Minimum Value
5.Change Tree by Swapping nodes
6.Search