Help w/ Binary Tree Traversal (prefix / postfix)

Hi everyone I'm working on this BTree Class and I can't seem to figure out how to make the traversal functions (prefix or pre-order and the postfix or post-order) to work as intended.

I uploaded an image of what my Tree looks like: http://i43.tinypic.com/fv9w0m.jpg

These are parts of my BTree:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class BTree
{
   private:
      int data;          // --- data stored in node of tree
      BTree* left;       // --- pointer to left subtree
      BTree* right;      // --- pointer to right subtree
      BTree* parent;     // --- pointer to the parent node;

// There are more functions here, but no need to display them :)

      void infix(void);              // do infix traversal
      void prefix(void);             // do prefix traversal
      void postfix(void);            // do postfix traversal
      void level(void);              // do level order traversal
};


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
/******************************************************************************
* infix() - LVR - Goes Left then Visit(display Data), then Goes Right
******************************************************************************/
void BTree::infix()
{
   if (left != NULL) left->infix();   // L
   cout << data << " ";               // V
   if (right != NULL) right->infix(); // R
}

/******************************************************************************
* prefix() - VLR - Visits then Goes Left then goes Right
******************************************************************************/
void BTree::prefix()
{
   cout << data << " ";               // V
   if (left != NULL) left->infix();   // L
   if (right != NULL) right->infix(); // R
}

/******************************************************************************
* postFix() - LRV - Goes Left then Goes Right then Visits
******************************************************************************/
void BTree::postfix()
{
   if (left != NULL) left->infix();   // L
   if (right != NULL) right->infix(); // R
   cout << data << " ";               // V
}


The infix function works like a charm, but the problem is that using the code that I have for the prefix/postfix functions, result in the wrong output.

Should Output:
Tree in Prefix-Order Traversal
23 15 29 10 0 26 2 20 21 27 9 14 3 22 1
Tree in Postfix-Order Traversal
10 0 29 2 20 26 15 9 14 27 22 1 3 21 23


Actual Output:
Tree in Prefix-Order Traversal
23 10 29 0 15 2 26 20 9 27 14 21 22 3 1
Tree in Postfix-Order Traversal
10 29 0 15 2 26 20 9 27 14 21 22 3 1 23


It really doesn't make any sense to me that using those functions produce the output above. Since they're recursive, it makes sense to me that for each level down, they do the visit in the right order, but for example in the prefix, it "jumps" from the root (23) to the bottom left (10). (see the image of the tree in the link on top)

Can anyone help me out figure out what am I doing wrong in those functions, or why they are behaving like that??
Last edited on
I guess if I wanted to maintain certain patterns these three functions would look like these:
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
/******************************************************************************
* infix() - LVR - Goes Left then Visit(display Data), then Goes Right
******************************************************************************/
void BTree::infix()
{
   // this would be a normal traverse of the tree
   if (left != NULL) left->infix();   // L
   cout << data << " ";               // V
   if (right != NULL) right->infix(); // R
}

/******************************************************************************
* prefix() - VLR - Visits then Goes Left then goes Right
******************************************************************************/
void BTree::prefix()
{
   cout << data << " ";               // V
   if (left != NULL) left->prefix();   // L
   if (right != NULL) right->prefix(); // R
}

/******************************************************************************
* postFix() - LRV - Goes Left then Goes Right then Visits
******************************************************************************/
void BTree::postfix()
{
   if (left != NULL) left->postfix();   // L
   if (right != NULL) right->postfix(); // R
   cout << data << " ";               // V
}


I guess I didn't understand why we didn't maintain consistency with the calls through the functions.
oh, dang LOL I must be blind!
thx for the help!
the problem was that when I copy/pasted, I forgot to change the name of the function to be called(I was calling the infix function when I should have been calling the other functions...)
Last edited on
Topic archived. No new replies allowed.