Lecture+19 20+Red+Black+Trees

Embed Size (px)

Citation preview

  • 8/3/2019 Lecture+19 20+Red+Black+Trees

    1/23

    Lecture 15

    Umar Manzoor

  • 8/3/2019 Lecture+19 20+Red+Black+Trees

    2/23

    Chapter 13, Red Black Trees,

    Introduction to Algorithms ByCormen et al

  • 8/3/2019 Lecture+19 20+Red+Black+Trees

    3/23

    Red-Black Trees (Intro)

    BSTs perform dynamic set operations such asSEARCH, INSERT, DELETE etc in O(h) time.

    If the tree height is large, performance may be nobetter than a linked list, for large n.

    RBTs are balanced in order to guarantee O(lg n)worst case time for set dynamic operations

  • 8/3/2019 Lecture+19 20+Red+Black+Trees

    4/23

    Red-Black Trees (Intro) A RBT is a binary search tree where each

    node is colored red or black

    No path from root to leaf is twice as long asany other. (approximately balanced)

    A node contains: color, key, left, right, p

    A NIL is a leaf, all other nodes are internal

    nodes.

  • 8/3/2019 Lecture+19 20+Red+Black+Trees

    5/23

    Red-Black Trees (Properties)

    A binary search tree is a red-black tree if:

    1. Every node is either red or black

    2. The root is black3. Every leaf (NIL) is black

    4. A rednodes children are black

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

  • 8/3/2019 Lecture+19 20+Red+Black+Trees

    6/23

  • 8/3/2019 Lecture+19 20+Red+Black+Trees

    7/23

    Red-Black Trees (Intro)

    We use sentinel nil[ T ]to aid us in dealingwith boundary conditions in pseudo code.

    nil[ T ]object with color field black, otherfields are set to arbitrary values.

  • 8/3/2019 Lecture+19 20+Red+Black+Trees

    8/23

    Black-Height

    Black Height of a node: bh(x)Excluding the node x itself, the number of black

    nodes on any path down to a leaf.

    Black Height of tree = Black Height of root

    node

  • 8/3/2019 Lecture+19 20+Red+Black+Trees

    9/23

    Rotations

    The operations TREE-INSERT and TREE-DELETE modify the tree, they may violate

    the red-black properties.

    To restore these, we

    Change color of nodes

    Change pointer structure (rotations)

    Two kinds, left rotation and right rotation

  • 8/3/2019 Lecture+19 20+Red+Black+Trees

    10/23

    Rotations

    Left-Rotation on a node x:Right child yis not nil[ T ]

    Left rotation pivots around the link from x to y

    i.e., it makes ythe new root of the subtree, withxas ys left child and ys left child asxs rightchild.

  • 8/3/2019 Lecture+19 20+Red+Black+Trees

    11/23

  • 8/3/2019 Lecture+19 20+Red+Black+Trees

    12/23

  • 8/3/2019 Lecture+19 20+Red+Black+Trees

    13/23

  • 8/3/2019 Lecture+19 20+Red+Black+Trees

    14/23

    Inserting Nodes

    A node is inserted into the tree just as inBST.

    When a node is inserted, it is colored redinitially.

    A procedure is called at the end of theinsertion to correct the tree structure

  • 8/3/2019 Lecture+19 20+Red+Black+Trees

    15/23

    Inserting Nodes

    Suppose we are inserting a node zin the tree. The following red-black properties can be

    violated:

    2. The root is black (when the first red node is inserted)4. A red node has black children (when the parent of

    inserted node is red)

    A violation of property 4 leads to three cases: Case 1: zs uncle yis red

    Case 2: zs uncle yis black and zis a right child

    Case 3: zs uncle yis black and zis a left child

  • 8/3/2019 Lecture+19 20+Red+Black+Trees

    16/23

    Inserting a node z

    Case 1: z,p[z]and zs uncle yall are red.

    Since p[p[z]]is black, color it red, color p[z] and yblack.

    Check for the three cases from p[p[z]], i.e., now zisthe node p[p[z]]

  • 8/3/2019 Lecture+19 20+Red+Black+Trees

    17/23

  • 8/3/2019 Lecture+19 20+Red+Black+Trees

    18/23

  • 8/3/2019 Lecture+19 20+Red+Black+Trees

    19/23

    Inserting a node z

    Case 2: z,p[z]are red,zs uncle yis black, zisright child

    Use a left rotation, we get to case 3.

    Case 3: z,p[z]are red,zs uncle yis black, zis

    left childChange color of p[p[z]]to red, p[z]to black and

    perform a right rotation on p[p[z]]

  • 8/3/2019 Lecture+19 20+Red+Black+Trees

    20/23

  • 8/3/2019 Lecture+19 20+Red+Black+Trees

    21/23

    Example

    Input : { 31, 22, 19, 11, 7, 5, 18, 34, 44, 59,80, 85, 60, 32}

    31 31

    22

  • 8/3/2019 Lecture+19 20+Red+Black+Trees

    22/23

  • 8/3/2019 Lecture+19 20+Red+Black+Trees

    23/23