Presentation is loading. Please wait.

Presentation is loading. Please wait.

Trees & Forests D. J. Foreman. A Forest A BC R ST DE F.

Similar presentations


Presentation on theme: "Trees & Forests D. J. Foreman. A Forest A BC R ST DE F."— Presentation transcript:

1 Trees & Forests D. J. Foreman

2 A Forest A BC R ST DE 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 child of N right child of N' comes from N 's next sibling --- i.e., the next node at N's level-order That is, each node's children are linked 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 1. Root of general tree becomes root of new binary tree 2. Find first child of old node (left child of copied node) 3. Insert this node as left child of new node 4. Repeat above TWO steps for all nodes at this level 5. When no siblings left, return to parent of the last node entered and repeat the above process. In other words, determine the first sibling of the last node entered. 6. Complete the tree for all nodes. 7. Insert children of a sibling node by locating their parent and then inserting them as first child. 8. Repeat all steps until done

6 Forest algorithm As for 1-tree, but treat the root of the 2 nd, 3 rd, etc trees as siblings of the first root. 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 A BC DE F A B C D E F

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


Download ppt "Trees & Forests D. J. Foreman. A Forest A BC R ST DE F."

Similar presentations


Ads by Google