19 PRGM
19 PRGM
Program to create a binary tree and cout << root->info << " ";
traverse it in all the three methods. preorder(root->left);
preorder(root->right);
#include <iostream.h>
}
#include <conio.h>
}
#include <stdlib.h>
void postorder(Node* root)
struct Node
{
{
if (root != NULL)
int info;
{
Node* left;
postorder(root->left);
Node* right;
};
postorder(root->right);
Node* root = NULL;
void insert()
cout << root->info << " ";
{
int item;
}
Node* temp;
}
Node* p = NULL;
void inorder(Node* root)
Node* q;
{
cout << "Enter the element to be inserted:
if (root != NULL)
"<<endl;
{
cin >> item;
inorder(root->left);
q = new Node;
q->info = item;
cout << root->info << " ";
q->left = q->right = NULL;
temp = root;
inorder(root->right);
if (temp == NULL)
{
}
root = q;
}
return;
int main()
}
{
while (temp != NULL)
int ch;
{
while (1)
p = temp;
{
if (item < temp->info)
cout << "\nBST MENU"<<endl;
temp = temp->left;
cout << "1. Insert "<<endl;
else
cout << "2. Preorder" <<endl;
temp = temp->right;
cout << "3. Inorder "<<endl;
}
cout << "4. Postorder"<<endl;
if (item < p->info)
cout << "5. Exit"<<endl;
p->left = q;
cout << "Enter your choice: ";
else
cin >> ch;
p->right = q;
switch (ch)
}
{
void preorder(Node* root)
case 1: insert();
{
break;
if (root != NULL) {
case 2:
cout << "Preorder traversal\n";
preorder(root);
break;
case 3:
cout << "Inorder traversal\n";
inorder(root);
break;
case 4:
cout << "Postorder traversal\n";
postorder(root);
break;
case 5:
exit(0);
default:
cout << "Invalid choice\n";
}
}
}