1 Red-Black Trees. 2 Black-Height of the tree = 4.

Slides:



Advertisements
Similar presentations
Chapter 13. Red-Black Trees
Advertisements

Definitions and Bottom-Up Insertion
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture.
Red-Black Trees CIS 606 Spring Red-black trees A variation of binary search trees. Balanced: height is O(lg n), where n is the number of nodes.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Lecture 12: Balanced Binary Search Trees Shang-Hua Teng.
1 /26 Red-black tree properties Every node in a red-black tree is either black or red Every null leaf is black No path from a leaf to a root can have two.
1 /26 Red-black tree properties Every node in a red-black tree is either black or red Every null leaf is black No path from a leaf to a root can have two.
1 Red-Black Trees. 2 Black-Height of the tree = 4.
1 /26 Red-black tree properties Every node in a red-black tree is either black or red Every null leaf is black No path from a leaf to a root can have two.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 9 Balanced trees Motivation Red-black trees –Definition, Height –Rotations, Insert,
David Luebke 1 7/2/2015 ITCS 6114 Red-Black Trees.
Balanced Trees Balanced trees have height O(lg n).
1 Red-Black Trees. 2 Definition: A red-black tree is a binary search tree where: –Every node is either red or black. –Each NULL pointer is considered.
Red-Black Trees Lecture 10 Nawazish Naveed. Red-Black Trees (Intro) BSTs perform dynamic set operations such as SEARCH, INSERT, DELETE etc in O(h) time.
Analysis of Red-Black Tree Because of the rules of the Red-Black tree, its height is at most 2log(N + 1). Meaning that it is a balanced tree Time Analysis:
10/20/2015 2:03 PMRed-Black Trees v z. 10/20/2015 2:03 PMRed-Black Trees2 Outline and Reading From (2,4) trees to red-black trees (§9.5) Red-black.
Red-Black Trees Red-black trees: –Binary search trees augmented with node color –Operations designed to guarantee that the height h = O(lg n)
Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees.
© 2004 Goodrich, Tamassia Red-Black Trees v z.
Red Black Tree Smt Genap Outline Red-Black Trees ◦ Motivation ◦ Definition ◦ Operation Smt Genap
CS-2851 Dr. Mark L. Hornick 1 Okasaki’s Insertion Method for Red/Black balancing A step-by-step procedure for maintaining balance through application of.
CSIT 402 Data Structures II
Balanced Search Trees 3.4 – 3.7 Team names Majed Suhaim Ahmed Sulaiman M Alharbi.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 8.
Lecture 10 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Lecture 2 Red-Black Trees. 8/3/2007 UMBC CSMC 341 Red-Black-Trees-1 2 Red-Black Trees Definition: A red-black tree is a binary search tree in which: 
Advanced Data Structures and Implementation Top-Down Splay Trees Top-Down Splay Trees Red-Black Trees Red-Black Trees Top-Down Red Black Trees Top-Down.
Red Black Trees Top-Down Insertion. Review of Bottom-Up Insertion In B-Up insertion, “ordinary” BST insertion was used, followed by correction of the.
Red–black trees.  Define the red-black tree properties  Describe and implement rotations  Implement red-black tree insertion  We will skip red-black.
October 19, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL7.1 Introduction to Algorithms LECTURE 8 Balanced Search Trees ‧ Binary.
Red-Black Trees Definitions and Bottom-Up Insertion.
Red Black Trees Top-Down Deletion. Recall the rules for BST deletion 1.If vertex to be deleted is a leaf, just delete it. 2.If vertex to be deleted has.
Red-Black Tree Insertion Start with binary search insertion, coloring the new node red NIL l Insert 18 NIL l NIL l 1315 NIL l
1 Binary Search Trees  Average case and worst case Big O for –insertion –deletion –access  Balance is important. Unbalanced trees give worse than log.
Red-Black Trees Bottom-Up Deletion. Recall “ordinary” BST Delete 1.If vertex to be deleted is a leaf, just delete it. 2.If vertex to be deleted has just.
Red-Black Trees an alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A.
CSC317 1 x y γ β α x y γ β x β What did we leave untouched? α y x β.
1 Red-Black Trees. 2 A Red-Black Tree with NULLs shown Black-Height of the tree = 4.
Lecture 23 Red Black Tree Chapter 10 of textbook
Definitions and Bottom-Up Insertion
File Organization and Processing Week 3
Red-Black Trees v z Red-Black Trees Red-Black Trees
CS 332: Algorithms Red-Black Trees David Luebke /20/2018.
Red Black Trees
Red-Black Trees.
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
Summary of General Binary search tree
Red-Black Trees Bottom-Up Deletion.
Red-Black Trees Bottom-Up Deletion.
Red-Black Trees Motivations
Red-Black Trees Bottom-Up Deletion.
TCSS 342, Winter 2006 Lecture Notes
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
Red-Black Trees v z /20/2018 7:59 AM Red-Black Trees
Red-Black Trees v z Red-Black Trees Red-Black Trees
Lecture 9 Algorithm Analysis
Red-Black Trees Bottom-Up Deletion.
Lecture 9 Algorithm Analysis
Lecture 9 Algorithm Analysis
Red-Black Trees.
Red Black Trees Top-Down Deletion.
Algorithms and Data Structures Lecture VIII
Red-Black Trees v z /17/2019 4:20 PM Red-Black Trees
Red-Black Trees Bottom-Up Deletion.
Red-Black Trees.
Red-Black Trees Bottom-Up Deletion.
Red-black tree properties
Red Black Trees Top-Down Deletion.
Red-Black Trees v z /6/ :10 PM Red-Black Trees
Presentation transcript:

1 Red-Black Trees

2 Black-Height of the tree = 4

3 Red-Black Trees Definition: A red-black tree is a binary search tree where: –Every node is either red or black. –Each NULL pointer is considered to be a black node –If a node is red, then both of its children are black. –Every path from a node to a leaf contains the same number of black nodes. Definition: The black-height of a node, n, in a red-black tree is the number of black nodes on any path to a leaf, not counting nulls.

4 A valid Red-Black Tree Black-Height = 2

5 Theorem 1 – Any red-black tree with root x, has at least n = 2 bh(x) – 1 internal nodes, where bh(x) is the black height of node x. Proof: by induction on height of x.

6 Theorem 2 – In a red-black tree, at least half the nodes on any path from the root to a leaf must be black. Proof – If there is a red node on the path, there must be a corresponding black node.

7 Theorem 3 – In a red-black tree, no path from any node, N, to a leaf is more than twice as long as any other path from N to any other leaf. Proof: By definition, every path from a node to any leaf contains the same number of black nodes. By Theorem 2, a least ½ the nodes on any such path are black. Therefore, there can no more than twice as many nodes on any path from N to a leaf as on any other path. Therefore the length of every path is no more than twice as long as any other path

8 Theorem 4 – A red-black tree with n internal nodes has height h <= 2 lg(n + 1). Proof: Let h be the height of the red-black tree with root x. By Theorem 2, bh(x) >= h/2 From Theorem 1, n >= 2 bh(x) - 1 Therefore n >= 2 h/2 – 1 n + 1 >= 2 h/2 lg(n + 1) >= h/2 2lg(n + 1) >= h

9 Bottom –Up Insertion Insert node as usual in BST Color the Node RED What Red-Black property may be violated? –Every node is Red or Black –Leaf nodes are Black NULLS –If node is Red, both children must be Black –Every path from node to descendant leaf must contain the same number of Blacks

10 Bottom Up Insertion Insert node; Color it RED; X is pointer to it Cases 0: X is the root -- color it black 1: Both parent and uncle are red -- color parent and uncle black, color grandparent red, point X to grandparent, check new situation (recoloring may have created a problem) 2 (zig-zag): Parent is red, but uncle is black. X and its parent are opposite type children -- color grandparent red, color X black, rotate left on parent, rotate right on grandparent 3 (zig-zig): Parent is red, but uncle is black. X and its parent are both left or both right children -- color parent black, color grandparent red, rotate right on grandparent

11 X P G U P G U Case 1 – U is Red Just Recolor see if there is a new problem at G X

12 X P G U S X P G S U Case 2 – Zig-Zag (X and P are opposite children) Double Rotate X around P; X around G Recolor G and X as can’t rotate a black from a common path without compensating Note, that if both kids of G were red, we would have recolored. If both kids were black, there would be no problem.

13 X P G U S P X G S U Case 3 – Zig-Zig (X and P are both left kids) Single Rotate P around G Recolor P and G as we’ve rotated a black from common path

Black node Red node Insert 4 into this R-B Tree

15 Insertion Practice Insert the values 2, 1, 4, 5, 9, 3, 6, 7 into an initially empty Red-Black Tree

16 Asymptotic Cost of Insertion O(lg n) to descend to insertion point O(1) to do insertion O(lg n) to ascend and readjust == worst case only for case 1 Total: O(log n)

17 Red-Black Trees Bottom-Up Deletion

18 Recall “ordinary” BST Delete 1.If vertex to be deleted is a leaf, just delete it. 2.If vertex to be deleted has just one child, replace it with that child 3.If vertex to be deleted has two children, replace the value of by it’s in-order predecessor’s value then delete the in- order predecessor (a recursive step)

19 Bottom-Up Deletion 1.Do ordinary BST deletion. Eventually a “case 1” or “case 2“ will be done (leaf or just one child). If deleted node, D, is a leaf, think of deletion as replacing with the NULL pointer, V. If D had one child, V, think of deletion as replacing D with V. 2.What can go wrong??

20 Which RB Property may be violated after deletion? 1.If D is red? Not a problem – no RB properties violated 2.If D is black? If D is not the root, deleting it will change the black-height along some path

21 Fixing the problem Think of V as having an “extra” unit of blackness. This extra blackness must be absorbed into the tree (by a red node), or propagated up to the root and out of the tree. There are four cases – our examples and “rules” assume that V is a left child. There are symmetric cases for V as a right child

22 Terminology The node just deleted was D The node that replaces it is V, which has an extra unit of blackness (it was black and if deleted, will create an imbalance) The parent of V is P The sibling of V is S Black Node Red Node Red or Black and don’t care

23 Bottom-Up Deletion Case 1 V’s sibling, S, is Red –Rotate S around P and recolor S & P NOT a terminal case – One of the other cases will now apply All other cases apply when S is Black

24 Case 1 Diagram P S V+ P S Rotate P V+ S Recolor

25 Bottom-Up Deletion Case 2 V’s sibling, S, is black and has two black children. –Recolor S to be Red (subtract 1 black from each) –P absorbs V’s extra blackness If P is Red, we’re done If P is Black, it now has extra blackness and problem has been propagated up the tree

26 Case 2 diagram P S V+ P+ S V Recolor and absorb Either extra black absorbed by P or P now has extra blackness

27 Bottom-Up Deletion Case 3 S is black S’s RIGHT child is RED (Left child either color) –Rotate S around P –Swap colors of S and P, and color S’s Right child Black This is the terminal case – we’re done

28 Case 3 diagrams P S V+ P S V Rotate P S V Recolor

29 Bottom-Up Deletion Case 4 S is Black, S’s right child is Black and S’s left child is Red –RotateS’s left child around S –Swap color of S and S’s left child –Now in case 3

30 Case 4 Diagrams P S V+ P S Rotate P S V+ Recolor

Perform the following deletions, in the order specified Delete 90, Delete 80, Delete 70

32 Red Black Trees Top-Down Insertion

33 Review of Bottom-Up Insertion In Bottom-Up insertion, “ordinary” BST insertion was used, followed by correction of the tree on the way back up to the root This is most easily done recursively –Insert winds up the recursion on the way down the tree to the insertion point –Fixing the tree occurs as the recursion unwinds

34 Top-Down Insertion Strategy In Top-Down insertion, the corrections are done while traversing down the tree to the insertion point. When the actual insertion is done, no further corrections are needed, so no need to traverse back up the tree. So, Top-Down insertion can be done iteratively which is generally faster

35 Goal of T-D Insertion Insertion is always done as a leaf (as in ordinary BST insertion) Recall from the Bottom-Up discussion that if the uncle of a newly inserted node is black, we restore the RB tree properties by one or two local rotations and recoloring – we do not need to make changes further up the tree

36 Goal (2) Therefore, the goal of top down insertion is to traverse from the root to the insertion point in such a way that RB properties are maintained, and at the insertion point, the uncle is Black. That way we may have to rotate and recolor, but not propagate back up the tree

37 Possible insertion configurations X (Red or Black) Y Z If a new node is inserted as a child of Y or Z, there is no problem since the new node’s parent is black

38 Possible insertion configurations X Y Z If new node is child of Z, no problem since Z is black. If new node is child of Y, no problem since the new node’s uncle (Z) is black – do a few rotations and recolor…. done

39 Possible insertion configurations X Y Z If new node is inserted as child of Y or Z, it’s uncle will be red and we will have to go back up the tree. This is the only case we need to avoid.

40 Top-Down Traversal X Y Z As we traverse down the tree and encounter this case, we recolor and possible do some rotations. There are 3 cases. Remember the goal – to create an insertion point at which the parent of the new node is Black, or the uncle of the new node is black.

41 Case 1 – X’s Parent is Black X Z Y P X Z P Just recolor and continue down the tree Y

42 Case 2 X’s Parent is Red (so Grandparent is Black) and X and P are both left/right children –Rotate P around G –Color P black –Color G red Note that X’s uncle, U, must be black because it (a) was initially black, or (b) would have been made black when we encountered G (which would have had two red children -- X’s Parent and X’s uncle)

43 Case 2 diagrams X Z Y P G U S X Z Y P G US Rotate P around G. Recolor X, Y, Z, P and G

44 Case 3 X’s Parent is Red (so Grandparent is Black) and X and P are opposite children –Rotate P around G –Color P black –Color G red Again note that X’s uncle, U, must be black because it (a) was initially black, or (b) would have been made black when we encountered G (which would have had two red children -- X’s Parent and X’s uncle)

45 Case 3 Diagrams (1 of 2) X ZY P G U S X Y S P G U Z Step 1 – recolor X, Y and Z. Rotate X around P.

46 Case 3 Diagrams (2 of 2) X Y S P G U Z P YS X G UZ Step 2 – Rotate X around G. Recolor X and G

47 An exercise – insert F D T W Z V L J P EK

48 Top-Down Insert Summary P X YZ Case 1 P is Black Just Recolor P X YZ Case 2 P is Red X & P both left children (or both right children) P X Y Z G P X YZ G Recolor X,Y,Z P X Y Z G Rotate P around G Recolor P,G Case 3 P is Red X and P are opposite children P X YZ G Recolor X,Y,Z Rotate X around P X P Y Z G Rotate X around G Recolor X, G Recolor X,Y,Z X P YZ G