Hi! I made a program that accepts random numbers, create a binary tree from it, then traverse it using in-order, pre-order and post-order traversal. The program is working well but my instructor added something else, when the user input numbers, it should be arranged just like an actual tree. The output should be like this:
Enter number of nodes: 5
What are the nodes? 12 22 18 5 3 [press enter]
12
5 22
3 18
Choices are:
[A] In Order traversal
[B] Pre Order traversal
[C] Post Order traversal
[D] Exit
Enter your choice: B
12 , 5, 3, 22, 18
How can I do that? Here's the code:
#include <iostream>
using namespace std;
class BT
{
private:
struct node
{
node* left;
node* right;
int data;
};
node* root;
public:
BT()
{
root = NULL;
}
bool isEmpty() const { return root==NULL; }
void displayinorder();
void inorder(node*);
void displaypreorder();
void preorder(node*);
void displaypostorder();
void postorder(node*);
void insert(int);
};
int main()
{
BT b;
int ch,tmp,num;
cout << "Enter number of nodes: ";
cin >> num;
for(int i=0; i<num; i++)
{
cout <<endl;
cout << "Enter node: ";
cin >> tmp;
b.insert(tmp);
}
choice:
cout <<endl<<endl;
cout << "[1] In-order Traversal" <<endl;
cout << "[2] Pre-order Traversal" <<endl;
cout << "[3] Post-order Traversal" <<endl;
cout << "[4] Exit" <<endl;
cout << "Enter your choice: ";
cin >> ch;
switch(ch)
{
case 1:
cout<<endl;
cout<<" In-Order Traversal "<<endl;
b.displayinorder();
break;
case 2:
cout<<endl;
cout<<" Pre-Order Traversal "<<endl;
b.displaypreorder();
break;
case 3:
cout<<endl;
cout<<" Post-Order Traversal "<<endl;
b.displaypostorder();
break;
case 4:
return 0;
break;
default:
cout << "Invalid entry. Try Again";
goto choice;
break;
}
system("pause>null");
return 0;
}
void BT::insert(int d)
{
node* t = new node;
node* parent;
t->data = d;
t->left = NULL;
t->right = NULL;
parent = NULL;
// is this a new tree?
if(isEmpty()) root = t;
else
{
//Note: ALL insertions are as leaf nodes
node* curr;
curr = root;
// Find the Node's parent
while(curr)
{
parent = curr;
if(t->data > curr->data) curr = curr->right;
else curr = curr->left;
}
if(t->data < parent->data)
parent->left = t;
else
parent->right = t;
}
}
void BT::displayinorder()
{
inorder(root);
}
void BT::inorder(node* p)
{
if(p != NULL)
{
if(p->left) inorder(p->left);
cout<<" "<<p->data<<" ";
if(p->right) inorder(p->right);
}
else return;
}
void BT::displaypreorder()
{
preorder(root);
}
void BT::preorder(node* p)
{
if(p != NULL)
{
cout<<" "<<p->data<<" ";
if(p->left) preorder(p->left);
if(p->right) preorder(p->right);
}
else return;
}
void BT::displaypostorder()
{
postorder(root);
}
void BT::postorder(node* p)
{
if(p != NULL)
{
if(p->left) postorder(p->left);
if(p->right) postorder(p->right);
cout<<" "<<p->data<<" ";
}
else return;
}