1 / 26

Red-Black Tree

Red-Black Tree. Algorithm : Design & Analysis [12]. In the last class …. Design against an Adversary Selection Problem – Median A Linear Time Selection Algorithm Analysis of Selection Algorithm A Lower Bound for Finding the Median. Red-Black Tree. Dynamic sets Amortized time analysis

Download Presentation

Red-Black Tree

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. Red-Black Tree Algorithm : Design & Analysis [12]

  2. In the last class… • Design against an Adversary • Selection Problem – Median • A Linear Time Selection Algorithm • Analysis of Selection Algorithm • A Lower Bound for Finding the Median

  3. Red-Black Tree • Dynamic sets • Amortized time analysis • Definition of red-black tree • Black height • Insertion into a red-black tree • Deletion from a red-black tree

  4. Dynamic Sets • Dynamic set • Membership varies during computation • Maximum size unknown previously • Space allocation techniques such as array doubling may be needed. • The problem of “unusually expensive” individual operation.

  5. Amortized Time Analysis • Amortized equation: amotized cost = actual cost + accounting cost • Design goals for accounting cost • In any legal sequence of operations, the sum of the accounting costs is nonnegative. • The amortized cost of each operation is fairly regular, in spite of the wide fluctuate possible for the actual cost of individual operations.

  6. Accounting Scheme for Stack Push • Push operation with array doubling • No resize triggered: 1 • Resize(n2n) triggered: tn+1 (t is a constant) • Accounting scheme(specifying accounting cost) • No resize triggered: 2t • Resize(n2n) triggered: -nt+2t • So, the amortized cost of each individual push operation is 1+2t(1)

  7. Node Group in a binTree As in 2-tree, the number of external node is one more than that of internal node Node group 50 70 70 15 80 80 60 60 25 10 40 75 20 65 75 90 30 5 principal subtrees

  8. 80 60 30 50 Binary Search Tree Poor balancing (n) Good balancing (logn) 40 30 60 20 20 80 50 40 In a properly drawn tree, pushing forward to get the ordered list. • Each node has a key, belonging to a linear ordered set • An inorder traversal produces a sorted list of the keys

  9. Improving the Balancing by Rotation The node group to be rotated 50 50 25 Root of the group is changed. 15 15 40 25 10 10 20 30 40 20 30 The middle principal subtree changes parent

  10. Red-Black Tree: the Definition • If T is a binary tree in which each node has a color, red or black, and all external nodes are black, then T is a red-black tree if and only if: • [Color constraint] No red node has a red child • [Black height constrain] The black length of all external paths from a given node u is the same (the black height of u) • The root is black. • Almost-red-black tree(ARB tree) Balancing is under controlled

  11. 40 Red-Black Tree with 6 Nodes 40 60 poorest balancing: height(normal) is 4 20 30 80 30 50 20 60 Black edge 80 40 50 60 20 80 30 50

  12. 40 30 60 40 20 20 50 80 30 40 30 60 60 50 20 50 80 80 Black-Depth Convention All with the same largest black depth: 2 ARB Trees

  13. Recursive Definition of Red-Black Tree (A red-black tree of black height h is denoted as RBh) • Definition: • An external node is an RB0 tree, and the node is black. • A binary tree ia an ARBh (h1) tree if: • Its root is red, and • Its left and right subtree are each an RBh-1 tree. • A binary tree ia an RBh (h1) tree if: • Its root is black, and • Its left and right subtree are each either an RBh-1 tree or an ARBh tree.

  14. Properties of Red-Black Tree • The black height of any RBh tree or ARBh tree is well defind and is h. • Let T be an RBh tree, then: • T has at least 2h-1 internal black nodes. • T has at most 4h-1 internal nodes. • The depth of any black node is at most twice its black depth. • Let A be an ARBh tree, then: • A has at least 2h-2 internal black nodes. • A has at most (4h)/2-1 internal nodes. • The depth of any black node is at most twice its black depth. • Let T be a red-black tree with n internal nodes, the height of T, in the usual sense, is at most 2lg(n+1)

  15. 40 40 60 60 20 20 70 30 50 50 30 80 80 Influences of Insertion into an RB Tree • Black height constrain: • No violation if inserting a red node. • Color constraint: Critical clusters(external nodes excluded), which originated by color violation, with 3 or 4 red nodes Inserting 70

  16. 40 60 40 20 20 30 50 70 30 70 Repairing 4-node Critical Cluster No new critical cluster occurs, inserting finished. 80 Color flip: Root of the critical cluster exchanges color with its subtrees 60 80 50

  17. 40 40 20 20 90 30 30 70 90 Repairing 4-node Critical Cluster 2 more insertions 60 Critical cluster 80 85 50 New critical cluster with 3 nodes. Color flip doesn’t work, Why? 60 80 70 85 50

  18. L L L R M M M M R R LR RR RL RL RL RL LL Patterns of 3-Node Critical Cluster B A LR RR LR RR LL LL Shown as properly drawn D C L R LR RR LL 85

  19. 20 90 30 40 60 80 RL 70 85 50 Repairing 3-Node Critical Cluster All into one pattern Root of the critical cluster is changed to M, and the parentship is adjusted accordingly L M R LR RR LL The incurred critical cluster is of pattern A

  20. Deletion: Logical and Structral u: to be deleted logically  80 40 60 20 70 85 90 30 50 S : tree successor of u, to be deleted structurally, with information moved into u right subtree of S, to replacing S  80 40 70 20 85 90 30 50 After deletion

  21. To be deleted 40 60 80 20 20 70 85 50 30 30 90 90 Deletion in a Red-Black Tree one deletion The black height of  is not well-defined !  40 70 80 85 50 black depth=1 black depth=2

  22. Procedure of Red-Black Deletion 1. Do a standard BST search to locate the node to be logically deleted, call it u 2. If the right child of u ia an external node, identify u as the node to be structurally deleted. 3. If the right child of u is an internal node, find the tree successor of u , call it , copy the key and information from  to u. (color of u not changed) Identify  as the node to be deleted structurally. 4. Carry out the structural deletion and repair any imbalance of black height.

  23. 40 80 50 20 20 90 30 90 90 30 Imbalance of Black Height 60 70 85 deleting 80 deleting 40 50 85 Black height has to be restored 70 deleting 60 80 deleting 85 70 80 90 85

  24. Analysis of Black Imbalance • The imbalance occurs when: • A black node is delete structrally, and • Its right subtree is black(external) • The result is: • An RBh-1 occupies the position of an RBhas required by its parent, coloring it as a “gray” node. • Solution: • Find a red node and turn it red as locally as possible. • The gray color might propagate up the tree.

  25. p g g l s Propagation of Gray Node The pattern for which paropagation is needed p p s s r l r Gray Up In the worst case, up to the root of the tree, and successful Map of the vicinity of g, the gray node g l r p-subtree gets well-defined black height, but that is less than that required by its parent

  26. g c s a g p b a s b c p l l Schemes for Repairing Deletion Rebalance group Restructured 4 principal subtrees, RBh-1 • Restructuring the deletion rebalance group: • Red p: form an RB1 or ARB2tree • Black p: form an RB2tree

More Related