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

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 << " In-order Traversal" <<endl;
cout << " Pre-order Traversal" <<endl;
cout << " Post-order Traversal" <<endl;
cout << " 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;
}
``````

## All 2 Replies

when the user input numbers, it should be arranged just like an actual tree

If the tree is required to be displayed top-down then you're looking at some form of level order traversal to get the nodes in the proper order for display and a relatively non-trivial algorithm for determining the grid positioning of each node for printing. If you can get away with it, rotating the tree by 90 degrees is leaps and bounds simpler, and looks just as good:

``````#include <cstdlib>
#include <iostream>

using namespace std;

struct node
{
int data;
node* left;
node* right;

node(int data): data(data), left(0), right(0) {}
};

node* insert(node* root, int data)
{
if (root == 0)
{
return new node(data);
}
else if (data < root->data)
{
root->left = insert(root->left, data);
}
else
{
root->right = insert(root->right, data);
}

return root;
}

void prettyprint_node(node* root, int level)
{
for (int i = 0; i < level; i++)
{
cout << '\t';
}

if (root == 0)
{
cout << "--\n";
}
else
{
cout << root->data << '\n';
}
}

void prettyprint_90(node* root, int level)
{
if (root == 0)
{
prettyprint_node(root, level);
}
else
{
prettyprint_90(root->right, level + 1);
prettyprint_node(root, level);
prettyprint_90(root->left, level + 1);
}
}

int main()
{
node *root = 0;

for (int i = 0; i < 10; i++)
{
root = insert(root, rand() % 100);
}

prettyprint_90(root, 0);
}
``````

Hey Thanks! I will try doing this :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.