Lecture-16-CS210-2012 (1).pptx

Embed Size (px)

Citation preview

  • 7/27/2019 Lecture-16-CS210-2012 (1).pptx

    1/37

    Data Structures and Algorithms

    (CS210/ESO207/ESO211)

    Lecture 16

    Height balanced binary search tree

    Red-black trees (Description and handling insertion)

    1

  • 7/27/2019 Lecture-16-CS210-2012 (1).pptx

    2/37

    A rooted binary tree

    2

    root

    edgesNodes

    leaves

  • 7/27/2019 Lecture-16-CS210-2012 (1).pptx

    3/37

    Terminologies

    Fields associated with a node in T:

    1. value

    2. left(pointer to left child)

    3. right(pointer to right child)

    4. parent(pointer to parent)

    Height(T): The maximum number of nodes on any path from root to a leaf node.

    (Note that this definition of height is a bit different from the one we gave in lecture 8.)

    Depth(v): The number of nodes on the path from root to node vin T. Depth of a node

    is also called its level. For example, depth/level of root of T is 1, depth/level of a child

    of root is 2, and so on.

    3

  • 7/27/2019 Lecture-16-CS210-2012 (1).pptx

    4/37

    Terminologies

    Full binary tree:

    A binary tree where every internal node has exactly two children.

    4

    This node has exactly one child. So

    the current tree is not a full binary

    tree.

    This is now a full

    binary tree.

  • 7/27/2019 Lecture-16-CS210-2012 (1).pptx

    5/37

    Binary Search Tree

    Definition:A Binary Tree Tstoring values is said to be Binary Search Tree if for each node vin T

    If left(v) NULL, then value(v) > value of every node in subtree(left(v)).

    If right(v)NULL, then value(v) < value of every node in subtree(right(v)).

    5

    root

    2

    28

    46

    67

    25

    5

    31 41

    35 49

    5348

  • 7/27/2019 Lecture-16-CS210-2012 (1).pptx

    6/37

    Binary Search Tree: a slight transformation

    Henceforth, for each NULLchild link of a node in a BST, we create a NULLnode.

    As a result,

    Each leaf node in a BST will be a NULLnode.

    The BST will always be a full binary tree. 6

    root

    2

    28

    46

    67

    25

    5

    31 41

    35 49

    5348

    This transformation is merely to help us in

    the analysis of red-black trees. It does not

    cause any extra overhead of space. All

    NULL nodescorrespond to a single node

    in memory.

  • 7/27/2019 Lecture-16-CS210-2012 (1).pptx

    7/37

    A fact we noticed in our previous discussion on

    BSTs (Lecture 8)

    Time complexity of Search(T,x) and Insert(T,x) in a Binary Search

    Tree T= ??

    7

    O(Height(T))

  • 7/27/2019 Lecture-16-CS210-2012 (1).pptx

    8/37

    Time complexity of Searching and inserting in a

    perfectly balanced Binary Search Tree on nnodes

    8

    2

    28

    46

    67

    9625

    5

    31 41

    35 49

    5348 73

    83

    T

    O( log n) time

  • 7/27/2019 Lecture-16-CS210-2012 (1).pptx

    9/37

    Time complexity of Searching and inserting in a

    skewed Binary Search tree on nnodes

    9

    23

    T2

    39

    48

    19

    11

    14

    18

    O(n) time !!

  • 7/27/2019 Lecture-16-CS210-2012 (1).pptx

    10/37

    A height balanced BST

    10

  • 7/27/2019 Lecture-16-CS210-2012 (1).pptx

    11/37

    Definition: A BST storing nelements is said to be heightbalanced if its height is O(log n).

    Examples: AVL Trees

    Red Black Trees

    Splay trees

    11

  • 7/27/2019 Lecture-16-CS210-2012 (1).pptx

    12/37

    Red Black Tree

    A height balanced BST

    12

  • 7/27/2019 Lecture-16-CS210-2012 (1).pptx

    13/37

    RedBlackTree

    Red-Black tree is a binary search tree satisfying the following properties at

    each stage.

    Each node is colored redor black.

    Each leaf is colored black and so is the root.

    Every red node will have both its children black.

    The number of blacknodes on a path from root to a leaf node is same for

    every leaf. This parameter is called blackheight of the tree.

    13

  • 7/27/2019 Lecture-16-CS210-2012 (1).pptx

    14/37

    A binary search tree

    14

    head

    2

    28

    46

    67

    25

    5

    31 41

    35 49

    Can you color the

    nodes to make it a

    red-black tree ?

  • 7/27/2019 Lecture-16-CS210-2012 (1).pptx

    15/37

    A binary search tree

    15

    head

    2

    28

    46

    67

    25

    5

    31 41

    35 49

    A Red Black Tree

  • 7/27/2019 Lecture-16-CS210-2012 (1).pptx

    16/37

    Why is a red black tree height balanced ?

    16

    This could not be explained properly in this class. So

    it will be discussed again in the next lecture (12

    September).

  • 7/27/2019 Lecture-16-CS210-2012 (1).pptx

    17/37

    Insertion in a red-black tree

    17

    T

    2

    28

    46

    67

    25

    5

    31 41

    35 49 83

    Let us insert 83into T.

    What color should

    we assign to the

    new node ?

    We hall assign every

    newly inserted node a

    redcolor. (give reason)

  • 7/27/2019 Lecture-16-CS210-2012 (1).pptx

    18/37

    Insertion in a red-black tree

    18

    T

    2

    28

    46

    67

    25

    5

    31 41

    35 49 83

    Let us insert 54into T.

    54

    Color imbalance

    We have again a red

    black tree.In order to remove the color

    imbalance try flipping the colors

    of parent (and uncle) of the new

    node with the grandparent

  • 7/27/2019 Lecture-16-CS210-2012 (1).pptx

    19/37

    Insertion in a red-black tree

    19

    T

    2

    28

    46

    67

    25

    5

    31 41

    35 49 83

    54

  • 7/27/2019 Lecture-16-CS210-2012 (1).pptx

    20/37

    Insertion in a red-black tree

    20

    T

    2

    28

    46

    67

    25

    5

    31 41

    35 49 83

    54

    Let us insert 44into T.

    44

    Color imbalance

    In order to remove the color

    imbalance try flipping the colors

    of parent (and uncle) of the

    new node with the grandparent

  • 7/27/2019 Lecture-16-CS210-2012 (1).pptx

    21/37

    Insertion in a red-black tree

    21

    T

    2

    28

    46

    67

    25

    5

    31 41

    35 49 83

    54

    44

    Color imbalance

    Do the same

    trick again.

  • 7/27/2019 Lecture-16-CS210-2012 (1).pptx

    22/37

    Insertion in a red-black tree

    22

    T

    2

    28

    46

    67

    25

    5

    31 41

    35 49 83

    54

    44

    Color imbalance is removed. But the

    root is rednow.

  • 7/27/2019 Lecture-16-CS210-2012 (1).pptx

    23/37

    Insertion in a red-black tree

    23

    T

    2

    28

    46

    67

    25

    5

    31 41

    35 49 83

    54

    44

  • 7/27/2019 Lecture-16-CS210-2012 (1).pptx

    24/37

    Insertion in a red-black treesummary till now

    Letpbe the newly inserted node. Assign redcolor top.

    Case 1:parent(p) is black

    nothing needs to be done.

    Case 2:parent(p) is red and uncle(p) is red,

    Swap colors ofparent(and uncle) with grandparent(p).

    This balances the color atpbut may lead to imbalance of color at

    grandparent of p. Sopgrandparent(p), and proceed upwards similarly.

    If in this mannerp becomes root, then we color it black.

    Case 3:parent(p) is red and uncle(p) is black.

    This is a nontrivial case. So we need some more tools .

    24

  • 7/27/2019 Lecture-16-CS210-2012 (1).pptx

    25/37

    Rotation around a node

    An important tool for balancing trees

    25

  • 7/27/2019 Lecture-16-CS210-2012 (1).pptx

    26/37

    Rotationaround a node

    26

    v

    u

    xRight rotation Left rotation

    v

    u

    x

    p

    Note that the tree T continues to

    remain a BST even after rotation

    around any node.

  • 7/27/2019 Lecture-16-CS210-2012 (1).pptx

    27/37

    Handling case 3

    27

  • 7/27/2019 Lecture-16-CS210-2012 (1).pptx

    28/37

    Description of Case 3

    pis a redcolored node.

    parent(p) is also red.

    uncle(p) isblack.

    Without loss of generality assume: parent(p) isleft child ofgrandparent(p).

    (The case whenparent(p) isright child of grandparent(p) is handled similarly.)

    28

  • 7/27/2019 Lecture-16-CS210-2012 (1).pptx

    29/37

    Handling the case 3two cases arise depending upon whether p is left/right child of its parent

    29

    g

    d

    x

    b

    kp

    g

    d

    x

    k

    f

    p

    Case 3.1:

    p is left child of its parentCase 3.2:

    p is right child of its parent

    Can you

    transform Case

    3.2 to Case 3.1

    ?

  • 7/27/2019 Lecture-16-CS210-2012 (1).pptx

    30/37

    Handling the case 3two cases arise depending upon whether p is left/right child of its parent

    30

    Case 3.2:

    p is right child of its parent

    g

    f

    x

    k

    d

    21

    3

    Vow!

    This is exactly Case 3.1

    g

    d

    x

    k

    f

    2

    1

    3

    left rotation

  • 7/27/2019 Lecture-16-CS210-2012 (1).pptx

    31/37

    We need to handle onlycase 3.1

    31

  • 7/27/2019 Lecture-16-CS210-2012 (1).pptx

    32/37

    Handling the case 3.1

    32

    g

    d

    x

    b

    k

    21

    3f

    p

    Can we say

    anything about

    color of node f ?

    It has to be

    black.

  • 7/27/2019 Lecture-16-CS210-2012 (1).pptx

    33/37

    Handling the case 3.1

    33

    g

    d

    x

    b

    k

    21

    3f

    p

  • 7/27/2019 Lecture-16-CS210-2012 (1).pptx

    34/37

    Handling the case 3.1

    34

    Right rotation

    g

    d

    x

    b

    k

    21

    3f

    Now every node in tree 1 has one

    less blacknode on the path to root !

    We must restore it. Moreover, the

    color imbalance exists even now.

    What to do ?

    Color node d black

    p g

    2

    d

    x

    b

    kf1

    3

    p

  • 7/27/2019 Lecture-16-CS210-2012 (1).pptx

    35/37

    Handling the case 3.1

    35

    g

    d

    x

    b

    k

    21

    3f

    g

    2

    d

    x

    b

    kf1

    3

    The number of blacknodes on the path to root are

    restored for tree 1. Color imbalance is also removed.

    But the number of blacknodes on the path to root has

    increased by one for trees 2 and 3. What to do now ?

    Color node g red

    p

    p

  • 7/27/2019 Lecture-16-CS210-2012 (1).pptx

    36/37

    Handling the case 3.1

    36

    g

    d

    x

    b

    k

    21

    3f

    g

    2

    d

    x

    b

    kf1

    3

    The black height is

    restored for all trees 1,2,3.

    This completes Case 3.1

    p

    p

  • 7/27/2019 Lecture-16-CS210-2012 (1).pptx

    37/37

    Theorem: We can maintain red-black trees under insertion of

    nodes in O(log n) time per insert/search operation where nis the

    number of the nodes in the tree.

    In the next lecture, we shall show that we can handle deletion in

    a red-black tree with the same time complexity as well.

    37