Presentation is loading. Please wait.

Presentation is loading. Please wait.

Additional Tree Traversal Example #1

Similar presentations


Presentation on theme: "Additional Tree Traversal Example #1"— Presentation transcript:

1 Additional Tree Traversal Example #1
// Public function to “spell out” the values from the root to the tree’s // // leaf nodes, with the results output on a separate line for each leaf. // template <class E> void BinaryTree<E>::spellFromTop(ofstream &os) { continueSpelling(root, os, ""); } // Protected function to concatenate the char value of the current subtree’s // // root to the str param. until a leaf is reached, whereupon str is output. // void BinaryTree<E>::continueSpelling(nodePtr treeRoot, ofstream &os, string str) if (treeRoot == NULL) return; else str += char(treeRoot->data); if ((treeRoot->left == NULL) && (treeRoot->right == NULL)) os << str << endl; continueSpelling(treeRoot->left, os, str); continueSpelling(treeRoot->right, os, str); CS 240 Chapter 10 – Trees

2 Applying spellFromTop to a Sample Tree
str: “M” M I C E L K O P S M str: “MI” I O str: “MO” str: “MIC” str: “MOM” str: “MOP” str: “MIL” C L M P output “MOM” str: “MILK” str: “MICE” str: “MOPS” str: “MILL” E K L S output “MICE” output “MILK” output “MILL” output “MOPS” CS 240 Chapter 10 – Trees

3 Additional Tree Traversal Example #2
// Public function to output the sequence of left and right offspring that // // comprise the longest path from the tree’s root to one of its leaf nodes. // template <class E> void BinaryTree<E>::outputLongestPath(ofstream &os) { os << longestPath(root); } // Protected function to recursively generate a string indicating which // // offspring (left or right) yield the longest path from the root to a leaf. // string BinaryTree<E>::longestPath(nodePtr treeRoot) if (treeRoot == NULL) return “”; else string leftStr = longestPath(treeRoot->left); string rightStr = longestPath(treeRoot->right); if (leftStr.size() > rightStr.size()) leftStr.insert(0, ‘L’); return leftStr; rightStr.insert(0, ‘R’); return rightStr; CS 240 Chapter 10 – Trees

4 Applying outputLongestPath to a Sample Tree
Final result: “LLRR” leftStr: “LLRR” rightStr: “RRL” “LRR” “RL” leftStr: “LRR” leftStr: “LL” rightStr: “” rightStr: “RL” “L” “L” “RR” leftStr: “L” leftStr: “L” leftStr: “” rightStr: “” rightStr: “” rightStr: “RR” “” “” “R” leftStr: “” leftStr: “L” leftStr: “” rightStr: “” rightStr: “R” rightStr: “” “” “” leftStr: “” leftStr: “” rightStr: “” rightStr: “” CS 240 Chapter 10 – Trees

5 AVL Trees Although an average search in an n-node binary search tree is O(logn), the worst case could be as bad as O(n). An AVL (Adelson-Velskii and Landis) tree places a balance condition on a binary search tree by requiring the left and right subtrees of each node to have heights differing by at most one. During insertion, this is accomplished by means of single and double rotations. Single rotations: Note that in each of the trees illustrated below: (any element of X)  k1  (any element of Y)  k2  (any element of Z) k1 k2 X Z Y So when a new element’s insertion causes an imbalance, “rotate” the tree to restore the balance. CS 240 Chapter 10 – Trees

6 Single Rotation Examples
27 14 31 12 30 45 27 14 31 12 30 45 5 27 12 31 5 30 45 14 INSERT 5 ROTATE 84 75 93 92 98 84 75 93 92 98 99 93 84 98 75 99 92 INSERT 99 ROTATE CS 240 Chapter 10 – Trees

7 Double rotations: If a single rotation doesn’t restore balance, a double rotation will.
Note that in the two trees illustrated below: (any value in A)  k1  (any value in B)  k2  (any value in C)  k3  (any value in D) k3 k2 D C B k1 A Also note that in the two trees illustrated below: (any value in E)  k1  (any value in F)  k2  (any value in G)  k3  (any value in H) k3 k2 H G F k1 E If a single rotation fails to restore balance after a new insertion, a double rotation may be tried. CS 240 Chapter 10 – Trees

8 Double Rotation Example
25 16 49 9 36 64 19 31 41 25 16 49 9 36 64 19 31 41 47 SINGLE ROTATION 25 16 36 9 31 49 19 41 64 47 INSERT 47 STILL UNBALANCED 25 16 49 9 36 64 19 31 41 25 16 49 9 36 64 19 31 41 47 DOUBLE ROTATION 25 16 41 9 36 49 19 47 64 31 INSERT 47 BALANCED! CS 240 Chapter 10 – Trees


Download ppt "Additional Tree Traversal Example #1"

Similar presentations


Ads by Google