Code:
# include iostream
# include cstdlib
using namespace std;
/*
* Node Declaration
*/
struct node
{
int info;
struct node *left;
struct node *right;
}*root;
/*
* Class Declaration
*/
class BST
{
public:
void insert(node *, node *);
void inorder(node *);
void display(node *, int);
BST()
{
root = NULL;
}
};
/*
* Main Contains Menu
*/
int main()
{
int choice, num;
BST bst;
node *temp;
while (1)
{
cout<<"-----------------"<
cout<<"Operations on BST"<
cout<<"-----------------"<
cout<<"1.Insert Element "<
cout<<"2.Inorder Traversal"<
cout<<"3.Display"<
cout<<"4.Quit"<
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
temp = new node;
cout<<"Enter the number to be inserted : ";
cin>>temp->info;
bst.insert(root, temp);
break;
case 2:
cout<<"Inorder Traversal of BST:"<
bst.inorder(root);
cout<
break;
case 3:
cout<<"Display BST:"<
bst.display(root,1);
cout<
break;
case 4:
exit(1);
default:
cout<<"Wrong choice"<
}
}
}
/*
* Inserting Element into the Tree
*/
void BST::insert(node *tree, node *newnode)
{
if (root == NULL)
{
root = new node;
root->info = newnode->info;
root->left = NULL;
root->right = NULL;
cout<<"Root Node is Added"<
return;
}
if (tree->info == newnode->info)
{
cout<<"Element already in the tree"<
return;
}
if (tree->info > newnode->info)
{
if (tree->left != NULL)
{
insert(tree->left, newnode);
}
else
{
tree->left = newnode;
(tree->left)->left = NULL;
(tree->left)->right = NULL;
cout<<"Node Added To Left"<
return;
}
}
else
{
if (tree->right != NULL)
{
insert(tree->right, newnode);
}
else
{
tree->right = newnode;
(tree->right)->left = NULL;
(tree->right)->right = NULL;
cout<<"Node Added To Right"<
return;
}
}
}
/*
* In Order Traversal
*/
void BST::inorder(node *ptr)
{
if (root == NULL)
{
cout<<"Tree is empty"<
return;
}
if (ptr != NULL)
{
inorder(ptr->left);
cout<
inorder(ptr->right);
}
}
/*
* Display Tree Structure
*/
void BST::display(node *ptr, int level)
{
int i;
if (ptr != NULL)
{
display(ptr->right, level+1);
cout<
if (ptr == root)
cout<<"Root->: ";
else
{
for (i = 0;i < level;i++)
cout<<" ";
}
cout<
display(ptr->left, level+1);
}
}
Output:
-----------------
Operations on BST
-----------------
1.Insert Element
2.Inorder Traversal
3.Display
4.Quit
Enter your choice : 1
Enter the number to be inserted : 7
Root Node is Added
-----------------
Operations on BST
-----------------
1.Insert Element
2.Inorder Traversal
3.Display
4.Quit
Enter your choice : 3
Display BST:
Root->: 7
-----------------
Operations on BST
-----------------
1.Insert Element
2.Inorder Traversal
3.Display
4.Quit
Enter your choice : 1
Enter the number to be inserted : 3
Node Added To Left
-----------------
Operations on BST
-----------------
1.Insert Element
2.Inorder Traversal
3.Display
4.Quit
Enter your choice : 3
Display BST:
Root->: 7
3
-----------------
Operations on BST
-----------------
1.Insert Element
2.Inorder Traversal
3.Display
4.Quit
Enter your choice : 2
Inorder Traversal of BST:
3 7
-----------------
Operations on BST
-----------------
1.Insert Element
2.Inorder Traversal
3.Display
4.Quit
Enter your choice : 1
Enter the number to be inserted : 9
Node Added To Right
-----------------
Operations on BST
-----------------
1.Insert Element
2.Inorder Traversal
3.Display
4.Quit
Enter your choice : 3
Display BST:
9
Root->: 7
3
-----------------
Operations on BST
-----------------
1.Insert Element
2.Inorder Traversal
3.Display
4.Quit
Enter your choice : 2
Inorder Traversal of BST:
3 7 9
-----------------
Operations on BST
-----------------
1.Insert Element
2.Inorder Traversal
3.Display
4.Quit
Enter your choice : 1
Enter the number to be inserted : 2
Node Added To Left
-----------------
Operations on BST
-----------------
1.Insert Element
2.Inorder Traversal
3.Display
4.Quit
Enter your choice : 1
Enter the number to be inserted : 1
Node Added To Left
-----------------
Operations on BST
-----------------
1.Insert Element
2.Inorder Traversal
3.Display
4.Quit
Enter your choice : 3
Display BST:
9
Root->: 7
3
2
1
-----------------
Operations on BST
-----------------
1.Insert Element
2.Inorder Traversal
3.Display
4.Quit
Enter your choice : 2
Inorder Traversal of BST:
1 2 3 7 9
-----------------
Operations on BST
-----------------
1.Insert Element
2.Inorder Traversal
3.Display
4.Quit
Enter your choice : 1
Enter the number to be inserted : 8
Node Added To Left
-----------------
Operations on BST
-----------------
1.Insert Element
2.Inorder Traversal
3.Display
4.Quit
Enter your choice : 3
Display BST:
9
8
Root->: 7
3
2
1
-----------------
Operations on BST
-----------------
1.Insert Element
2.Inorder Traversal
3.Display
4.Quit
Enter your choice : 2
Inorder Traversal of BST:
1 2 3 7 8 9
-----------------
Operations on BST
-----------------
1.Insert Element
2.Inorder Traversal
3.Display
4.Quit
Enter your choice : 1
Enter the number to be inserted : 11
Node Added To Right
-----------------
Operations on BST
-----------------
1.Insert Element
2.Inorder Traversal
3.Display
4.Quit
Enter your choice : 3
Display BST:
11
9
8
Root->: 7
3
2
1
-----------------
Operations on BST
-----------------
1.Insert Element
2.Inorder Traversal
3.Display
4.Quit
Enter your choice : 2
Inorder Traversal of BST:
1 2 3 7 8 9 11
-----------------
Operations on BST
-----------------
1.Insert Element
2.Inorder Traversal
3.Display
4.Quit
Enter your choice : 4
More C++ Programs: