80 likes | 209 Views
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) {
E N D
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. // template <class E> 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; else { continueSpelling(treeRoot->left, os, str); continueSpelling(treeRoot->right, os, str); } } } Chapter 10 – Trees
Applying spellFromTop to a Sample Tree M I O C L M P E K L S str: “M” M I O str: “MI” str: “MO” str: “MOM” str: “MOP” str: “MIC” 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” Chapter 10 – Trees
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. // template <class E> 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; } else { rightStr.insert(0, ‘R’); return rightStr; } } } Chapter 10 – Trees
“LRR” “RL” “L” “L” “RR” “R” “” “” “” “” Applying outputLongestPath to a Sample Tree Final result: “LLRR” leftStr: “LLRR” rightStr: “RRL” leftStr: “LRR” leftStr: “LL” rightStr: “” rightStr: “RL” leftStr: “L” leftStr: “L” leftStr: “” rightStr: “” rightStr: “” rightStr: “RR” leftStr: “” leftStr: “L” leftStr: “” rightStr: “” rightStr: “R” rightStr: “” leftStr: “” leftStr: “” rightStr: “” rightStr: “” Chapter 10 – Trees
k2 k1 k1 k2 Z X X Y Y Z 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) So when a new element’s insertion causes an imbalance, “rotate” the tree to restore the balance. Chapter 10 – Trees
27 27 27 INSERT 5 ROTATE 14 31 14 31 12 31 12 30 45 12 30 45 5 14 30 45 5 84 84 93 INSERT 99 ROTATE 75 93 75 93 84 98 92 98 92 98 75 92 99 99 Single Rotation Examples Chapter 10 – Trees
Double rotations: If a single rotation doesn’t restore balance, a double rotation will. k2 k1 k1 k3 k3 A k2 A B C D D B C k2 k3 k1 k3 k1 k2 H E E F G H F G 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) 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) If a single rotation fails to restore balance after a new insertion, a double rotation may be tried. Chapter 10 – Trees
SINGLE ROTATION 25 25 25 25 INSERT 47 16 16 49 49 16 49 16 36 9 9 19 19 36 36 64 64 9 19 36 64 9 19 31 49 31 31 41 41 31 41 41 64 47 47 25 DOUBLE ROTATION 25 INSERT 47 16 49 16 41 9 19 36 64 9 19 36 49 31 41 31 47 64 47 Double Rotation Example STILLUNBALANCED BALANCED! Chapter 10 – Trees