1 / 29

Chapter 4: Trees

Chapter 4: Trees. Tree Traversal. Binary Trees. Balanced Trees (AVL, Splay, B-). Search Alternatives. CS 340. Page 60. . . . . . . . . . . . . . . The Tree ADT. A tree is a collection of nodes, one of which is identified as the root .

josie
Download Presentation

Chapter 4: Trees

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 4: Trees • Tree Traversal • Binary Trees • Balanced Trees (AVL, Splay, B-) • Search Alternatives CS 340 Page 60

  2.              The Tree ADT A tree is a collection of nodes, one of which is identified as the root. Each non-root node is connected to some other node (perhaps the root) by an edge, making the non-root node the child of the other node, the parent. CS 340 Page 61

  3.                            Linked List Implementation of the Tree ADT structtreeNode { Etype element; treeNode *firstChild; treeNode *nextSibling; } CS 340 Page 62

  4. Text Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Section 2.1 Section 2.2 Section 2.3 Section 4.1 Section 4.2 Section 4.3 Section 3.1 Section 3.2 Subsection 3.1.a Subsection 3.1.b Subsection 3.1.c Tree Traversals To examine the contents of a tree, a traversal strategy must be selected. There are three principal options: Option 1: Preorder Traversal First examine the current node, then examine its offspring. Preorder Traversal: Text Chapter 1 Chapter 2 Section 2.1 Section 2.2 Section 2.3 Chapter 3 Section 3.1 Subsection 3.1.a Subsection 3.1.b Subsection 3.1.c Section 3.2 Chapter 4 Section 4.1 Section 4.2 Section 4.3 Chapter 5 CS 340 Page 63

  5. Postorder Traversal: Flight $150 Cab $ 25 Travel $175 Dinner $ 20 Food $ 20 Hotel $110 Lodging $110 DAY ONE: $305 Rental $ 45 Travel $ 45 Breakfast $ 10 Lunch $ 15 Dinner $ 35 Food $ 60 Hotel $110 Lodging $110 DAY TWO: $215 Rental $ 45 Flight $175 Travel $220 Breakfast $ 10 Lunch $ 15 Food $ 25 Hotel $ 45 Lodging $ 45 DAY THREE: $280 TOTAL $800 Total: $800 Day One: $305 Day Two: $215 Day Three: $280 Travel: $220 Food: $25 Lodging: $35 Travel: $175 Food: $20 Lodging: $110 Travel: $45 Food: $60 Lodging: $110 Rental: $45 Hotel: $35 Dinner: $20 Hotel: $110 Rental: $45 Hotel: $110 Flight: $175 Breakfast: $10 Breakfast: $10 Lunch: $15 Flight: $150 Cab: $25 Lunch: $15 Dinner: $35 Option 2: Postorder Traversal First examine the offspring, then examine the current node. CS 340 Page 64

  6. 57 31 64 18 40 61 89 11 25 48 76 95 23 29 92 93 Option 3: Inorder Traversal (restricted to binary trees) Examine the left subtree, then the root, and then the right subtree. Inorder Traversal: 11 18 23 25 29 31 40 48 57 61 64 76 89 92 93 95 CS 340 Page 65

  7.                            Binary Trees A binary tree is a tree in which no node has more than two children. Implementation: The limitation of at most two children per node makes it possible to implement each node with direct pointers to its children. structtreeNode { Etype element; treeNode *left; treeNode *right; } CS 340 Page 66

  8. * +  + 17 * 5 13 / 2 + 20 4 5 6 Application: Expression Trees By placing the operands in the leaf nodes and the binary (and unary) operators in the non-leaf nodes, we can conveniently store and evaluate arithmetic expressions. Inorder traversal (modified to parenthesize each non-trivial subtree): ((-5)*(13+(20/4)))-(17+(2*(5+6))) Note that a postorder traversal produces a postfix expression, which can be easily evaluated via the stack operations we saw earlier. CS 340 Page 67

  9. 165 165 165 213 104 104 213 213 165 122 165 165 104 213 104 213 104 213 122 173 256 122 256 122 256 240 240 Binary Search Trees A convenient means of searching (or sorting) a list, the binary search tree uses a simple insertion policy: • Starting at the root, if the new element is smaller than the current node’s value, go left; otherwise, go right. • Insert the new element when a NULL pointer is reached. EXAMPLE: 165 213 104 122 256 240 173 165 CS 340 Page 68

  10. 15 Remove 20 10 35 15 15 15 0 25 50 5 30 45 60 10 10 35 35 10 35 40 55 65 0 25 50 0 25 50 0 25 50 5 20 30 45 60 5 20 30 45 60 5 20 30 45 60 15 Remove 10 40 55 40 40 55 55 65 65 65 0 35 5 25 50 20 30 45 60 40 55 65 15 Remove 35 10 40 0 25 50 5 20 30 45 60 55 65 Removal From A Binary Search Tree Removing a node with no children: Set parent’s appropriate pointer to NULL. Removing a node with one child: Set parent’s appropriate pointer to node’s child. Removing a node with two children: Replace the node’s value with the smallest value in its right subtree, and then recursively remove that value from the right subtree. CS 340 Page 69

  11. 0 28 56 84 112 140 168 196  1  29 57 85 113 141 169 197  2 30 58 86 114 142 170 198  3 31 59 87 115 143 171 199  4 32 60 88 116 144 172 200  5 33 61 89 117 145 173 201  6 34 62 90 118 146 174 202 7 35 63 91 119 147 175 203 8 36 64 92 120 148 176 204  9 37 65 93 121 149 177  205 10 38 66 94 122 150 178 206  11  39 67 95 123 151 179 207 12  40 68 96 124 152 180 208 13 41 69 97 125 153 181 209  14 42 70 98 126 154 182 210 15 43 71  99 127 155 183 211 16 44 72  100 128 156 184 212 17 45 73 101 129 157 185 213 18 46 74  102 130 158 186 214  19 47 75 103 131 159 187 215  20 48 76 104 132 160 188 216 21  49 77 105 133 161 189 217 22  50 78 106 134 162 190 218  23 51 79 107 135 163 191 219  24 52 80 108 136 164 192 220 25 53 81 109 137 165 193 221 26 54  82 110 138 166 194 222 27 55 83 111 139 167 195 223                Array Implementation Of Binary Tree          • Place root in slot #0 • Place left child of slot k's node in slot #(2*k+1) • Place right child of slot k's node in slot #(2*k+2) • Locate parent of slot k's node in slot #((k-1)/2) CS 340 Page 70

  12. 1000 875 650 325 700 575 250 275 100 425 150 350 400 125 75 165 235 An Application Using The Array Implementation A (maximum) heap structure makes sure that the data in each node is greater than or equal to the data in both of its subtrees. It is used to ensure that the largest elements are always most accessible (i.e., nearest to the root). CS 340 Page 71

  13. Balanced 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). In order to maintain logarithmic access times for insertions, removals, and searches, a mechanism is needed for ensuring that the tree remains balanced. One approach to maintaining balance in a tree is the concept of self-balancing trees, such as the AVL trees that we shall examine next. These structures require a rebalancing after every update operation. Self-adjusting trees use an amortized approach (i.e., maintaining an average worst-case running time that is logarithmic for each operation). We shall examine splay trees and B-trees as examples of this type of structure. CS 340 Page 72

  14. k2 k1 k1 k2 Z X X Y Y Z AVL Trees 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. CS 340 Page 73

  15. 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 CS 340 Page 74

  16. 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 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) 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) After a new insertion, if a single rotation fails to restore balance, a double rotation may be tried. CS 340 Page 75

  17. 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 STILL UNBALANCED BALANCED! CS 340 Page 76

  18. Balancing Via Amortization Rather than guaranteeing O(logn) time for every access within a binary search tree, we might try obtaining an amortized running time of O(logn) (i.e., m consecutive operations will take a total time of O(mlogn)). As a simple example of amortization, recall that a dynamic array’s size doubles every time it gets filled. This results in rare cases of linear (O(n)) time complexity, but an amortized time complexity of O(1). insert insert insert insert insert O(1) O(1) O(n) O(1) O(1) CS 340 Page 77

  19. Single Rotation Zig-Zag g p x x p g p x A p x C A D A B C D B C A B B C Zig-Zig g x p p D A x g C B B A C D Splay Trees Splay trees accomplish amortized balance by adjusting the tree’s balance with every access, via an AVL-type single rotation, an AVL-type double rotation (called a zig-zag), or a new type of double rotation (called a zig-zig). CS 340 Page 78

  20. 8 8 2 accessed (with a zig-zig) 7 9 7 9 6 10 6 10 5 11 5 11 4 12 2 12 3 13 1 2 3 13 2 14 4 14 1 7 1 15 15 5 8 8 (and a second zig-zig) (and a third zig-zig) 3 6 9 7 9 4 10 2 10 11 11 1 5 12 12 3 6 13 13 4 14 14 15 15 Splay Tree Example CS 340 Page 79

  21. 2 1 11 accessed (with a zig-zig) 7 (and a second zig-zig) 5 8 3 6 11 4 10 12 9 13 2 14 1 7 2 15 5 8 1 11 11 3 6 9 8 12 2 12 4 10 (and a single rotation) 7 10 13 1 8 13 11 5 9 14 7 14 10 12 3 6 15 5 15 9 13 4 3 6 14 4 15 Splay Tree Example (Continued) CS 340 Page 80

  22. 25:-- 25:-- 10,20 25,30 10,20 25,30,45 Insert 10,20,30 Insert 25 Insert 45 25:35 25:35 10,20 25,30 35,45 10,15,20 25,30 35,40,45 Insert 35 Insert 15,40 B-Trees A B-tree of order m is a tree with the following properties: • The root is either a leaf or has between 2 and m children. • All non-leaf nodes (except the root) have between m/2 and m children. • All leaf nodes have the same depth. Example: A 2-3 Tree 10,20,30 CS 340 Page 81

  23. Insert 5 Insert 7,8 25:-- 15:-- 35:-- 25:-- 5,10 15,20 25,30 12:25 35,40,45 Insert 12,13 8:15 35:-- 8:-- 15:-- 35:-- 5,7 8,10 15,20 25,30 35,40,45 5,7 8,10 12,13 15,20 25,30 35,40,45 25:-- 25:35 12:25 Insert 36,38 Insert 39 12:-- 38:-- 10,15,20 25,30 35,40,45 8:-- 15:-- 35:40 8:-- 15:-- 35:-- 40:-- 12,13 15,20 12,13 15,20 38,39 40,45 5,7 8,10 25,30 35,36,38 40,45 5,7 8,10 25,30 35,36 2-3 Tree Example (continued) CS 340 Page 82

  24. STL Search Alternatives: set & map The C++ Standard Template Library provides two built-in implementations of searchable container ADTs. The map class contains pairs of keys and values. The keys are unique and ordered. The set class contains objects in an ordered fashion without duplicates. Both classes are usually implemented within C++ as balanced binary trees. CS 340 Page 83

  25. A set Application: Finding Identifiers #include <set> #include <string> #include <iostream> #include <fstream> using namespace std; const int MAX_FILENAME_LENGTH = 30; const char ALPHANUM[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_"; const char ALPHA[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_"; void customizeWord(string &word, string &remainder); // The main function creates and outputs a set of // identifiers used in the user-specified program file. void main() { set<string> keywordSet, identifierSet; ifstream keywordFile, progFile; char progFileName[MAX_FILENAME_LENGTH]; string keyword, currentWord, leftoverWord; keywordFile.open("keywords.txt"); keywordFile >> keyword; while (!keywordFile.eof()) { keywordSet.insert( keyword ); keywordFile >> keyword; } keywordFile.close(); CS 340 Page 84

  26. cout << "Enter the name of the program file to be analyzed: "; cin >> progFileName; progFile.open(progFileName); getline(progFile, currentWord); while (!progFile.eof()) { customizeWord(currentWord, leftoverWord); if (currentWord != "") { set<string>::iterator keywordItr = keywordSet.find(currentWord); if (keywordItr == keywordSet.end()) identifierSet.insert( currentWord ); } if (leftoverWord == "") getline(progFile, currentWord); else currentWord = leftoverWord; } progFile.close(); cout << endl << "IDENTIFIERS FOR " << progFileName << ":" << endl; for (int i = 1; i <= 17 + strlen(progFileName); i++) cout << '-'; cout << endl; for (set<string>::iterator itr = identifierSet.begin(); itr != identifierSet.end(); itr++) cout << *itr << endl; cout << endl; } CS 340 Page 85

  27. // The customizeWord function splits the "word" parameter into a // single word, and whatever's left over, the "remainder". void customizeWord(string &word, string &remainder) { int endIndex; remainder = ""; // Start by removing initial blank spaces and tabs. while ( (word != "") && ( (word.find('\t', 0) == 0) || (word.find(' ', 0) == 0) ) ) word = word.substr(1, word.length() - 1); if (word == "") return; elseif (word[0] == '\"')// Remove double-quoted text. { endIndex = word.find('\"',1); remainder = word.substr(endIndex + 1, word.length() - endIndex - 1); word = ""; } else if (word.find_first_of("\'",0) == 0)// Remove single-quoted text. { endIndex = word.find_first_of("\'",1); remainder = word.substr(endIndex + 1, word.length() - endIndex - 1); word = ""; } else if (word[0] == '#')// Remove preprocessing directive. { remainder = ""; word = ""; } CS 340 Page 86

  28. else if ( (word.length() > 1) &&// Remove single-line comment. (word[0] == '/') && (word[1] == '/') ) { remainder = ""; word = ""; } else// Potential identifiers. { int frontIndex = word.find_first_of(ALPHA, 0); if (frontIndex < 0)// No alphabetics mean no identifiers. { remainder = ""; word = ""; } else if (frontIndex == 0 )// Line starts with potential identifier. { endIndex = word.find_first_not_of(ALPHANUM, 0); if (endIndex > 0) { remainder = word.substr(endIndex, word.length() - endIndex); word = word.substr(0, endIndex); } else remainder = ""; } else// Skip current non-alphbetic character. { remainder = word.substr(1, word.length() - 1); word = ""; } } } CS 340 Page 87

  29. CS 340 Page 88

More Related