Algorithms and Data Structures Lecture IX - PowerPoint PPT Presentation

About This Presentation
Title:

Algorithms and Data Structures Lecture IX

Description:

Produced using Dieter Pfoser's s as basis ... Lecture IX Simonas altenis Aalborg University simas_at_cs.auc.dk This Lecture Red-Black Trees Properties Rotations ... – PowerPoint PPT presentation

Number of Views:56
Avg rating:3.0/5.0
Slides: 43
Provided by: SimonasS3
Category:

less

Transcript and Presenter's Notes

Title: Algorithms and Data Structures Lecture IX


1
Algorithms and Data StructuresLecture IX
  • Simonas Å altenis
  • Aalborg University
  • simas_at_cs.auc.dk

2
This Lecture
  • Red-Black Trees
  • Properties
  • Rotations
  • Insertion
  • Deletion

3
Balanced Binary Search Trees
  • Problem execution time for dynamic set
    operations is Q(h), which in worst case is Q(n)
  • Solution balanced search trees guarantee small
    height h O(log n)

4
Red/Black Trees
  • A red-black tree is a binary search tree with the
    following properties
  • Nodes (incoming edges) are colored red or black
  • Nil-pointer leaves are black
  • The root is black
  • No two consecutive red edges on any root-leaf
    path
  • Same number of black edges on any root-leaf path
    ( black height of the tree)

5
RB-Tree Properties (1)
  • Some measures
  • n of internal nodes
  • h height
  • bh black height
  • 2bh 1 n
  • bh ³ h/2
  • 2h/2 n 1
  • h/2 lg(n 1)
  • h 2 lg(n 1)

6
RB-Tree Properties (2)
  • Operations in the binary-search tree (search,
    insert, delete, ...) can be accomplished in O(h)
    time
  • The RB-tree is a binary search tree, whose height
    is bound by 2 log(n 1), thus the operations run
    in O(log n)
  • Provided that we can maintain red-black tree
    properties spending no more than O(h) time on
    each insertion or deletion

7
Red-Black Tree ADT
  • RBTree ADT
  • Accessor functions
  • key()int
  • color() red, black
  • parent() RBTree
  • left() RBTree
  • right() RBTree
  • Modification procedures
  • setKey(kint)
  • setColor(cred, black)
  • setParent(TRBTree)
  • setLeft(TRBTree)
  • setRight(TRBTree)

8
NIL Sentinel
  • To simplify algorithms we have a special instance
    of the RBTree ADT nil
  • nil.color() black
  • nil.right() nil.left() root of the tree
  • parent() of the root node nil
  • nil.parent() and nil.key() undefined

9
Rotation
a
g
a
b
b
g
right rotation of B
10
Right Rotation
A
B
A
B
a
g
RightRotate(B) 01 A B.left() Move b 02
B.setLeft(A.right()) 03 A.right().setParent(B)
Move A 04 if B B.parent().left() then
B.parent().setLeft(A) 05 if B
B.parent().right() then B.parent().setRight(A) 06
A.setParent(B.parent()) Move B 07
A.setRight(B) 08 B.setParent(A)
a
b
b
g
11
The Effect of a Rotation
  • Maintains inorder key ordering
  • After right rotation
  • Depth(a) decreases by 1
  • Depth(b) stays the same
  • Depth(g) increases by 1
  • Rotation takes O(1) time

12
Insertion in the RB-Trees
RBInsert(T,n) 01 Insert n into T using the normal
binary search tree insertion procedure 02
n.setLeft(nil) 03 n.setRight(nil) 04
n.setColor(red) 05 RBIsertFixup(n)
13
Insertion, Plain and Simple
  • Let
  • n the new node
  • p n.parent()
  • g p.parent()
  • Case 0 p.color() black
  • No properties of the tree are violated we are
    done!
  • In the following assume
  • p g.left()

14
Insertion Case 1
  • Case 1
  • p.color() red and g.right().color() red
  • (parent and its sibling are red)
  • a tree rooted at g is balanced enough!

01 p.setColor(black) 02 g.right().setColor(black)
03 g.setColor(red) 04 n g // and update p and g
15
Insertion Case 1 (2)
  • We call this a promotion
  • Note how the black depth remains unchanged for
    all of the descendants of g

16
Insertion Case 3
  • Case 3
  • p.color() red and g.right().color() black
  • n p.left()

01 p.setColor(black) 02 g.setColor(red) 03
RightRotate(g) 04 // we are done!
17
Insertion Case 3 (2)
  • We do a right rotation and two re-colorings
  • Tree becomes more balanced
  • Black depths remain unchanged
  • No further work on the tree is necessary!

18
Insertion Case 2
  • Case 2
  • p.color() red and g.right().color() black
  • n p.right()

01 LeftRotate(p) 02 exchange n and p pointers and
do as in case 3!
19
Insertion Mirror cases
  • All three cases are handled analogously if p is a
    right child
  • For example, case 2 and 3 right-left double
    rotation

20
Insertion Pseudo Code
RBInsertFixup(n) 01 p n.parent() 02 g
p.parent() 03 while p.color() red do 04 if p
g.left() then 05 if g.right().color()
red 06 then p.setColor(black) 07
g.right().setColor(black) 08
g.setColor(red) 09 n g // and
update p and g, as in 01,02 10 else if n
p.right() 11 then LeftRotate(p) 12
exchange n p 13
p.setColor(black) 14
g.setColor(red) 15 RightRotate(g) 16
else (same as then clause with right and left
exchanged) 17 nil.left().setColor(black) // set
the color of root to black
Case 1
Case 2
Case 3
21
Insertion Summary
  • If two red edges are present, we do either
  • a restructuring (with a simple or double
    rotation) and stop (cases 2 and 3), or
  • a promotion and continue (case 1)
  • A restructuring takes constant time and is
    performed at most once. It reorganizes an
    off-balanced section of the tree
  • Promotions may continue up the tree and are
    executed O(log n) times (height of the tree)
  • The running time of an insertion is O(log n)

22
An Insertion Example
  • Inserting "REDSOX" into an empty tree
  • Now, let us insert "CUBS"

23
Example C
24
Example U
25
Example U (2)
26
Example B
What now?
27
Example B (2)
Right Rotation on D
28
Example S
two red edges and red uncle X- promotion
29
Example S (2)
30
Deletion
  • As with binary search trees, we can always delete
    a node that has at least one nil child
  • If the key to be deleted is stored at a node that
    has no external children, we move there the key
    of its in-order successor, and delete that node
    instead

31
Deletion Algorithm
  • 1. Remove v
  • 2. If v.color() red, we are done! Else, assume
    that u (vs non-nil child or nil) gets additional
    black color
  • If u.color() red then u.setColor(black) and we
    are done!
  • Else u s color is double black.

32
Deletion Algorithm (2)
  • How to eliminate double black edges?
  • The intuitive idea is to perform a "color
    compensation"
  • Find a red edge nearby, and change the pair (red,
    double black) into (black, black)
  • We have two cases
  • restructuring, and
  • recoloring
  • Restructuring resolves the problem locally, while
    recoloring may propagate it two levels up
  • Slightly more complicated than insertion

33
Deletion Case 2
  • If sibling and its children are black, perform a
    recoloring
  • If parent becomes double black, continue upward

34
Deletion Cases 3 and 4
  • If sibling is black and one of its children is
    red, perform a restructuring

35
Deletion Case 1
  • If sibling is red, perform a rotation to get into
    one of cases 2, 3, and 4
  • If the next case is 2 (recoloring), there is no
    propagation upward (parent is now red)

36
A Deletion Example
  • Delete 9

37
A Deletion Example (2)
  • Case 2 (sibling is black with black children)
    recoloring

38
A Deletion Example (3)
  • Delete 8 no double black

39
A Deletion Example (4)
  • Delete 7 restructuring

40
How long does it take?
  • Deletion in a RB-tree takes O(log n)
  • Maximum three rotations and O(log n) recolorings

41
Other balanced trees
  • Red-Black trees are related to 2-3-4 trees
    (non-binary trees)
  • AVL-trees have simpler algorithms, but may
    perform a lot of rotations

42
Next Week
  • Solving optimization problems using Dynamic
    Programming
  • Improved version of divide-and-conquer
Write a Comment
User Comments (0)
About PowerShow.com