Q. Binary Search Tree.: Using
Q. Binary Search Tree.: Using
#include <iostream>
using namespace std;
struct BinaryTree
{
int data;
BinaryTree *left;
BinaryTree *right;
};
/*
{
n1
/ \
n2 n3
/ \
n4 n5
}
*/
InOrder(temp->left);
cout << temp->data << " ";
InOrder(temp->right);
}
int main()
{
int i = 1, val;
root = NewNodeCreation(10);
root->left = NewNodeCreation(4);
root->right = NewNodeCreation(12);
root->left->left = NewNodeCreation(2);
root->left->right = NewNodeCreation(5);
while (i != 0)
{
cout << "Enter Choice :\n0. End\n1. Insert\n2. Print Inorder\nEnter :
";
cin >> i;
switch (i)
{
case 1:
{
cout << "Enter data : ";
cin >> val;
NewNode = NewNodeCreation(val);
Insert(root);
break;
}
case 2:
{
cout << "Inorder :- ";
InOrder(root);
cout << endl;
break;
}
case 0:
{
break;
}
default:
cout<<"Invadlid.";
break;
}
}
return 0;
}
Output:
Inorder :- 2 4 5 10 12
Enter Choice :
0. End
1. Insert
2. Print Inorder
Enter : 1
Enter data : 3
Enter Choice :
0. End
1. Insert
2. Print Inorder
Enter : 2
Inorder :- 2 3 4 5 10 12
Enter Choice :
0. End
1. Insert
2. Print Inorder
Enter : 1
Enter data : 11
Enter Choice :
0. End
1. Insert
2. Print Inorder
Enter : 2
Inorder :- 2 3 4 5 10 11 12
Enter Choice :
0. End
1. Insert
2. Print Inorder
Enter : 1
Enter data : 15
Enter Choice :
0. End
1. Insert
2. Print Inorder
Enter : 2
Inorder :- 2 3 4 5 10 11 12 15
Enter Choice :
0. End
1. Insert
2. Print Inorder
Enter : 1
Enter data : 6
Enter Choice :
0. End
1. Insert
2. Print Inorder
Enter : 2
Inorder :- 2 3 4 5 6 10 11 12 15
Enter Choice :
0. End
1. Insert
2. Print Inorder
Enter : 0