13' RedBlack Tree - PowerPoint PPT Presentation

1 / 32
About This Presentation
Title:

13' RedBlack Tree

Description:

According to property 4, at least half the nodes on any simply path from the ... RB-INSERT take a total of O(lg n) time. ... RB-DELETE is therefore also O(lg n) ... – PowerPoint PPT presentation

Number of Views:69
Avg rating:3.0/5.0
Slides: 33
Provided by: litsen
Category:

less

Transcript and Presenter's Notes

Title: 13' RedBlack Tree


1
13. Red-Black Tree
2
Red-black trees
  • One of many search-tree schemes that are
    balanced in order to guarantee that basic
    dynamic-set operations take O(lg n ) time in the
    worse case.

3
13.1 Properties of red-black tree
  • A red-black tree is a binary search tree with
    one extra bit of storage per node its color,
    which can either RED or BLACK. By constraining
    the way nodes can be colored on any path from the
    root to a leaf, red-black trees ensure that no
    such path is more then twice as long as any
    other, so that three is approximately balanced.

4
  • Each node of the tree now contains the fields
    color, key, left, right, and p. If a child or the
    parent of a node does not exist, the
    corresponding pointer field of the node contains
    the value NIL. We shall regard these NILs as
    being pointers to external nodes(leaves) of the
    binary search tree and the normal, key-bearing
    nodes as being internal nodes of the tree.

5
  • A binary search tree is a red-black tree if it
    satisfies the following red-black properties
  • 1. Every node is either red or 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 to
    descendant leaves contain the same number of
    black nodes.

6
A red-black tree with black nodes and red nodes
shaded.
7
(No Transcript)
8
(No Transcript)
9
Lemma 13.1
  • A red-black tree with n internal nodes has height
    at most 2lg(n1).

10
Proof.
  • We start by showing that the subtree rooted at
    any node x contains at least 2bh(x) 1 internal
    nodes. We prove this claim by induction on the
    height of x. if the eight of x is 0, then x must
    be a leaf(NILT), and the sub tree rooted at x
    indeed contains at least 2bh(x) 1 20 1 0
    internal nodes.

11
  • For the inductive step, consider a node x that
    has positive height and is an internal node with
    two children. Each child has a black-height of
    either bh(x) or bh(x) 1, depending on whether
    its color is red or black, respectively. Since
    the height of a child of x is less than the
    height of x itself, we can apply the inductive
    hypothesis to conclude that each child has at
    least 2bh(x)-1 -1 internal nodes. Thus, the
    subtree rooted at x contains at least (2bh(x)-1
    1)(2bh(x)-1 1) 1 2bh(x) 1 internal nodes,
    which proves the claim.

12
  • To complete the proof of the lemma, let h be the
    height of the tree. According to property 4, 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-height of the root
    must be at least h/2 thus,
  • n ? 2h/2 -1.
  • Moving the 1 to the left-hand side and taking
    logarithms on both sides yields lg(n 1) h/2,
    or h ? 2lg (n1).

13
13.2 Rotations
14
LEFT-ROTATE(T,x)
  • 1 y ? rightx
  • 2 rightx ? lefty
  • 3 plefty ? x
  • 4 py ? px
  • 5 If px nilT
  • 6 then rootT ? y
  • 7 else if x leftpx
  • 8 then leftpx ? y
  • 9 else rightpx ? y
  • 10 lefty ? x
  • 11 px ? y

The code for RIGHT-ROTATE is symmetric. Both
LEFT-ROTATE and RIGHT-ROTATE run in O(1) time.
15
An example of LEFT-ROTATE(T,x)
16
13.3 Insertion
  • RB-INSERT(T,x)
  • 1 y ? nilT
  • 2 x ? rootT
  • 3 while x ? nilT
  • 4 do y ? x
  • 5 if keyz lt keyx
  • 6 then x ? leftx
  • 7 else x ? rightx
  • 8 pz ? y

17
  • 9 if y nilT
  • 10 then rootT ? z
  • 11 else if keyz lt keyy
  • 12 then lefty ? z
  • 13 else righty ? z
  • 14 leftz ? nilT
  • 15 rightz ? nilT
  • 16 colorz ? RED
  • 17 RB-INSERT-FIXUP(T, z)

18
  • RB-INSERT-FIXUP(T,z)
  • 1 while colorpz RED
  • 2 do if pz leftppz
  • 3 then y ? rightppz
  • 4 if colory RED
  • 5 then colorpz ?BLACK Case 1
  • 6 colory ? BLACK Case 1
  • 7 colorppz ? RED Case 1
  • 8 z ? ppz Case 1

19
  • 9 else if z rightpz
  • 10 then z ? ppz Case 2
  • 11 LEFT-ROTATE(T,z) Case 2
  • 12 colorpz ? BLACK Case 3
  • 13 colorppz ? RED Case 3
  • 14 RIGHT-ROTATE(T,ppz) Case 3
  • 15 else (same as then clause
  • with right and left exchanged)
  • 16 colorrootT ? BLACK

20
The operation of RB-INSERT-FIXUP
21
(No Transcript)
22
case 1 of the RB-INSERT
23
case 2 and 3 of RB-INSERT
24
Analysis
  • RB-INSERT take a total of O(lg n) time.
  • It never performs more than two rotations, since
    the while loop terminates if case 2 or case 3
    executed.

25
13.4 Deletion
  • RB-DELETE(T,z)
  • 1 if leftz nilT or rightz nilT
  • 2 then y ? z
  • 3 else y ? TREE-SUCCESSOR(z)
  • 4 if lefty ? nilT
  • 5 then x ? lefty
  • 6 else x ? righty
  • 7 px ? py
  • 8 if py nilT

26
  • 9 then rootT ? x
  • 10 else if y leftpy
  • 11 then leftpy ? x
  • 12 else rightpy ? x
  • 13 if y ? z
  • 14 then keyz ? keyy
  • 15 copy ys satellite data into z
  • 16 if colory BLACK
  • 17 then RB-DELETE-FIXUP(T, x)
  • 18 return y

27
RB-DELETE-FIXUP(T, x)
  • RB-DELETE-FIXUP
  • 1 while x ? rootT and colorx BLACK
  • 2 do if x leftpx
  • 3 then w ? rightpx
  • 4 if colorw RED
  • 5 then colorw ? BLACK Case1
  • 6 colorpx RED Case1
  • 7 LEFT-ROTATE(T,px) Case1
  • 8 w ? rightpx Case1

28
  • 9 if colorrightw BLACK and
  • colorrightw BLACK
  • 10 then colorw ? RED Case2
  • 11 x ? px Case2
  • 12 else if colorrightw BLACK
  • 13 then colorleftw ? BLACK Case3
  • 14 colorw ? RED Case3
  • 15 RIGHT-ROTATE(T,w) Case3
  • 16 w ? rightpx Case3
  • 17 colorw ? colorpx Case4

29
  • 18 colorpx ? BLACK
    Case4
  • 19 colorrightw ? BLACK Case4
  • 20 LEFT-ROTATE(T,px) Case4
  • 21 x ? rootT Case4
  • 22 else (same as then clause with right
    and left exchanged)
  • 23 colorx ? BLACK

30
the case in the while loop of RB-DELETE-FIXUP
31
(No Transcript)
32
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)
Write a Comment
User Comments (0)
About PowerShow.com