Hello, everybody!

I've been struggling on implementing the Binary Tree data structure with some standard functions in it.
After reading information from many sources and some work on my own, I've came up to this: http://pastebin.com/B6Gtjb61

Problems occur in the main funciton.
What I can't understand is why does the postorder traversal just reverses the input values?
Why size, height and depth are equal since when I sketch the tree on a sheet, it's much more different?

I think I make some great mistakes in the insertion of the elements and the whole idea about the root node ... but since I'm really feeling myself a rookie in this kind of structures, I'd like to ask for your help.
I'd appreciate if you give me some ideas about how to fix this mess and have some legitimate output in the main() of this program.

Thank you very much!

Recommended Answers

All 2 Replies

First this input :

// inserting elements
    for (int i = 1; i <= 10; i++)
    {
    exampleTree.insert(i);
    }

is the worst case scenario for a binary search tree, it will be equivalent to a linked list. So perhaps you should try some predetermined values so you know what the output should be.

Second, post order does what its supposed to. It prints in reverse order because your binary tree is like this :

[1]
/  \
     [2]
    /  \
         [3]
        /  \
            ...

since there is no left node, it just keeps going right, until it hits [10] then it prints 10, then goes back to [9] and prints 9 and so on.


Third , size, height, and depth are equal because of the tree structure.

Try to do manual inputs.

I got it. Thank you very much. What I've previously done is just adding new right children and that's not what I needed. Different values and manual input, that's the right approach and it works fine.
Thank you very much for this response and the immense help. :)

Be a part of the DaniWeb community

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