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

67_DSA_Prac4

The document contains a C++ implementation of a Binary Search Tree (BST) with functionalities such as inserting values, searching for values, printing the tree in different orders (preorder, inorder, postorder), finding the height of the tree, and determining the minimum value. It includes a menu-driven interface for user interaction, allowing users to perform various operations on the BST. The code also handles duplicate entries and provides feedback on user actions.

Uploaded by

sumdhok2002
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)
7 views

67_DSA_Prac4

The document contains a C++ implementation of a Binary Search Tree (BST) with functionalities such as inserting values, searching for values, printing the tree in different orders (preorder, inorder, postorder), finding the height of the tree, and determining the minimum value. It includes a menu-driven interface for user interaction, allowing users to perform various operations on the BST. The code also handles duplicate entries and provides feedback on user actions.

Uploaded by

sumdhok2002
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/ 7

Name- Sumedh Machhindra Dhokare Roll No- 67

SE(AIDS) DSA

#include <iostream> using


namespace std; class
Treenode
{
public: int
val; Treenode
*left;
Treenode *right;

Treenode()
{
val=0;
left=NULL;
right=NULL;
}
};

class BST
{
public:
Treenode *root;

BST()
{
root=NULL;
}

bool isempty()
{
if(root==NULL)
return true;
else
return false;
}

void insert(Treenode *newnode)


{
if(root==NULL)
{
root = newnode;
cout<<"Value Inserted as Root Node";

}
else
{
Treenode *temp = root;
while(temp!=NULL)
{
if(newnode->val == temp->val)
{
cout<<"Duplicates Not Allowed"<<endl;
return;
}
else if(newnode->val < temp->val && temp->left == NULL)
{
temp->left = newnode;
cout<<"Value Inserted to Left"<<endl;

}
else if(newnode->val < temp->val)
{
temp = temp->left;
}
else if(newnode->val > temp->val && temp->right == NULL)
{
temp->right = newnode;
cout<<"Value Inserted to Right"<<endl;

}
else
{
temp = temp->right;
}
}
}
}

void printPostorder (Treenode* r)


{ if (r ==
NULL)
return; printPostorder(r-
>left); printPostorder(r->right);
cout << r->val << " ";
}

void printInorder(Treenode* r)
{ if (r ==
NULL)
return;
printInorder(r->left);
cout << r->val << " ";
printInorder(r->right);
}
void printPreorder(Treenode* r)
{
if (r == NULL)
return; cout << r-
>val << " ";
printPreorder(r->left);
printPreorder(r->right);
}
Treenode * searchit(int v)
{
Treenode *temp = root;
while(temp!=NULL)
{
if(temp->val == v)
{
return temp;
}
else if(v < temp->val)
{
temp = temp->left;
}
else
{
temp = temp->right;
}
}
return NULL;
}

int height(Treenode *r)


{
if(r==NULL)
return -1;
else
{
int lheight = height(r->left);
int rheight = height(r->right);

if(lheight > rheight)


return lheight+1;
else
return rheight+1;
}
}

Treenode * Minval(Treenode *r)


{
Treenode *temp = r;
while(temp->left != NULL)
{
temp = temp->left;
}
return temp;
}

};

int main()
{

BST bst;

int ch=0,v;

while(ch!=7)

cout<<endl<<" Menu "<<endl;


cout<<"1. Insert Value"<<endl;
cout<<"2. Print BST"<<endl;
cout<<"3. Search a Value"<<endl;
cout<<"4. Height of the Tree"<<endl;
cout<<"5. Minimum Value"<<endl;

cout<<"6. Swapping of Nodes"<<endl;

cout<<"7. Exit"<<endl;

cout<<"Enter Your Choice: "; cin>>ch;

Treenode *node = new Treenode();

switch(ch)
{
case 1:
cout<<endl<<"Enter Value to Insert:";
cin>>v;
node->val = v;
bst.insert(node);
break;
case 2:

cout<<endl<<" Preorder Traversal ";


bst.printPreorder(bst.root);

cout<<endl<<" Inorder Traversal ";

bst.printInorder(bst.root);

cout<<endl<<" Postorder Traversal ";


bst.printPostorder(bst.root);

break;

case 3:
cout<<endl<<" Searching ";
cout<<endl<<"Enter Value to Search:";
cin>>v;
node = bst.searchit(v);
if(node != NULL)
{
cout<<endl<<"Value Found!";
}
else
{
cout<<endl<<"Value Not Found";
}
break;

case 4:
cout<<endl<<" Height of Tree
"; v = bst.height(bst.root);
cout<<endl<<"Height of the Tree: "<<v<<endl;
break;

case 5:
cout<<endl<<" Minimum Value "; node = bst.Minval(bst.root);
cout<<endl<<"Minimum Value: "<<node->val<<endl;
break;
case 6:

break;

case 7:
cout<<endl<<" Thank You!\n";
break;

default:
cout<<endl<<"Enter Valid Choice!in";
}
}
return 0;
}
Menu
1. Insert Value
2. Print BST
3. Search a Value
4. Height of the Tree
5. Minimum Value
6. Swapping of Nodes
7. Exit
Enter Your Choice: 1

Enter Value to Insert:12


Value Inserted as Root Node
Menu
1. Insert Value
2. Print BST
3. Search a Value
4. Height of the Tree
5. Minimum Value
6. Swapping of Nodes
7. Exit
Enter Your Choice: 1

Enter Value to Insert:23


Value Inserted to Right
Duplicates Not Allowed

Menu
1. Insert Value
2. Print BST
3. Search a Value
4. Height of the Tree
5. Minimum Value
6. Swapping of Nodes
7. Exit
Enter Your Choice: 1

Enter Value to Insert:34


Value Inserted to Right
Duplicates Not Allowed

Menu
1. Insert Value
2. Print BST
3. Search a Value
4. Height of the Tree
5. Minimum Value
6. Swapping of Nodes
7. Exit
Enter Your Choice: 1

Enter Value to Insert:45


Value Inserted to Right
Duplicates Not Allowed

Menu
1. Insert Value
2. Print BST
3. Search a Value
4. Height of the Tree
5. Minimum Value
6. Swapping of Nodes
7. Exit
Enter Your Choice: 2

Preorder Traversal 12 23 34 45
Inorder Traversal 12 23 34 45
Postorder Traversal 45 34 23 12
Menu
1. Insert Value
2. Print BST
3. Search a Value
4. Height of the Tree
5. Minimum Value
6. Swapping of Nodes
7. Exit
Enter Your Choice: 2

Preorder Traversal 12 23 34 45
Inorder Traversal 12 23 34 45
Postorder Traversal 45 34 23 12
Menu
1. Insert Value
2. Print BST
3. Search a Value
4. Height of the Tree
5. Minimum Value
6. Swapping of Nodes
7. Exit
Enter Your Choice: 3

Searching
Enter Value to Search:12

Value Found!
Menu
1. Insert Value
2. Print BST
3. Search a Value
4. Height of the Tree
5. Minimum Value
6. Swapping of Nodes
7. Exit
Enter Your Choice: 4

Height of Tree
Height of the Tree: 3

Menu
1. Insert Value
2. Print BST
3. Search a Value
4. Height of the Tree
5. Minimum Value
6. Swapping of Nodes
7. Exit
Enter Your Choice: 5

Minimum Value
Minimum Value: 12

Menu
1. Insert Value
2. Print BST
3. Search a Value
4. Height of the Tree
5. Minimum Value
6. Swapping of Nodes
7. Exit
Enter Your Choice: 6

Menu
1. Insert Value
2. Print BST
3. Search a Value
4. Height of the Tree
5. Minimum Value
6. Swapping of Nodes
7. Exit
Enter Your Choice: 2

Preorder Traversal 12 23 34 45
Inorder Traversal 12 23 34 45
Postorder Traversal 45 34 23 12
Menu
1. Insert Value
2. Print BST
3. Search a Value
4. Height of the Tree
5. Minimum Value
6. Swapping of Nodes
7. Exit
Enter Your Choice: 7

Thank You!

You might also like