0

Hi,

My teacher asked us for homework to do the following task:

Create a binary tree with the vowels and walk through this tree using a pre-order traversal algorithm and insert the output in a array, then write an algorithm to print the array using post-order traversal.

I haven't figured out how can I do this, I can't use a tree, The traversal must be done using the array.

Does anyone have any idea?

2
Contributors
1
Reply
3
Views
4 Years
Discussion Span
Last Post by deceptikon
0

For example using Wikipedia's traversal result (F, B, A, D, C, E, G, I, H), the execution logic might go like this:

F, B, A, D, C, E, G, I, H
^
F, B, A, D, C, E, G, I, H
^  ^
F, B, A, D, C, E, G, I, H
^     ^
F, B, A, D, C, E, G, I, H        A
^  ^  *
F, B, A, D, C, E, G, I, H        A
^     *  ^
F, B, A, D, C, E, G, I, H        A
^     *     ^
F, B, A, D, C, E, G, I, H        A, C
^     *  ^  *
F, B, A, D, C, E, G, I, H        A, C
^     *     *  ^
F, B, A, D, C, E, G, I, H        A, C, E
^     *  ^  *  *
F, B, A, D, C, E, G, I, H        A, C, E, D
^  ^  *  *  *  *
F, B, A, D, C, E, G, I, H        A, C, E, D, B
^  *  *  *  *  *
F, B, A, D, C, E, G, I, H        A, C, E, D, B
^  *  *  *  *  *  ^
F, B, A, D, C, E, G, I, H        A, C, E, D, B
^  *  *  *  *  *     ^
F, B, A, D, C, E, G, I, H        A, C, E, D, B
^  *  *  *  *  *        ^
F, B, A, D, C, E, G, I, H        A, C, E, D, B, H
^  *  *  *  *  *     ^  *
F, B, A, D, C, E, G, I, H        A, C, E, D, B, H, I
^  *  *  *  *  *  ^  *  *
F, B, A, D, C, E, G, I, H        A, C, E, D, B, H, I, G
^  *  *  *  *  *  *  *  *
F, B, A, D, C, E, G, I, H        A, C, E, D, B, H, I, G, F
*  *  *  *  *  *  *  *  *

The idea is that at each step you'd select a root, then find the smallest unprocessed item first from the left "subtree" (anything less than the root), then from the right subtree (anything greater than or equal to the root). The steps would be done recursively, so subtrees themselves could also have left and right subtrees.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.