67_DSA_Prac4
67_DSA_Prac4
SE(AIDS) DSA
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;
}
}
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 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 main()
{
BST bst;
int ch=0,v;
while(ch!=7)
cout<<"7. Exit"<<endl;
switch(ch)
{
case 1:
cout<<endl<<"Enter Value to Insert:";
cin>>v;
node->val = v;
bst.insert(node);
break;
case 2:
bst.printInorder(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
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
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
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!