Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to build a binary tree from preorder and inorder traversals

Im doing an assignment on building a binary tree from the preorder and inorder traversals (a char in each Node) and im trying to wrap my brain around how to build the actual tree.

Here is my thought process on how to accomplish this:

  1. store the first entry in the preorder as the root node
  2. search the inorder for that entry.
  3. take the chars to the left of the root node and save them as a char array.
  4. take the chars to the right of the root node and save them as a char array.
  5. make a new tree, with the root as the parent and its 2 children being the left and right char arrays.
  6. keep going recursively until the preorder length is 0.

I have steps 1-4 taken care of, but im not too sure how to properly build my tree, and was wondering if anyone had any pointers. Thank you.

like image 553
jfisk Avatar asked Mar 06 '11 20:03

jfisk


1 Answers

Do the recursion before building the new tree. So, your list would look like this:

  1. If the arrays have length 1, just return a leaf node with this single item in it. (This is the recursion foundation.) (O(1))
  2. Store the first entry in the preorder array as the root node. (O(1))
  3. Search the inorder array for that entry. (O(n))
  4. Take the chars to the left of the root node in the inorder array and save them as a char array. Take the same amount of characters from the preorder array (after the root). (O(n), or O(1) when just throwing pointers/indexes around.)
  5. Take the chars to the right of the root node and save them as a char array. Take the same amount of characters from the preorder array (after the first part – that should be just the remainding part). (O(n), or O(1) when just throwing pointers/indexes around.)
  6. Recursively make a tree from both left char arrays.
  7. Recursively make a tree from both right char arrays.
  8. Combine both trees with your root node. (O(1).)

The non-recursive parts can be done in O(n) overall, and summing them up for each recursion level is also O(n) each. The total runtime thus depends on the number of recursion levels. If you have a approximately balanced tree, the depth is O(log n), thus we get to O(n · log n) at all. As the only necessarily slow part is the search of the root node in the inorder array, I guess we could optimize that even a bit more if we know more about the tree.

In the worst case we have one recursion level for each node in the tree, coming to complexity O(n·n).

Example: Preorder ABCDEF, Inorder FEDCBA, Tree:

                                   +---+
                                   | A |
                                   ++--+
                                    |
                            +---+   |
                            | B +<--+
                            ++--+
                             |
                     +---+   |
                     | C +<--+
                     ++--+
                      |
              +---+   |
              | D +<--+
              ++--+
               |
       +---+   |
       | E +<--+
       ++--+
        |
+---+   |
| F +<--+
+---+
like image 77
Paŭlo Ebermann Avatar answered Oct 13 '22 01:10

Paŭlo Ebermann