Solve my Tree Problem.

I have made a program for making and traversing Binary Search tree. My program is running Ok the only problem is when i am inserting nodes in order.
8,7,6,9,5,1,4,3
The post treveral is printing.
1,3,4,5,6,7,9,8
However according to my calculations it should print.
3,4,1,5,6,7,9,8

my Post treversal function is.

ANY WHERE HEAD IS ROOT NODE IN THE PROGRAMM.

1
2
3
4
5
6
7
8
9
10
11
void postorder(bst *temp)
             {
                  if(temp!=NULL)
                  {
                          inorder(temp->left);
                          inorder(temp->right);
                          cout<<temp->myData<<" ";
                          
                  }
                  
             }


I am passing root node from my main program.

It seems ok to me the only problem should be in insertion, my be my program is not implementing the tree perfectly that's why it is not printing perfectly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
void insert(int num)
             {
                  
                  bst *temp=head;
                  bst *prev=head;
                  bst *p=new bst(num);
                  while(temp)
                  {
                             prev=temp;
                             if(temp->myData>p->myData)
                             {
                                                       temp=temp->left;
                             }
                             else
                                                       temp=temp->right;
                             
                  }
                  if(head==NULL)
                  {
                           head=p;
                  }
                  else
                  {
                           if(prev->myData>p->myData)
                           prev->left=p;
                           else
                           prev->right=p;   
                  }
             }
             


It is in the class containing head as the element of class that's why it does not need the head pointer in the parameter. Head is root node.


http://www.infinityloopers.com
Why is postorder calling the inorder?
Victim of Copy paste, thanks...!-)
Topic archived. No new replies allowed.