13 Red-Black Trees

download 13 Red-Black Trees

of 32

Transcript of 13 Red-Black Trees

  • 7/31/2019 13 Red-Black Trees

    1/32

    13. Red-Black Tree

    Hsu, Lih-Hsing

  • 7/31/2019 13 Red-Black Trees

    2/32

    Chapter 13 P.2

    Computer Theory Lab.

    One of many search-tree schemes thatare balanced in order to guarantee

    that basic dynamic-set operations takeO(lg n ) time in the worse case.

    Red-black trees

  • 7/31/2019 13 Red-Black Trees

    3/32

    Chapter 13 P.3

    Computer Theory Lab.

    13.1 Properties of red-black treeAred-black tree is a binary search treewith one extra bit of storage per node:

    its color, which can either RED or BLACK.By constraining the way nodes can becolored on any path from the root to aleaf, red-black trees ensure that no

    such path is more then twice as long asany other, so that three isapproximately balanced.

  • 7/31/2019 13 Red-Black Trees

    4/32

    Chapter 13 P.4

    Computer Theory Lab.

    Each node of the tree now contains the fieldscolor, key, left, right, and p. If a child or the

    parent of a node does not exist, thecorresponding pointer field of the nodecontains the value NIL. We shall regard theseNILs as being pointers to external

    nodes(leaves) of the binary search tree andthe normal, key-bearing nodes as beinginternal nodes of the tree.

  • 7/31/2019 13 Red-Black Trees

    5/32

    Chapter 13 P.5

    Computer Theory Lab.

    A binary search tree is a red-black tree if itsatisfies the following red-black properties:

    1. Every node is either red or black.2. The root is black.2. The root is black.

    3. Every leaf (NIL) is black

    4. If a node is red, then both its children are black.

    5. For each node, all paths from the node todescendant leaves contain the same number ofblack nodes.

  • 7/31/2019 13 Red-Black Trees

    6/32

    Chapter 13 P.6

    Computer Theory Lab.

    A red-black tree with black nodes

    and red nodes shaded.

  • 7/31/2019 13 Red-Black Trees

    7/32

  • 7/31/2019 13 Red-Black Trees

    8/32Chapter 13 P.8

    Computer Theory Lab.

  • 7/31/2019 13 Red-Black Trees

    9/32Chapter 13 P.9

    Computer Theory Lab.

    Lemma 13.1A red-black tree with n internal nodes

    has height at most 2lg(n+1).

  • 7/31/2019 13 Red-Black Trees

    10/32Chapter 13 P.10

    Computer Theory Lab.

    Proof. We start by showing that the subtree

    rooted at any nodex contains at least

    2bh(x) 1 internal nodes. We prove thisclaim by induction on the height ofx. ifthe eight ofx is 0, thenx must be a

    leaf(NIL[T]), and the sub tree rooted atx indeed contains at least 2bh(x) 1 = 20 1 =0 internal nodes.

  • 7/31/2019 13 Red-Black Trees

    11/32Chapter 13 P.11

    Computer Theory Lab.

    For the inductive step, consider a node x that

    has positive height and is an internal nodewith two children. Each child has a black-height of either bh(x) or bh(x) 1, dependingon whether its color is red or black,

    respectively. Since the height of a child ofx isless than the height ofx itself, we can applythe inductive hypothesis to conclude thateach child has at least 2bh(x)-1 -1 internal nodes.

    Thus, the subtree rooted at x contains atleast (2bh(x)-1 1)+(2bh(x)-1 1) +1 = 2bh(x) 1internal nodes, which proves the claim.

  • 7/31/2019 13 Red-Black Trees

    12/32Chapter 13 P.12

    Computer Theory Lab.

    To complete the proof of the lemma, let h bethe height of the tree. According to property4, at least half the nodes on any simply path

    from the root to a leaf, not including the root,must be black. Consequently, the black-heightof the root must be at least h/2; thus,

    n 2h/2

    -1.Moving the 1 to the left-hand side and takinglogarithms on both sides yields lg(n + 1) h/2,orh 2lg (n+1).

  • 7/31/2019 13 Red-Black Trees

    13/32Chapter 13 P.13

    Computer Theory Lab.

    13.2 Rotations

  • 7/31/2019 13 Red-Black Trees

    14/32

  • 7/31/2019 13 Red-Black Trees

    15/32

    Chapter 13 P.15

    Computer Theory Lab.

    An example of LEFT-ROTATE(T,x)

  • 7/31/2019 13 Red-Black Trees

    16/32

    Chapter 13 P.16

    Computer Theory Lab.

    13.3 InsertionRB-INSERT(T,x)

    1 y nil[T]

    2 x root[T]3 whilex nil[T]

    4 doy x

    5 if key[z] < key[x]

    6 thenx left[x]

    7 else x right[x]

    8 p[z] y

  • 7/31/2019 13 Red-Black Trees

    17/32

    Chapter 13 P.17

    Computer Theory Lab.

    9 ify = nil[T]

    10 thenroot[T] z

    11 else ifkey[z] < key[y]12 thenleft[y] z

    13 else right[y] z

    14 left[z] nil[T]

    15 right[z] nil[T]

    16 color[z] RED

    17 RB-INSERT-FIXUP(T,z)

  • 7/31/2019 13 Red-Black Trees

    18/32

    Chapter 13 P.18

    Computer Theory Lab.

    RB-INSERT-FIXUP(T,z)

    1 whilecolor[p[z]] = RED

    2 do ifp[z] = left[p[p[z]]]3 theny right[p[p[z]]]

    4 if color[y] = RED

    5 thencolor[p[z]] BLACK Case 1

    6 color[y] BLACK Case 1

    7 color[p[p[z]]] RED Case 1

    8 zp[p[z]] Case 1

  • 7/31/2019 13 Red-Black Trees

    19/32

    Chapter 13 P.19

    Computer Theory Lab.

    9 elseifz= right[p[z]]

    10 thenzp[p[z]] Case 2

    11 LEFT-ROTATE(T,z) Case 212 color[p[z]] BLACK Case 3

    13 color[p[p[z]]] RED Case 3

    14 RIGHT-ROTATE(T,p[p[z]]) Case 3

    15 else (same as then clause

    with right and left exchanged)

    16 color[root[T]] BLACK

  • 7/31/2019 13 Red-Black Trees

    20/32

    Chapter 13 P.20

    Computer Theory Lab.

    The operation of RB-INSERT-

    FIXUP

  • 7/31/2019 13 Red-Black Trees

    21/32

    Chapter 13 P.21

    Computer Theory Lab.

  • 7/31/2019 13 Red-Black Trees

    22/32

    Chapter 13 P.22

    Computer Theory Lab.

    case 1 of the RB-INSERT

  • 7/31/2019 13 Red-Black Trees

    23/32

    Chapter 13 P.23

    Computer Theory Lab.

    case 2 and 3 of RB-INSERT

  • 7/31/2019 13 Red-Black Trees

    24/32

    Chapter 13 P.24

    Computer Theory Lab.

    Analysis RB-INSERT take a total ofO(lg n) time.

    It never performs more than two

    rotations, since the while loopterminates if case 2 or case 3 executed.

  • 7/31/2019 13 Red-Black Trees

    25/32

    Chapter 13 P.25

    Computer Theory Lab.

    13.4 Deletion RB-DELETE(T,z)

    1 ifleft[z] = nil[T] orright[z] = nil[T]

    2 theny z3 elsey TREE-SUCCESSOR(z)

    4 ifleft[y] nil[T]

    5 thenx left[y]

    6 elsex right[y]

    7 p[x] p[y]

    8 ifp[y] = nil[T]

  • 7/31/2019 13 Red-Black Trees

    26/32

    Chapter 13 P.26

    Computer Theory Lab.

    9 thenroot[T] x

    10 else ify = left[p[y]]

    11 thenleft[p[y]] x

    12 else right[p[y]] x

    13 ify z

    14 thenkey[z] key[y]

    15 copy ys satellite data into z

    16 if color[y] = BLACK

    17 then RB-DELETE-FIXUP(T,x)

    18 returny

  • 7/31/2019 13 Red-Black Trees

    27/32

    Chapter 13 P.27

    Computer Theory Lab.

    RB-DELETE-FIXUP(T, x) RB-DELETE-FIXUP

    1 whilex root[T] and color[x] = BLACK

    2 do ifx = left[p[x]]3 thenw right[p[x]]

    4 ifcolor[w] = RED

    5 thencolor[w] BLACK Case1

    6 color[p[x]] = RED Case1

    7 LEFT-ROTATE(T,p[x]) Case1

    8 w right[p[x]] Case1

  • 7/31/2019 13 Red-Black Trees

    28/32

    Chapter 13 P.28

    Computer Theory Lab.

    9 ifcolor[right[w]] = BLACKand

    color[right[w]= BLACK

    10 thencolor[w] RED Case2

    11 x p[x] Case2

    12 elseifcolor[right[w]] = BLACK

    13 thencolor[left[w]] BLACKCase3

    14 color[w] RED Case3

    15 RIGHT-ROTATE(T,w) Case3

    16 w right[p[x]] Case3

    17 color[w] color[p[x]] Case4

  • 7/31/2019 13 Red-Black Trees

    29/32

    Chapter 13 P.29

    Computer Theory Lab.

    18 color[p[x]] BLACK Case4

    19 color[right[w]] BLACK Case4

    20 LEFT-ROTATE(T,p[x]) Case421 x root[T] Case4

    22 else (same as then clause with right

    and left exchanged)

    23 color[x] BLACK

  • 7/31/2019 13 Red-Black Trees

    30/32

    Chapter 13 P.30

    Computer Theory Lab.

    the case in the while loop of

    RB-DELETE-FIXUP

    C h b

  • 7/31/2019 13 Red-Black Trees

    31/32

    Chapter 13 P.31

    Computer Theory Lab.

    C t Th L b

  • 7/31/2019 13 Red-Black Trees

    32/32

    Ch t 13 P 32

    Computer Theory Lab.

    Analysis The RB-DELETE-FIXUP takes O(lg n) time

    and performs at most three rotations.

    The overall time for RB-DELETE is

    therefore also O(lg n)