Download presentation
Presentation is loading. Please wait.
1
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 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 Root of general tree becomes root of new binary tree Find first child of old node (left child of copied node) Insert this node as left child of new node Repeat above TWO steps for all nodes at this level 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. Complete the tree for all nodes. Insert children of a sibling node by locating their parent and then inserting them as first child. Repeat all steps until done
6
Forest algorithm As for 1-tree, but treat the root of the 2nd, 3rd, 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 B C D E F A B C D E F
9
Add tree 2 A B R C S D T E F
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.