1 / 102

Chapter 26 Supplement AVL Trees, Splay Trees, 2-4 Trees, Red - Black Trees

Chapter 26 Supplement AVL Trees, Splay Trees, 2-4 Trees, Red - Black Trees. Topics. AVL Trees Properties and maintenance Splay Trees Properties and maintenance 2-4 Trees Properties and maintenance Red-Black Trees Properties and equivalence to 2-4 Trees. 6. v. 8. 3. z. 4.

keegan-noel
Download Presentation

Chapter 26 Supplement AVL Trees, Splay Trees, 2-4 Trees, Red - Black 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 26 SupplementAVL Trees, Splay Trees, 2-4 Trees, Red-Black Trees

  2. Topics • AVL Trees • Properties and maintenance • Splay Trees • Properties and maintenance • 2-4 Trees • Properties and maintenance • Red-Black Trees • Properties and equivalence to 2-4 Trees

  3. 6 v 8 3 z 4 AVL Trees

  4. Tree Terminology • Height of a tree • Maximum depth of any node • Internal node • Node with at least one child 2 6 1 1 8 3

  5. AVL Trees • Goal: Achieve logarithmic time for all fundamental dictionary operations • Dictionary is a map that allows for multiple entries to have the same key (like an English dictionary) • Add a rule to the binary search tree definition called the height-balance property • Characterizes the structure of a binary tree in terms of the heights of its internal nodes • The height of a node v is the length of the longest path from v to an external node

  6. Height-Balance Property • For every internal node v of T, the heights of the children of v can differ by at most 1

  7. AVL Tree Definition • Any binary search tree that satisfies the height-balance property is said to be an AVL tree (keeps the height small) An example of an AVL tree where the heights are shown next to the nodes

  8. Height of an AVL Tree (1) • Fact: The heightof an AVL tree storing n keys is O(log n)

  9. Insertion in an AVL Tree • Insertion starts like it does as in a binary search tree • Height-balance rule may be violated • The tree may be have to be restructured

  10. Balanced Nodes • An internal node v is balancedif the absolute value of the difference between the heights of the children of v is at most 1, otherwise it is unbalanced • Every internal node of an AVLtree is balanced

  11. Starting the Trinode Restructuring • Let z be the first node encountered going up from w (new node) toward the root that is unbalanced • Let y be the child of z with the greater height • y is an ancestor of w • Let x be the child of y with the greater height • x is an ancestor of w • x is a grandchild of z and could be w

  12. 44 17 78 44 32 50 88 17 78 48 62 32 50 88 54 48 62 Insertion in an AVL Tree • Insert 54 (before restructured) c=z a=y b=x w before insertion after insertion Nodes 44 and 78 are unbalanced

  13. Restructuring an AVL Tree (1) • Input: A node, x, of a search binary tree that has both a parent, y, and a grandparent, z • Output: Tree T after a trinode restructuring (single or double rotation) • Let (a, b, c) be a left to right (inorder) listing of nodes x, y, and z and let (T0, T1, T2, T3) be the left to right listing (inorder) of the four subtrees of x, y, and z not rooted at x, y, or z

  14. Restructuring an AVL Tree (2) • Replace the subtree rooted at z with a new subtree rooted at b • Let a be the left child of b and let T0 and T1 be the left and right subtrees of a, respectfully • Let c be the right child of b and let T2 and T3 be the left and right subtrees of c, respectfully

  15. single rotation c = z b = y b = y a = x c = z a = x T T 3 0 T T T T T 0 2 1 2 3 T 1 Restructuring: Single Rotations • Single Rotations: a = z b = y single rotation b = y a = z c = x c = x T T 0 3 T T T T T 1 3 0 1 2 T 2

  16. double rotation a = z b = x c = y a = z c = y b = x T T 0 2 T T T T T 3 0 1 3 2 T 1 double rotation c = z b = x a = y a = y c = z b = x T T 3 1 T T T T T 0 0 2 3 1 T 2 Restructuring: Double Rotations • double rotations:

  17. T T 1 1 Insertion Example 44 z=c y=a 17 78 32 50 88 x=b 48 62 T 54 3 T T 2 0 T 1 44 b c 17 unbalanced... a 62 32 78 50 ...balanced 88 54 48 T 2 T T 3 0

  18. 44 17 78 32 50 88 48 62 Another Insertion Example • Insert 49 (before restructured) 44 c=z 17 78 b=y T3 32 50 88 48 62 a=x T0 49 T2 w before insertion T1 after insertion Nodes 44 and 78 are unbalanced

  19. Insertion in an AVL Tree • Insert 49 (after restructured) 44 b=y 17 50 a=x c=z 32 48 78 62 88 49 w after insertion

  20. Removal in an AVL Tree • Remove the node and elevate one of its children into its place • The operation may cause the height-balance property to be violated

  21. 44 17 62 32 50 78 88 48 54 Example of Removing a node from an AVL Tree 44 17 62 50 78 W 88 48 54 before deletion of 32 after deletion Before restructuring

  22. Rebalancing after Removal • Let z be the first unbalanced node encountered while traveling up the tree from w (node removed) • Let y be the child of z with the greater height • Let x be the child of y with the greater height or • If both children have the same height then let x be the child of y on the same side as y (e.g. if y is a left child then x is a left child) • We perform restructure(x) to restore balance at z • As this restructuring may upset the balance of another node higher in the tree • One must continue checking for balance until the root of T is reached

  23. Rebalancing after Removal 62 44 a=z 44 78 17 62 b=y 17 50 88 50 78 c=x T0 48 54 88 48 54 T0 T3 T1 T2 T1 T3 T2

  24. Running Times for AVL Trees • Find is O(log n) • height of tree is O(log n), no restructures needed • insert is O(log n) • initial find is O(log n) • Restructuring up the tree, maintaining heights is O(log n) • remove is O(log n) • initial find is O(log n) • Restructuring up the tree, maintaining heights is O(log n)

  25. 6 v 8 3 z 4 Splay Trees

  26. Splay Trees • Does not use any explicit rules to enforce balance • Related to a certain move-to-rootoperation (called splaying) after every access in order to keep the search tree balanced • Performed at the bottom most node reached during an insert, deletion, or a search • Guarantees logarithmrunning times

  27. Splaying • One “splays” x by moving x to the root of a tree through a series of restructurings • Operation depends upon the relative positions of x, it parent y, and (if it exists) x’s grandparent z

  28. The node x and its parent y are both left children or both right children One replaces z with x, making y a child of x and z a child of y while maintaining the in-order relationship zig-zig (with Right Children) Z=10 T1 Y=20 X=30 X=30 T2 T3 T4 Y=20 T4 Z=10 T3 T1 T2

  29. zig-zig (with Left Children) Z=30 Y=20 T4 X=10 X=10 T3 Y=20 T1 T2 T1 Z=30 T2 T3 T4

  30. Either x and y is a left child and the other is a right child One replaces z with x, making x have y and z as its children while maintaining the in-order relationship zig-zag X=20 Z=10 Z=10 Y=30 Y=30 T1 T1 T2 T3 T4 T4 X=20 T2 T3

  31. Another zig-zag X=20 Z=30 T4 y=10 z=30 Y=10 T1 T2 T3 T4 T1 X=20 T2 T3

  32. X does not have a grandparent One rotates x over y, making x’s children be the node y and one of x’s former children w while maintaining the in-order relationship zig y=10 T1 x=20 w=30 X=20 T2 T3 2 T4 Y=10 W=30 T1 T2 T3 T4

  33. Another zig Y=30 T4 X=20 X=20 W=10 Y=30 W=10 T3 T1 T2 T3 T4 T1 T2

  34. What Operation to Perform • One performs a zig-zig or a zig-zag when x has a grandparent • One performs a zig when x does not have a grandparent • A splaying step consists of repeating the above restructurings at x until x becomes the root of the tree

  35. Splaying: • “x is aleft-left grandchild” means x is a left child of its parent, which is itself a left child of its parent • p is x’s parent; g is p’s parent start with node x is x a left-left grandchild? is x the root? zig-zig yes stop right-rotate about g, right-rotate about p yes no is x a right-right grandchild? is x a child of the root? zig-zig no left-rotate about g, left-rotate about p yes is x a right-left grandchild? yes zig-zag is x the left child of the root? left-rotate about p, right-rotate about g no yes is x a left-right grandchild? zig zig zig-zag yes right-rotate about p, left-rotate about g right-rotate about the root left-rotate about the root yes

  36. Splaying Example (1) 8 3 10 4 11 6 12 5 7 z y 15 13 x 17 Splaying node 14 Beginning of a zig-zag 14 T4 T1 T2 T3

  37. Splaying Example (2) 8 3 10 4 11 6 12 5 7 x 14 y z zig-zag complete 13 15 17 T2 T1 T3 T4

  38. T2 T4 T3 T1 Splaying Example (3) 8 3 10 4 z 11 6 y 12 5 7 x 14 Beginning of a zig-zig 13 15 17

  39. T2 T4 T3 T1 Splaying Example (4) 8 10 3 x 14 4 15 y 12 6 5 7 13 17 Z 11 zig-zig complete

  40. T2 T3 T4 Splaying Example (5) Z 8 y 10 3 x 14 4 15 12 6 5 7 13 17 11 T1 Beginning of a zig-zig

  41. T2 15 12 T3 13 17 11 T4 Splaying Example (6) x y 14 10 Z 8 3 4 6 5 7 zig-zig complete T1

  42. When to Splay • The following rules dictate when splaying is performed • When searching for a key • If the key is found, one splays at the node where the key is found • If the key is not found, one splays the parent of the external node at which the search terminates unsuccessfully • (The splaying done on the previous slides (at 14) would be performed after searching for 14 or unsuccessfully search for 14.5 • Enhances future searches

  43. 1 When to Splay – Insertion (1) • When inserting a key, one splays the newly created node 1 2 2 1 Inserting 2 initial tree after splaying

  44. When to Splay – Insertion (2) 3 2 2 2 1 3 1 1 Inserting 3 Updated tree after splaying

  45. When to Splay – Deletion (1) • When deleting a key, one splays the parent of the node that is moved to the root (recall the removal for binary search trees) To delete node 8, move the right most node in the left subtree to the root, and splay its parent (in this case, splay 6) 8 8 3 10 3 10 4 4 11 11 6 6 5 7 5 7 removing 8 Initial tree

  46. T3 T2 T1 T4 When to Splay – Deletion (2) 7 7 3 10 3 10 4 4 11 11 6 6 5 5 7 move to the root beginning of splaying

  47. T3 T4 T2 T4 T2 T1 T1 T3 When to Splay – Deletion (3) 7 7 6 10 6 10 4 4 11 11 5 5 3 3 splaying complete Begin another splaying

  48. T2 T1 T3 When to Splay – Deletion (4) 6 7 4 10 3 5 11 T4 splaying complete http://www.cs.armstrong.edu/liang/animation/SplayTreeAnimation.html

  49. (2,4) Trees 9 2 5 7 10 14

  50. Multi-Way Search Tree (1) • Let v be a node of an ordered tree • v is called a d-node if v has d children • Definition of a Multi-Way Search Tree • Each internal node has at least two children • Each node is a d-node where d is  2 • Each internal d-node v with children v1 v2 … vdstores an ordered set of d key-value entries (k1 ,x1),…,(ki,xi), where k1… kd-1

More Related