1 / 44

Ch4c

Ch4c. AVL trees, Splay trees, tries. 6. 3. 8. 6. 1. 10. 1. 10. 10. 1. 2. 2. 5. 5. 5. 2. 7. 7. 7. 9. 3. 8. 3. 9. 8. 11. 11. Rotations of trees. 9. 6. 11. Rotation to change shape One side gets taller, the other shrinks. Randomized BST.

azana
Download Presentation

Ch4c

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. Ch4c AVL trees, Splay trees, tries

  2. 6 3 8 6 1 10 1 10 10 1 2 2 5 5 5 2 7 7 7 9 3 8 3 9 8 11 11 Rotations of trees 9 6 11 • Rotation to change shape • One side gets taller, the other shrinks

  3. Randomized BST • BST have good performance with random insertions … … can we randomize the tree shape

  4. Collection ADTs

  5. AVL Tree • Named for inventors • Adel’son • Vel’skiii • Landis • Balanced – height close to minimum • Binary Search Tree

  6. AVL Tree • Has properties: • Structure BALANCED binary tree • Order  the value at each node is greater than the value in the node’s left child and less than the value in the node’s right child

  7. AVL operations • Search • like BST • Traverse • like BST • Insert • like BST, but maintain balance • Delete • like BST, but maintain balance

  8. 6 10 3 8 1 11 5 7 Balanced? 9

  9. 6 11 3 8 1 10 12 5 7 AVL Insert • Search for place to put new item • Add leaf node 9 Insert “12” Balanced?

  10. 6 11 3 8 1 10 2 5 7 AVL Insert • Search for place to put new item • Add leaf node 9 Insert “2” Balanced?

  11. 6 11 HRT = 2 HLT = 4 3 8 1 10 5 2 7 AVL Insert Unbalanced point • Search for place to put new item • Add leaf node 9 Insert “2” Balanced? NO! Because it was balanced before the insertion,insert can create ‘unbalance’ by 1

  12. 6 11 HRT = 2 HLT = 4 3 8 1 10 2 5 7 AVL Insert Maintenance Unbalanced point • Detect the imbalance • Remove the imbalance 9

  13. 6 3 8 6 1 10 1 10 10 1 2 2 5 5 5 2 7 7 7 9 3 8 3 9 8 11 11 AVL Insert (rotation) 9 6 11 • Rotation to change shape

  14. HRT = 3 HLT = 3 6 1 10 2 5 7 3 9 8 11 AVL Insert (rotation)

  15. Double Rotation • Some unbalanced conditions are not solved by a single rotation • Solved by double rotation • One to left, then one right • or one to right, then one to left

  16. AVL Deletion • Can cause the same unbalanced property • Use the same rotations to restore balance

  17. 27 27 27 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 INSERT 5

  18. 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) If single rotation fails to restore balance after a new insertion, double rotation may be tried.

  19. 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!

  20. 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).

  21. AVL Tree Summary • Ordered / searchable • Good performance • comes from balanced property • Considered more complex than • 2-3-4 Tree

  22. Collection ADTs

  23. Indexed Search Tree (Trie) • Special case of tree: Also called Radix Tree • Applicable when • Key C can be decomposed into a sequence of subkeysC1, C2, … Cn • Redundancy exists between subkeys • Approach • Store subkey at each node • Path through trie yields full key • Example • Huffman tree C1 C2 C3 C3 C4

  24. Trie: say “Try” or “Tree”? • Aside 1: The name trie comes from its use for retrieval. It is pronounced like "try" by some, like "tree" (the pronunciation of "trie" in "retrieval") by others • Aside 2: Implementations of Tries vary • Aside 3: not the text (in any form)

  25. Tries • Useful for searching strings • String decomposes into sequence of letters • Example • “ART”  “A” “R” “T” • Can be very fast • Less overhead than hashing • May reduce memory • Exploiting redundancy • May require more memory • Explicitly storing substrings A R S E T “ART”

  26. Types of Tries • Standard • Single character per node • Compressed • Eliminating chains of nodes • Compact • Stores indices into original string(s) • Suffix • Stores all suffixes of string

  27. Standard Tries • Approach • Each node (except root) is labeled with a character • Children of node are ordered (alphabetically) • Paths from root to leaves yield all input strings Trie for Morse Code

  28. Standard Trie Example • For strings • { a, an, and, any, at }

  29. Standard Trie Example • For strings • { bear, bell, bid, bull, buy, sell, stock, stop }

  30. Standard Tries • Node structure • Value between 1…m • Reference to m children • Array or linked list • Example Class Node { Letter value; // Letter V = { V1, V2, … Vm } Node child[ m ]; }

  31. Standard Tries • Efficiency • Uses O(n) space • Supports search / insert / delete in O(dm) time • For • n total size of strings indexed by trie • d length of the parameter string • m size of the alphabet

  32. Word Matching Trie • Insert words into trie • Each leaf stores occurrences of word in the text

  33. Compressed Trie • Observation • Internal node v of T is redundant if v has one child and is not the root • Approach • A chain of redundant nodes can be compressed • Replace chain with single node • Include concatenation of labels from chain • Result • Internal nodes have at least 2 children • Some nodes have multiple characters

  34. Compressed Trie

  35. Compact Tries • Compact representation of a compressed trie • Approach • For an array of strings S = S[0], … S[s-1] • Store ranges of indices at each node • Instead of substring • Represent as a triplet of integers (i, j, k) • Such that X = s[i][j..k] • Example: S[0] = “abcd”, (0,1,2) = “bc” • Properties • Uses O(s) space, where s = # of strings in the array • Serves as an auxiliary index structure

  36. Compact Representation

  37. D I P P P D S D P S P S S S Suffix Trie • Compressed trie of all suffixes of text • Example: “IPDPS” • Suffixes • IPDPS • DPS • PDPS • PS • S • Useful for finding pattern in any part of text • Occurrence  prefix of some suffix • Example: find PDP inIPDPS

  38. Suffix Trie • Properties • For • String X with length n • Alphabet of size m • Pattern P with length d • Uses O(n) space • Can be constructed in O(n) time • Find pattern P in X in O(dm) time • Proportional to length of pattern, not text

  39. Suffix Trie Example

  40. Tries and Web Search Engines • Search engine index • Collection of all searchable words • Stored in compressed trie • Each leaf of trie • Associated with a word • List of pages (URLs) containing that word • Called occurrence list • Trie is kept in memory (fast) • Occurrence lists kept in external memory • Ranked by relevance

  41. Computational Biology • DNA • Sequence of 4 different nucleotides (ATCG) • Portions of DNA sequence produce proteins (genes) • Genome • Master DNA sequence for organism • For Human • 46 chromosomes • 3 billion nucleotides

  42. Collection ADTs

  43. Array Implementation Of Binary Tree  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                         • 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)

  44. 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.

More Related