0% found this document useful (0 votes)
16 views2 pages

19 PRGM

The document outlines a program for creating and traversing a binary tree using three methods: preorder, inorder, and postorder. It includes code snippets for inserting nodes and performing the traversals, as well as a simple menu interface for user interaction. The program utilizes a structure for nodes and handles user input to build the binary tree.

Uploaded by

divya R K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views2 pages

19 PRGM

The document outlines a program for creating and traversing a binary tree using three methods: preorder, inorder, and postorder. It includes code snippets for inserting nodes and performing the traversals, as well as a simple menu interface for user interaction. The program utilizes a structure for nodes and handles user input to build the binary tree.

Uploaded by

divya R K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

19.

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";

}
}
}

You might also like