Download presentation
Presentation is loading. Please wait.
1
Algorithms CSCI 235, Spring 2019 Lecture 23 Red Black Trees Read: Chapter 13
2
Properties of Red-black trees
Every node is either red or black The root is black Every leaf (NIL) is black If a node is red, both children are black For each node, all paths from that node to the descendent leaves contain the same number of black nodes. 26 Usually omitted when drawing trees 17 41 14 21 30 NIL NIL NIL NIL NIL NIL NIL
3
Red-Black Insertion Insertion in a Red-Black Tree starts the same way as insertion into a regular binary-search-tree. Insert node z at the bottom of the tree by following the branches from root to leaf in the correct order. Then: 1) Color node z red. 2) Call RB-Insert-Fixup(T, z) to regain Red-Black Tree properties.
4
RB-Insert(T, z) pseudocode
... //Insert z into T, just like Binary-Search-Tree z.left = T.nil z.right = T.nil z.color = RED RB-Insert-Fixup(T, z)
5
Fixing up the tree What might need fixing? Property Description True?
1 Every node in the tree is red or black yes 2 The root is black ? 3 Every leaf (T.nil) is black yes 4 If a node is red, both children are black ? 5 All paths from any given node to the yes descendant leaves contain the same number of black nodes. Properties, 1, 3 and 5 still hold. Therefore, we only need to check properties 2 and 4 and fix them if they are violated.
6
Things we know The following are true at the start of RB-Insert-Fixup:
a) Node z is red. b) if z.p is the root, then z.p is black. Therefore, if z.p is red, then (z.p).p exists. c) If property 4 is violated, then it is because z.p is red. d) If property 2 is violated, it is because z is the root of the tree. As we fix up the tree, we keep these facts the same after each iteration of the fixup (i.e. they are loop invariant).
7
Cases to Consider If property 2 is violated, then z is the root. Color z black and we are done. Cases to consider for Property 4: Case 1: z's parent's sibling (i.e. z's uncle) is also red. Case 2: z's uncle is black and z is a right child. Case 3: z's uncle is black and z is a left child.
8
Case 1 Case 1: z's uncle is also red. z C C C A D A D a a B z B b g b
y A D A D a a B z B b g b g a) Color both z.p and uncle of z black. b) Color (z.p).p red (to maintain equal numbers of black nodes on all paths. c) Move z pointer up two levels (to (z.p).p). d) Repeat check for violation of properties 2 or 4.
9
Pseudocode for case 1 while (z.p).color == RED //violation of property 4 if z.p == ((z.p).p).left y = ((z.p).p).right //y is uncle if y.color == RED //case 1 applies (z.p).color = BLACK y.color = BLACK ((z.p).p).color = RED z = (z.p).p //...consider cases 2 and 3 else //else clause is the same except left and right exchanged
10
Case 2 Case 2: z's uncle is black and z is a right child.
Use left rotation to convert to case 3. Case 3: z's uncle is black and z is a left child. C C uncle y uncle y A D B D a B z A z g Case 3 Case 2 b g a b if z == (z.p).right z = z.p Left-Rotate(T, z) //Proceed with case 3
11
Case 3 Case 3: z's uncle is black and z is a left child. C B z B D A C
y z B D A C A z a g b g D Case 3 a b 1) Color z.p black. 2) Color (z.p).p red. 3) Perform right rotation. After fixing case 3, we are done. There are no more violations (Why?)
12
Pseudocode for case 3 (z.p).color = BLACK ((z.p).p).color = RED
Right-Rotate(T, (z.p).p) //End of While loop //After the end of the while loop, set the root color to black: (T.root).color = BLACK //fix any violations of property 2 Running time of RB-Insert-Fixup = ?
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.