Presentation is loading. Please wait.

Presentation is loading. Please wait.

Trees & Forests D. J. Foreman.

Similar presentations


Presentation on theme: "Trees & Forests D. J. Foreman."— Presentation transcript:

1 Trees & Forests D. J. Foreman

2 A Forest A R B C S T D E F

3 The problem Want to traverse this forest
Traversal algorithms are for binary and other special trees (AVL,2-3-4, etc) No "forest" traversals Can we convert?? And to what??

4 Definition General ordered trees map 1:1 to binary trees
Each node N in the ordered tree corresponds to a node N* in the (new) binary tree; left child of N* comes from the first (leftmost) child of N right child of N* comes from next sibling of N i.e., the next node (going to the right) at same level That is, each node's children are linked to each other by their right childptrs, and the new node only has a pointer to this list, via its left childptr Visualize each tree rotated 45 clockwise with siblings connected and right-child links broken

5 The 1-tree Algorithm Root of general tree becomes root of new binary tree Start at root of current (old) subtree Going left to right, find next child of that node If 1st node, insert it as left child of current root Insert remaining children of subtree root as right children of previously added node Repeat above 4 steps for all nodes at this level Repeat the above process (from step 3) for each node at this level.

6 Forest algorithm As for 1-tree, but treat the root of the 2nd, 3rd, etc trees as right-siblings of the first node of the new tree. Re-apply the 1-tree algorithm

7 The solution Convert ANY forest to a binary tree
Almost the same as for converting ANY non-binary tree to a binary tree

8 Convert tree 1 original A B C D E F A A B C B C D E F D E F

9 Add tree 2 A B R C S D T E F


Download ppt "Trees & Forests D. J. Foreman."

Similar presentations


Ads by Google