Red Black Trees - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Red Black Trees

Description:

Deletion from Red Black Tree. Works exactly like deletion from Binary Search Tree (BST) ... Black depth problem. R-B Delete Case 1. Red node deleted ... – PowerPoint PPT presentation

Number of Views:1143
Avg rating:3.0/5.0
Slides: 22
Provided by: homeb
Category:
Tags: black | red | trees

less

Transcript and Presenter's Notes

Title: Red Black Trees


1
Red Black Trees
  • Deletion
  • (Last time Insertion get notes if you dont
    have them)

2
Deletion from Red Black Tree
  • Works exactly like deletion from Binary Search
    Tree (BST)
  • UP TO fixing Red-Black tree
  • Steps
  • Find element (x) to delete
  • Swap element with leftmost right
  • Delete node that now contains element (x)
  • Reattach right child

3
BST delete
51
54
19
88
19
88
71
71
83
54
83
60
55
60
51
51
55
4
R-B Tree Delete
  • Find
  • Swap
  • Moves entry to node with one external node (left)
  • Remove entry
  • Reattach right child

5
R-B Tree Delete
  • What about color?
  • Deleting node can introduce
  • red parent, red child problem
  • Black depth problem

6
R-B Delete Case 1
  • Red node deleted
  • Entry swapped into red node, red node deleted
  • Problems?
  • Can this lead to red parent, red child problem?
  • Can this lead to black depth problem?

7
R-B Delete Case 2
  • Delete black node with red right child
  • Left child was dummy placeholder (black)
  • Problems?
  • Red parent, red child?
  • Black depth?

8
R-B Delete Case 2
  • Could have either problem
  • Solution?
  • Recolor child of deleted node to black

9
R-B Delete Case 3
  • Delete black node with black child
  • Problem
  • Cant recolor to fix black depth
  • Black depth is broken

10
R-B Delete Case 3
  • Black depth is broken need to fix
  • Cant fix with recoloring
  • Color R with fictitious color
  • double black
  • Simply way of tracking where violated
  • black depth
  • How we fix double black problem
  • depends on neighboring nodes

X
V
R
X
R
11
Double Black in 2-4 Tree
  • Like having underflow in 2-4 Tree

X
X
2-4
R
R
12
Recap of Delete
  • 3 Cases
  • Red node deleted
  • No problem
  • Delete black node with red child
  • Color red child black
  • Delete black node with black child
  • Color child Double Black
  • 3 possibilities depending on neighboring nodes
  • Rs sibling is black with at least one red child
  • Rs sibling is black with no red children
  • Rs sibling is red

13
R-B Delete Case 3a
  • Rs sibling is black with at least one red child
  • Goal reintroduce black node in path that
    contains R
  • Equivalent to transferring (rotation) in 2-4 Tree
  • Move parent down, sibling or red child of former
    sibling moves into parents node

14
R-B Delete Case 3a Example
15
R-B Delete Case 3b
X
Think of how this looks as 2-4 Tree What do we
do to the 2-4 Tree?
Y
R
S
Z
In 2-4 we drop parent and propagate underflow
upward
X
Y
R
S
Z
16
R-B Delete Case 3b
X
Think of how this looks as 2-4 Tree What do we
do to the 2-4 Tree?
Y
R
S
Z
In 2-4 we drop parent and propagate underflow
upward BUT X was in red node it was part of a 3
node so underflow goes no further
X
Y
R
S
Z
17
R-B Delete Case 3b Summary
  • When sibling of double black node is black
    without red children
  • Pass double black up tree, like passing underflow
    up in 2-4 tree
  • If double black reaches root, color root single
    black, decreasing black depth by one for entire
    tree

18
R-B Delete Case 3c
  • Rs sibling is red
  • Fix by rotating
  • Rs sibling will be black (in new location!)
  • Rs parent red (in new location!)
  • R still double black
  • Use one of previous cases (3a or 3b)

19
R-B Delete Case 3c Example
20
R-B Delete Example
21
Run-Times
  • Search Trees Find, Insert, Next, Previous
  • BST O(h)
  • 2-4 Tree O(log n)
  • R-B Tree O(log n)
  • All operations are O(h) h varies
Write a Comment
User Comments (0)
About PowerShow.com