11 ds and algorithm session 16

Upload: ashish-srivastava

Post on 14-Apr-2018

215 views

Category:

Documents


1 download

TRANSCRIPT

  • 8/2/2019 11 DS and Algorithm Session 16

    1/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    Objectives

    In this session, you will learn to:

    Implement a threaded binary tree

    Implement a height balanced binary tree

    Store data in a graph

  • 8/2/2019 11 DS and Algorithm Session 16

    2/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    Deleting a Node from a Threaded Binary Tree

    Delete operation in a threaded binary tree refers to theprocess of removing the specified node from the threadedbinary tree.

    Before implementing a delete operation, you first need to

    locate the node to be deleted.This requires you to implement a search operation.

    After the search operation, the node to be deleted is markedas the currentNode and its parent as parent.

    Write an algorithm to locate the node to be deleted in athreaded binary tree.

  • 8/2/2019 11 DS and Algorithm Session 16

    3/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    30 50 80

    60

    72

    69

    40. ..

    65.

    .

    header node

    . .

    .

    Algorithm to locate the nodeto be deleted and its parent ina threaded binary tree.

    1. Mark the left child of the header node ascurrentNode.

    2. Mark the header node as parent.3. Repeat steps a, b, c, d, and e until the node

    to be searched is found or currentNodebecomes NULL:a. Mark currentNode as parent.b. If the value to be searched is less

    than that of currentNode, and the leftchild of the currentNode is a link:i. Make currentNode point to its

    left child and go to step 3.c. If the value to be searched is less

    than that of currentNode, and the leftchild of the currentNode is a thread:i. Make currentNode as NULL

    and go to step 3.d. If the value to be searched is greater

    than that of currentNode, and theright child of the currentNode is a link:

    i. Make currentNode point to itsright child and go to step 3.

    e. If the value to be searched is greaterthan that of currentNode, and theright child of the currentNode is athread:i. Mark currentNode as NULL

    and go to step 3.

    Delete 80

    Deleting a Node from a Threaded Binary Tree (Contd.)

  • 8/2/2019 11 DS and Algorithm Session 16

    4/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    30 50 80

    60

    72

    69

    40. ..

    65.

    .

    header node

    . .

    .

    Delete 80

    currentNode

    1. Mark the left child of the header node ascurrentNode.

    2. Mark the header node as parent.3. Repeat steps a, b, c, d, and e until the node

    to be searched is found or currentNodebecomes NULL:a. Mark currentNode as parent.b. If the value to be searched is less

    than that of currentNode, and the leftchild of the currentNode is a link:i. Make currentNode point to its

    left child and go to step 3.c. If the value to be searched is less

    than that of currentNode, and the leftchild of the currentNode is a thread:i. Make currentNode as NULL

    and go to step 3.d. If the value to be searched is greater

    than that of currentNode, and theright child of the currentNode is a link:

    i. Make currentNode point to itsright child and go to step 3.

    e. If the value to be searched is greaterthan that of currentNode, and theright child of the currentNode is athread:i. Mark currentNode as NULL

    and go to step 3.

    Deleting a Node from a Threaded Binary Tree (Contd.)

  • 8/2/2019 11 DS and Algorithm Session 16

    5/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    30 50 80

    60

    72

    69

    40. ..

    65.

    .

    header node

    . .

    .

    Delete 80

    currentNode

    parent

    Deleting a Node from a Threaded Binary Tree (Contd.)1. Mark the left child of the header node as

    currentNode.

    2. Mark the header node as parent.3. Repeat steps a, b, c, d, and e until the node

    to be searched is found or currentNodebecomes NULL:a. Mark currentNode as parent.b. If the value to be searched is less

    than that of currentNode, and the leftchild of the currentNode is a link:i. Make currentNode point to its

    left child and go to step 3.c. If the value to be searched is less

    than that of currentNode, and the leftchild of the currentNode is a thread:i. Make currentNode as NULL

    and go to step 3.d. If the value to be searched is greater

    than that of currentNode, and theright child of the currentNode is a link:

    i. Make currentNode point to itsright child and go to step 3.

    e. If the value to be searched is greaterthan that of currentNode, and theright child of the currentNode is athread:i. Mark currentNode as NULL

    and go to step 3.

  • 8/2/2019 11 DS and Algorithm Session 16

    6/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    30 50 80

    60

    72

    69

    40. ..

    65.

    .

    header node

    . .

    .

    Delete 80

    currentNode

    parent

    Deleting a Node from a Threaded Binary Tree (Contd.)1. Mark the left child of the header node as

    currentNode.

    2. Mark the header node as parent.3. Repeat steps a, b, c, d, and e until the node

    to be searched is found or currentNodebecomes NULL:a. Mark currentNode as parent.b. If the value to be searched is less

    than that of currentNode, and the leftchild of the currentNode is a link:i. Make currentNode point to its

    left child and go to step 3.c. If the value to be searched is less

    than that of currentNode, and the leftchild of the currentNode is a thread:i. Make currentNode as NULL

    and go to step 3.d. If the value to be searched is greater

    than that of currentNode, and theright child of the currentNode is a link:

    i. Make currentNode point to itsright child and go to step 3.

    e. If the value to be searched is greaterthan that of currentNode, and theright child of the currentNode is athread:i. Mark currentNode as NULL

    and go to step 3.

  • 8/2/2019 11 DS and Algorithm Session 16

    7/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    30 50 80

    60

    72

    69

    40. ..

    65.

    .

    header node

    . .

    .

    Delete 80

    currentNode

    parent

    parent

    Deleting a Node from a Threaded Binary Tree (Contd.)1. Mark the left child of the header node as

    currentNode.

    2. Mark the header node as parent.3. Repeat steps a, b, c, d, and e until the node

    to be searched is found or currentNodebecomes NULL:a. Mark currentNode as parent.b. If the value to be searched is less

    than that of currentNode, and the leftchild of the currentNode is a link:i. Make currentNode point to its

    left child and go to step 3.c. If the value to be searched is less

    than that of currentNode, and the leftchild of the currentNode is a thread:i. Make currentNode as NULL

    and go to step 3.d. If the value to be searched is greater

    than that of currentNode, and theright child of the currentNode is a link:

    i. Make currentNode point to itsright child and go to step 3.

    e. If the value to be searched is greaterthan that of currentNode, and theright child of the currentNode is athread:i. Mark currentNode as NULL

    and go to step 3.

  • 8/2/2019 11 DS and Algorithm Session 16

    8/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    30 50 80

    60

    72

    69

    40. ..

    65.

    .

    header node

    . .

    .

    Delete 80

    currentNode parent

    Deleting a Node from a Threaded Binary Tree (Contd.)1. Mark the left child of the header node as

    currentNode.

    2. Mark the header node as parent.3. Repeat steps a, b, c, d, and e until the node

    to be searched is found or currentNodebecomes NULL:a. Mark currentNode as parent.b. If the value to be searched is less

    than that of currentNode, and the leftchild of the currentNode is a link:i. Make currentNode point to its

    left child and go to step 3.c. If the value to be searched is less

    than that of currentNode, and the leftchild of the currentNode is a thread:i. Make currentNode as NULL

    and go to step 3.d. If the value to be searched is greater

    than that of currentNode, and theright child of the currentNode is a link:

    i. Make currentNode point to itsright child and go to step 3.

    e. If the value to be searched is greaterthan that of currentNode, and theright child of the currentNode is athread:i. Mark currentNode as NULL

    and go to step 3.

  • 8/2/2019 11 DS and Algorithm Session 16

    9/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    30 50 80

    60

    72

    69

    40. ..

    65.

    .

    header node

    . .

    .

    Delete 80

    currentNode parent

    Deleting a Node from a Threaded Binary Tree (Contd.)1. Mark the left child of the header node as

    currentNode.

    2. Mark the header node as parent.3. Repeat steps a, b, c, d, and e until the node

    to be searched is found or currentNodebecomes NULL:a. Mark currentNode as parent.b. If the value to be searched is less

    than that of currentNode, and the leftchild of the currentNode is a link:i. Make currentNode point to its

    left child and go to step 3.c. If the value to be searched is less

    than that of currentNode, and the leftchild of the currentNode is a thread:i. Make currentNode as NULL

    and go to step 3.d. If the value to be searched is greater

    than that of currentNode, and theright child of the currentNode is a link:

    i. Make currentNode point to itsright child and go to step 3.

    e. If the value to be searched is greaterthan that of currentNode, and theright child of the currentNode is athread:i. Mark currentNode as NULL

    and go to step 3.

  • 8/2/2019 11 DS and Algorithm Session 16

    10/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    30 50 80

    60

    72

    69

    40. ..

    65.

    .

    header node

    . .

    .

    Delete 80

    currentNode parent

    Deleting a Node from a Threaded Binary Tree (Contd.)1. Mark the left child of the header node as

    currentNode.

    2. Mark the header node as parent.3. Repeat steps a, b, c, d, and e until the node

    to be searched is found or currentNodebecomes NULL:a. Mark currentNode as parent.b. If the value to be searched is less

    than that of currentNode, and the leftchild of the currentNode is a link:i. Make currentNode point to its

    left child and go to step 3.c. If the value to be searched is less

    than that of currentNode, and the leftchild of the currentNode is a thread:i. Make currentNode as NULL

    and go to step 3.d. If the value to be searched is greater

    than that of currentNode, and theright child of the currentNode is a link:

    i. Make currentNode point to itsright child and go to step 3.

    e. If the value to be searched is greaterthan that of currentNode, and theright child of the currentNode is athread:i. Mark currentNode as NULL

    and go to step 3.

  • 8/2/2019 11 DS and Algorithm Session 16

    11/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    30 50 80

    60

    72

    69

    40. ..

    65.

    .

    header node

    . .

    .

    Delete 80

    currentNode parent

    currentNode

    Deleting a Node from a Threaded Binary Tree (Contd.)1. Mark the left child of the header node as

    currentNode.

    2. Mark the header node as parent.3. Repeat steps a, b, c, d, and e until the nodeto be searched is found or currentNodebecomes NULL:a. Mark currentNode as parent.b. If the value to be searched is less

    than that of currentNode, and the leftchild of the currentNode is a link:i. Make currentNode point to its

    left child and go to step 3.c. If the value to be searched is less

    than that of currentNode, and the leftchild of the currentNode is a thread:i. Make currentNode as NULL

    and go to step 3.d. If the value to be searched is greater

    than that of currentNode, and theright child of the currentNode is a link:

    i. Make currentNode point to itsright child and go to step 3.

    e. If the value to be searched is greaterthan that of currentNode, and theright child of the currentNode is athread:i. Mark currentNode as NULL

    and go to step 3.

  • 8/2/2019 11 DS and Algorithm Session 16

    12/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    30 50 80

    60

    72

    69

    40. ..

    65.

    .

    header node

    . .

    .

    Delete 80

    parent

    currentNode

    Deleting a Node from a Threaded Binary Tree (Contd.)1. Mark the left child of the header node as

    currentNode.

    2. Mark the header node as parent.3. Repeat steps a, b, c, d, and e until the nodeto be searched is found or currentNodebecomes NULL:a. Mark currentNode as parent.b. If the value to be searched is less

    than that of currentNode, and the leftchild of the currentNode is a link:i. Make currentNode point to its

    left child and go to step 3.c. If the value to be searched is less

    than that of currentNode, and the leftchild of the currentNode is a thread:i. Make currentNode as NULL

    and go to step 3.d. If the value to be searched is greater

    than that of currentNode, and theright child of the currentNode is a link:

    i. Make currentNode point to itsright child and go to step 3.

    e. If the value to be searched is greaterthan that of currentNode, and theright child of the currentNode is athread:i. Mark currentNode as NULL

    and go to step 3.

  • 8/2/2019 11 DS and Algorithm Session 16

    13/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    30 50 80

    60

    72

    69

    40. ..

    65.

    .

    header node

    . .

    .

    Delete 80

    parent

    currentNode

    parent

    Deleting a Node from a Threaded Binary Tree (Contd.)1. Mark the left child of the header node as

    currentNode.

    2. Mark the header node as parent.3. Repeat steps a, b, c, d, and e until the nodeto be searched is found or currentNodebecomes NULL:a. Mark currentNode as parent.b. If the value to be searched is less

    than that of currentNode, and the leftchild of the currentNode is a link:i. Make currentNode point to its

    left child and go to step 3.c. If the value to be searched is less

    than that of currentNode, and the leftchild of the currentNode is a thread:i. Make currentNode as NULL

    and go to step 3.d. If the value to be searched is greater

    than that of currentNode, and theright child of the currentNode is a link:

    i. Make currentNode point to itsright child and go to step 3.

    e. If the value to be searched is greaterthan that of currentNode, and theright child of the currentNode is athread:i. Mark currentNode as NULL

    and go to step 3.

  • 8/2/2019 11 DS and Algorithm Session 16

    14/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    30 50 80

    60

    72

    69

    40. ..

    65.

    .

    header node

    . .

    .

    Delete 80

    currentNode

    parent

    Deleting a Node from a Threaded Binary Tree (Contd.)1. Mark the left child of the header node as

    currentNode.

    2. Mark the header node as parent.3. Repeat steps a, b, c, d, and e until the nodeto be searched is found or currentNodebecomes NULL:a. Mark currentNode as parent.b. If the value to be searched is less

    than that of currentNode, and the leftchild of the currentNode is a link:i. Make currentNode point to its

    left child and go to step 3.c. If the value to be searched is less

    than that of currentNode, and the leftchild of the currentNode is a thread:i. Make currentNode as NULL

    and go to step 3.d. If the value to be searched is greater

    than that of currentNode, and theright child of the currentNode is a link:

    i. Make currentNode point to itsright child and go to step 3.

    e. If the value to be searched is greaterthan that of currentNode, and theright child of the currentNode is athread:i. Mark currentNode as NULL

    and go to step 3.

  • 8/2/2019 11 DS and Algorithm Session 16

    15/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    30 50 80

    60

    72

    69

    40. ..

    65.

    .

    header node

    . .

    .

    Delete 80

    currentNode

    parent

    Deleting a Node from a Threaded Binary Tree (Contd.)1. Mark the left child of the header node as

    currentNode.

    2. Mark the header node as parent.3. Repeat steps a, b, c, d, and e until the nodeto be searched is found or currentNodebecomes NULL:a. Mark currentNode as parent.b. If the value to be searched is less

    than that of currentNode, and the leftchild of the currentNode is a link:i. Make currentNode point to its

    left child and go to step 3.c. If the value to be searched is less

    than that of currentNode, and the leftchild of the currentNode is a thread:i. Make currentNode as NULL

    and go to step 3.d. If the value to be searched is greater

    than that of currentNode, and theright child of the currentNode is a link:

    i. Make currentNode point to itsright child and go to step 3.

    e. If the value to be searched is greaterthan that of currentNode, and theright child of the currentNode is athread:i. Mark currentNode as NULL

    and go to step 3.

  • 8/2/2019 11 DS and Algorithm Session 16

    16/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    30 50 80

    60

    72

    69

    40. ..

    65.

    .

    header node

    . .

    .

    Delete 80

    currentNode

    parent

    Deleting a Node from a Threaded Binary Tree (Contd.)1. Mark the left child of the header node as

    currentNode.

    2. Mark the header node as parent.3. Repeat steps a, b, c, d, and e until the nodeto be searched is found or currentNodebecomes NULL:a. Mark currentNode as parent.b. If the value to be searched is less

    than that of currentNode, and the leftchild of the currentNode is a link:i. Make currentNode point to its

    left child and go to step 3.c. If the value to be searched is less

    than that of currentNode, and the leftchild of the currentNode is a thread:i. Make currentNode as NULL

    and go to step 3.d. If the value to be searched is greater

    than that of currentNode, and theright child of the currentNode is a link:

    i. Make currentNode point to itsright child and go to step 3.

    e. If the value to be searched is greaterthan that of currentNode, and theright child of the currentNode is athread:i. Mark currentNode as NULL

    and go to step 3.

  • 8/2/2019 11 DS and Algorithm Session 16

    17/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    30 50 80

    60

    72

    69

    40. ..

    65.

    .

    header node

    . .

    .

    Delete 80

    currentNode

    parent

    currentNode

    Deleting a Node from a Threaded Binary Tree (Contd.)1. Mark the left child of the header node as

    currentNode.

    2. Mark the header node as parent.3. Repeat steps a, b, c, d, and e until the nodeto be searched is found or currentNodebecomes NULL:a. Mark currentNode as parent.b. If the value to be searched is less

    than that of currentNode, and the leftchild of the currentNode is a link:i. Make currentNode point to its

    left child and go to step 3.c. If the value to be searched is less

    than that of currentNode, and the leftchild of the currentNode is a thread:i. Make currentNode as NULL

    and go to step 3.d. If the value to be searched is greater

    than that of currentNode, and theright child of the currentNode is a link:

    i. Make currentNode point to itsright child and go to step 3.

    e. If the value to be searched is greaterthan that of currentNode, and theright child of the currentNode is athread:i. Mark currentNode as NULL

    and go to step 3.

  • 8/2/2019 11 DS and Algorithm Session 16

    18/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    30 50 80

    60

    72

    69

    40. ..

    65.

    .

    header node

    . .

    .

    Delete 80

    parent

    currentNode

    Nodes located

    Deleting a Node from a Threaded Binary Tree (Contd.)1. Mark the left child of the header node as

    currentNode.

    2. Mark the header node as parent.3. Repeat steps a, b, c, d, and e until the nodeto be searched is found or currentNodebecomes NULL:a. Mark currentNode as parent.b. If the value to be searched is less

    than that of currentNode, and the leftchild of the currentNode is a link:i. Make currentNode point to its

    left child and go to step 3.c. If the value to be searched is less

    than that of currentNode, and the leftchild of the currentNode is a thread:i. Make currentNode as NULL

    and go to step 3.d. If the value to be searched is greater

    than that of currentNode, and theright child of the currentNode is a link:

    i. Make currentNode point to itsright child and go to step 3.

    e. If the value to be searched is greaterthan that of currentNode, and theright child of the currentNode is athread:i. Mark currentNode as NULL

    and go to step 3.

  • 8/2/2019 11 DS and Algorithm Session 16

    19/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    Once you locate the node to be deleted and its parent, youcan release the memory of the node to be deleted afteradjusting the links and threads appropriately.

    Before implementing a delete operation, you first need tocheck whether the tree is empty or not.

    The tree is empty if the left child of the header node is athread pointing to itself.

    If the tree is empty, an error message is shown.

    However, if the tree is not empty, there can be three cases:

    Node to be deleted is a leaf node

    Node to be deleted has one child (left or right)

    Node to be deleted has two children

    header node

    Deleting a Node from a Threaded Binary Tree (Contd.)

  • 8/2/2019 11 DS and Algorithm Session 16

    20/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    Let us first consider a case in which the node to be deletedis the leaf node.

    In this case, you first need to check if there is only one nodepresent in the tree.

    header node

    65

    new node

    Deleting a Node from a Threaded Binary Tree (Contd.)

  • 8/2/2019 11 DS and Algorithm Session 16

    21/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    To delete this node, make the left child of the header nodeas a thread pointing to itself.

    header node

    65

    new node

    Deleting a Node from a Threaded Binary Tree (Contd.)

  • 8/2/2019 11 DS and Algorithm Session 16

    22/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    To delete this node, make the left child of the header nodeas a thread pointing to itself.

    Now release the memory of the node to be deleted.

    header node

    65

    new node

    Delete operation complete

    Deleting a Node from a Threaded Binary Tree (Contd.)

  • 8/2/2019 11 DS and Algorithm Session 16

    23/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    However, if there are more thanone nodes in the tree, you needanother algorithm to delete a leafnode.

    1. Locate the position of the node to

    be deleted. Mark it as currentNodeand its parent as parent.

    2. If currentNode is the left child ofparent:a. Set the left thread field of

    parent as zero.b. Make the left child field of

    parent as a thread pointing

    to the inorder predecessorof currentNode.

    c. Go to step 4.

    3. If currentNode is the right child ofparent:a. Set the right thread field of

    parent as zero.b. Make the right child field of

    parent as a thread pointingto the inorder successor ofcurrentNode.

    4. Release the memory forcurrentNode.

    30 50 80

    60

    72

    69

    40. ..

    65.

    .

    header node

    . .

    .

    Delete node 60

    Deleting a Node from a Threaded Binary Tree (Contd.)

  • 8/2/2019 11 DS and Algorithm Session 16

    24/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    30 50 80

    60

    72

    69

    40. ..

    65.

    .

    header node

    . .

    .

    Delete node 60

    currentNode

    parent

    1. Locate the position of the node to

    be deleted. Mark it as currentNodeand its parent as parent.

    2. If currentNode is the left child ofparent:a. Set the left thread field of

    parent as zero.b. Make the left child field of

    parent as a thread pointing

    to the inorder predecessorof currentNode.

    c. Go to step 4.

    3. If currentNode is the right child ofparent:a. Set the right thread field of

    parent as zero.b. Make the right child field of

    parent as a thread pointingto the inorder successor ofcurrentNode.

    4. Release the memory forcurrentNode.

    Deleting a Node from a Threaded Binary Tree (Contd.)

  • 8/2/2019 11 DS and Algorithm Session 16

    25/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    30 50 80

    60

    72

    69

    40. ..

    65.

    .

    header node

    . .

    .

    Delete node 60

    currentNode

    parent

    1. Locate the position of the node to

    be deleted. Mark it as currentNodeand its parent as parent.

    2. If currentNode is the left child ofparent:a. Set the left thread field of

    parent as zero.b. Make the left child field of

    parent as a thread pointing

    to the inorder predecessorof currentNode.

    c. Go to step 4.

    3. If currentNode is the right child ofparent:a. Set the right thread field of

    parent as zero.b. Make the right child field of

    parent as a thread pointingto the inorder successor ofcurrentNode.

    4. Release the memory forcurrentNode.

    Deleting a Node from a Threaded Binary Tree (Contd.)

  • 8/2/2019 11 DS and Algorithm Session 16

    26/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    30 50 80

    60

    72

    69

    40. ..

    65.

    .

    header node

    . .

    .

    Delete node 60

    currentNode

    parent

    1. Locate the position of the node to

    be deleted. Mark it as currentNodeand its parent as parent.

    2. If currentNode is the left child ofparent:a. Set the left thread field of

    parent as zero.b. Make the left child field of

    parent as a thread pointing

    to the inorder predecessorof currentNode.

    c. Go to step 4.

    3. If currentNode is the right child ofparent:a. Set the right thread field of

    parent as zero.b. Make the right child field of

    parent as a thread pointingto the inorder successor ofcurrentNode.

    4. Release the memory forcurrentNode.

    Deleting a Node from a Threaded Binary Tree (Contd.)

  • 8/2/2019 11 DS and Algorithm Session 16

    27/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    30 50 80

    60

    72

    69

    40. ..

    65.

    .

    header node

    . .

    .

    Delete node 60

    currentNode

    parent

    1. Locate the position of the node to

    be deleted. Mark it as currentNodeand its parent as parent.

    2. If currentNode is the left child ofparent:a. Set the left thread field of

    parent as zero.b. Make the left child field of

    parent as a thread pointing

    to the inorder predecessorof currentNode.

    c. Go to step 4.

    3. If currentNode is the right child ofparent:a. Set the right thread field of

    parent as zero.b. Make the right child field of

    parent as a thread pointingto the inorder successor ofcurrentNode.

    4. Release the memory forcurrentNode.

    Deleting a Node from a Threaded Binary Tree (Contd.)

  • 8/2/2019 11 DS and Algorithm Session 16

    28/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    30 50 80

    60

    72

    69

    40. ..

    65.

    .

    header node

    . .

    .

    Delete node 60

    currentNode

    parent

    1. Locate the position of the node to

    be deleted. Mark it as currentNodeand its parent as parent.

    2. If currentNode is the left child ofparent:a. Set the left thread field of

    parent as zero.b. Make the left child field of

    parent as a thread pointing

    to the inorder predecessorof currentNode.

    c. Go to step 4.

    3. If currentNode is the right child ofparent:a. Set the right thread field of

    parent as zero.b. Make the right child field of

    parent as a thread pointingto the inorder successor ofcurrentNode.

    4. Release the memory forcurrentNode.

    Deleting a Node from a Threaded Binary Tree (Contd.)

  • 8/2/2019 11 DS and Algorithm Session 16

    29/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    30 50 80

    60

    72

    69

    40. ..

    65.

    .

    header node

    .

    .

    Delete node 60

    currentNode

    parent

    Deletion complete

    1. Locate the position of the node to

    be deleted. Mark it as currentNodeand its parent as parent.

    2. If currentNode is the left child ofparent:a. Set the left thread field of

    parent as zero.b. Make the left child field of

    parent as a thread pointing

    to the inorder predecessorof currentNode.

    c. Go to step 4.

    3. If currentNode is the right child ofparent:a. Set the right thread field of

    parent as zero.b. Make the right child field of

    parent as a thread pointingto the inorder successor ofcurrentNode.

    4. Release the memory forcurrentNode.

    Deleting a Node from a Threaded Binary Tree (Contd.)

  • 8/2/2019 11 DS and Algorithm Session 16

    30/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    Write an algorithm to delete a node, which has one child in athreaded binary tree.

    Deleting a Node from a Threaded Binary Tree (Contd.)

  • 8/2/2019 11 DS and Algorithm Session 16

    31/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    Let us now consider a case inwhich the node to be deletedhas one child (left or right).

    30 50 80

    60

    72

    69

    40. ..

    65.

    .

    header node

    . .

    .

    Algorithm to delete a nodewith one child from athreaded binary tree.

    Delete node 50

    Deleting a Node from a Threaded Binary Tree (Contd.)

    1. Locate the node to be deleted. Mark it ascurrentNode and its parent as parent.

    2. If currentNode has a left subtree:a. Mark the left child of current node aschild.

    b. Go to step 4.3. If currentNode has a right subtree:

    a. Mark the right child of currentNode aschild.

    b. Go to step 4.4. If currentNode is the left child of parent:

    a. Make the left child of parent point to

    child.b. Go to step 6.

    5. If currentNode is the right child of parent:a. Make the right child of parent point to

    child.b. Go to step 6.

    6. Find the inorder successor and inorderpredecessor of currentNode. Mark them assuccessor and predecessor, respectively.

    7. If currentNode has a right child:a. Make the left child field of its successorpoint to its predecessor and set the leftthread of successor to zero.

    b. Go to step 9.8. If currentNode has a left child:

    a. Make the right child of its predecessorpoint to its successor and set the rightthread of predecessor to zero.

    9. Release the memory for currentNode.

  • 8/2/2019 11 DS and Algorithm Session 16

    32/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    30 50 80

    60

    72

    69

    40. ..

    65.

    .

    header node

    . .

    .parent

    currentNode

    Delete node 50

    Deleting a Node from a Threaded Binary Tree (Contd.)

    1. Locate the node to be deleted. Mark it ascurrentNode and its parent as parent.

    2. If currentNode has a left subtree:a. Mark the left child of current node aschild.

    b. Go to step 4.3. If currentNode has a right subtree:

    a. Mark the right child of currentNode aschild.

    b. Go to step 4.4. If currentNode is the left child of parent:

    a. Make the left child of parent point to

    child.b. Go to step 6.

    5. If currentNode is the right child of parent:a. Make the right child of parent point to

    child.b. Go to step 6.

    6. Find the inorder successor and inorderpredecessor of currentNode. Mark them assuccessor and predecessor, respectively.

    7. If currentNode has a right child:a. Make the left child field of its successor

    point to its predecessor and set the leftthread of successor to zero.

    b. Go to step 9.8. If currentNode has a left child:

    a. Make the right child of its predecessorpoint to its successor and set the rightthread of predecessor to zero.

    9. Release the memory for currentNode.

  • 8/2/2019 11 DS and Algorithm Session 16

    33/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    30 50 80

    60

    72

    69

    40. ..

    65.

    .

    header node

    . .

    .

    Delete node 50

    parent

    currentNode

    Deleting a Node from a Threaded Binary Tree (Contd.)

    1. Locate the node to be deleted. Mark it ascurrentNode and its parent as parent.

    2. If currentNode has a left subtree:a. Mark the left child of current node aschild.

    b. Go to step 4.3. If currentNode has a right subtree:

    a. Mark the right child of currentNode aschild.

    b. Go to step 4.4. If currentNode is the left child of parent:

    a. Make the left child of parent point to

    child.b. Go to step 6.

    5. If currentNode is the right child of parent:a. Make the right child of parent point to

    child.b. Go to step 6.

    6. Find the inorder successor and inorderpredecessor of currentNode. Mark them assuccessor and predecessor, respectively.

    7. If currentNode has a right child:a. Make the left child field of its successor

    point to its predecessor and set the leftthread of successor to zero.

    b. Go to step 9.8. If currentNode has a left child:

    a. Make the right child of its predecessorpoint to its successor and set the rightthread of predecessor to zero.

    9. Release the memory for currentNode.

  • 8/2/2019 11 DS and Algorithm Session 16

    34/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    30 50 80

    60

    72

    69

    40. ..

    65.

    .

    header node

    . .

    .

    Delete node 50

    parent

    currentNode

    Deleting a Node from a Threaded Binary Tree (Contd.)

    1. Locate the node to be deleted. Mark it ascurrentNode and its parent as parent.

    2. If currentNode has a left subtree:a. Mark the left child of current node aschild.

    b. Go to step 4.3. If currentNode has a right subtree:

    a. Mark the right child of currentNode aschild.

    b. Go to step 4.4. If currentNode is the left child of parent:

    a. Make the left child of parent point to

    child.b. Go to step 6.

    5. If currentNode is the right child of parent:a. Make the right child of parent point to

    child.b. Go to step 6.

    6. Find the inorder successor and inorderpredecessor of currentNode. Mark them assuccessor and predecessor, respectively.

    7. If currentNode has a right child:a. Make the left child field of its successor

    point to its predecessor and set the leftthread of successor to zero.

    b. Go to step 9.8. If currentNode has a left child:

    a. Make the right child of its predecessorpoint to its successor and set the rightthread of predecessor to zero.

    9. Release the memory for currentNode.

    S d l i h

  • 8/2/2019 11 DS and Algorithm Session 16

    35/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    30 50 80

    60

    72

    69

    40. ..

    65.

    .

    header node

    . .

    .

    Delete node 50

    parent

    currentNode

    child

    Deleting a Node from a Threaded Binary Tree (Contd.)

    1. Locate the node to be deleted. Mark it ascurrentNode and its parent as parent.

    2. If currentNode has a left subtree:a. Mark the left child of current node aschild.

    b. Go to step 4.3. If currentNode has a right subtree:

    a. Mark the right child of currentNode aschild.

    b. Go to step 4.4. If currentNode is the left child of parent:

    a. Make the left child of parent point to

    child.b. Go to step 6.

    5. If currentNode is the right child of parent:a. Make the right child of parent point to

    child.b. Go to step 6.

    6. Find the inorder successor and inorderpredecessor of currentNode. Mark them assuccessor and predecessor, respectively.

    7. If currentNode has a right child:a. Make the left child field of its successor

    point to its predecessor and set the leftthread of successor to zero.

    b. Go to step 9.8. If currentNode has a left child:

    a. Make the right child of its predecessorpoint to its successor and set the rightthread of predecessor to zero.

    9. Release the memory for currentNode.

    D S d Al i h

  • 8/2/2019 11 DS and Algorithm Session 16

    36/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    30 50 80

    60

    72

    69

    40. ..

    65.

    .

    header node

    . .

    .

    Delete node 50

    parent

    currentNode

    child

    Deleting a Node from a Threaded Binary Tree (Contd.)

    1. Locate the node to be deleted. Mark it ascurrentNode and its parent as parent.

    2. If currentNode has a left subtree:a. Mark the left child of current node aschild.

    b. Go to step 4.3. If currentNode has a right subtree:

    a. Mark the right child of currentNode aschild.

    b. Go to step 4.4. If currentNode is the left child of parent:

    a. Make the left child of parent point to

    child.b. Go to step 6.

    5. If currentNode is the right child of parent:a. Make the right child of parent point to

    child.b. Go to step 6.

    6. Find the inorder successor and inorderpredecessor of currentNode. Mark them assuccessor and predecessor, respectively.

    7. If currentNode has a right child:a. Make the left child field of its successor

    point to its predecessor and set the leftthread of successor to zero.

    b. Go to step 9.8. If currentNode has a left child:

    a. Make the right child of its predecessorpoint to its successor and set the rightthread of predecessor to zero.

    9. Release the memory for currentNode.

    D t St t d Al ith

  • 8/2/2019 11 DS and Algorithm Session 16

    37/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    30 50 80

    60

    72

    69

    40. ..

    65.

    .

    header node

    . .

    .

    Delete node 50

    parent

    currentNode

    child

    Deleting a Node from a Threaded Binary Tree (Contd.)

    1. Locate the node to be deleted. Mark it ascurrentNode and its parent as parent.

    2. If currentNode has a left subtree:a. Mark the left child of current node aschild.

    b. Go to step 4.3. If currentNode has a right subtree:

    a. Mark the right child of currentNode aschild.

    b. Go to step 4.4. If currentNode is the left child of parent:

    a. Make the left child of parent point to

    child.b. Go to step 6.

    5. If currentNode is the right child of parent:a. Make the right child of parent point to

    child.b. Go to step 6.

    6. Find the inorder successor and inorderpredecessor of currentNode. Mark them assuccessor and predecessor, respectively.

    7. If currentNode has a right child:a. Make the left child field of its successor

    point to its predecessor and set the leftthread of successor to zero.

    b. Go to step 9.8. If currentNode has a left child:

    a. Make the right child of its predecessorpoint to its successor and set the rightthread of predecessor to zero.

    9. Release the memory for currentNode.

    D t St t d Al ith

  • 8/2/2019 11 DS and Algorithm Session 16

    38/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    30 50 80

    60

    72

    69

    40. ..

    65.

    .

    header node

    . .

    .

    Delete node 50

    parent

    currentNode

    child

    Deleting a Node from a Threaded Binary Tree (Contd.)

    1. Locate the node to be deleted. Mark it ascurrentNode and its parent as parent.

    2. If currentNode has a left subtree:a. Mark the left child of current node aschild.

    b. Go to step 4.3. If currentNode has a right subtree:

    a. Mark the right child of currentNode aschild.

    b. Go to step 4.4. If currentNode is the left child of parent:

    a. Make the left child of parent point to

    child.b. Go to step 6.

    5. If currentNode is the right child of parent:a. Make the right child of parent point to

    child.b. Go to step 6.

    6. Find the inorder successor and inorderpredecessor of currentNode. Mark them assuccessor and predecessor, respectively.

    7. If currentNode has a right child:a. Make the left child field of its successor

    point to its predecessor and set the leftthread of successor to zero.

    b. Go to step 9.8. If currentNode has a left child:

    a. Make the right child of its predecessorpoint to its successor and set the rightthread of predecessor to zero.

    9. Release the memory for currentNode.

  • 8/2/2019 11 DS and Algorithm Session 16

    39/129

    D t St t d Al ith

  • 8/2/2019 11 DS and Algorithm Session 16

    40/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    30 50 80

    60

    72

    69

    40. ..

    65.

    .

    header node

    . .

    .

    Delete node 50

    parent

    currentNode

    child

    Deleting a Node from a Threaded Binary Tree (Contd.)

    1. Locate the node to be deleted. Mark it ascurrentNode and its parent as parent.

    2. If currentNode has a left subtree:a. Mark the left child of current node aschild.

    b. Go to step 4.3. If currentNode has a right subtree:

    a. Mark the right child of currentNode aschild.

    b. Go to step 4.4. If currentNode is the left child of parent:

    a. Make the left child of parent point to

    child.b. Go to step 6.

    5. If currentNode is the right child of parent:a. Make the right child of parent point to

    child.b. Go to step 6.

    6. Find the inorder successor and inorderpredecessor of currentNode. Mark them assuccessor and predecessor, respectively.

    7. If currentNode has a right child:a. Make the left child field of its successor

    point to its predecessor and set the leftthread of successor to zero.

    b. Go to step 9.8. If currentNode has a left child:

    a. Make the right child of its predecessorpoint to its successor and set the rightthread of predecessor to zero.

    9. Release the memory for currentNode.

    Data St t e a d Al o ith

  • 8/2/2019 11 DS and Algorithm Session 16

    41/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    30 50 80

    60

    72

    69

    40. ..

    65.

    .

    header node

    . .

    .

    Delete node 50

    parent

    currentNode

    childsuccessor

    predecessor

    Deleting a Node from a Threaded Binary Tree (Contd.)

    1. Locate the node to be deleted. Mark it ascurrentNode and its parent as parent.

    2. If currentNode has a left subtree:a. Mark the left child of current node aschild.

    b. Go to step 4.3. If currentNode has a right subtree:

    a. Mark the right child of currentNode aschild.

    b. Go to step 4.4. If currentNode is the left child of parent:

    a. Make the left child of parent point to

    child.b. Go to step 6.

    5. If currentNode is the right child of parent:a. Make the right child of parent point to

    child.b. Go to step 6.

    6. Find the inorder successor and inorderpredecessor of currentNode. Mark them assuccessor and predecessor, respectively.

    7. If currentNode has a right child:a. Make the left child field of its successor

    point to its predecessor and set the leftthread of successor to zero.

    b. Go to step 9.8. If currentNode has a left child:

    a. Make the right child of its predecessorpoint to its successor and set the rightthread of predecessor to zero.

    9. Release the memory for currentNode.

    Data Structures and Algorithms

  • 8/2/2019 11 DS and Algorithm Session 16

    42/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    30 50 80

    60

    72

    69

    40. ..

    65.

    .

    header node

    . .

    .

    Delete node 50

    parent

    currentNode

    childsuccessor

    predecessor

    Deleting a Node from a Threaded Binary Tree (Contd.)

    1. Locate the node to be deleted. Mark it ascurrentNode and its parent as parent.

    2. If currentNode has a left subtree:a. Mark the left child of current node aschild.

    b. Go to step 4.3. If currentNode has a right subtree:

    a. Mark the right child of currentNode aschild.

    b. Go to step 4.4. If currentNode is the left child of parent:

    a. Make the left child of parent point to

    child.b. Go to step 6.

    5. If currentNode is the right child of parent:a. Make the right child of parent point to

    child.b. Go to step 6.

    6. Find the inorder successor and inorderpredecessor of currentNode. Mark them assuccessor and predecessor, respectively.

    7. If currentNode has a right child:a. Make the left child field of its successor

    point to its predecessor and set the leftthread of successor to zero.

    b. Go to step 9.8. If currentNode has a left child:

    a. Make the right child of its predecessorpoint to its successor and set the rightthread of predecessor to zero.

    9. Release the memory for currentNode.

    Data Structures and Algorithms

  • 8/2/2019 11 DS and Algorithm Session 16

    43/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    30 50 80

    60

    72

    69

    40. ..

    65.

    .

    header node

    . .

    .

    Delete node 50

    parent

    currentNode

    childsuccessor

    predecessor

    currentNode

    Deleting a Node from a Threaded Binary Tree (Contd.)

    1. Locate the node to be deleted. Mark it ascurrentNode and its parent as parent.

    2. If currentNode has a left subtree:a. Mark the left child of current node aschild.

    b. Go to step 4.3. If currentNode has a right subtree:

    a. Mark the right child of currentNode aschild.

    b. Go to step 4.4. If currentNode is the left child of parent:

    a. Make the left child of parent point to

    child.b. Go to step 6.

    5. If currentNode is the right child of parent:a. Make the right child of parent point to

    child.b. Go to step 6.

    6. Find the inorder successor and inorderpredecessor of currentNode. Mark them assuccessor and predecessor, respectively.

    7. If currentNode has a right child:a. Make the left child field of its successor

    point to its predecessor and set the leftthread of successor to zero.

    b. Go to step 9.8. If currentNode has a left child:

    a. Make the right child of its predecessorpoint to its successor and set the rightthread of predecessor to zero.

    9. Release the memory for currentNode.

    Data Structures and Algorithms

  • 8/2/2019 11 DS and Algorithm Session 16

    44/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    30 50 80

    60

    72

    69

    40. ..

    65.

    .

    header node

    . .

    .

    Delete node 50

    parent

    childsuccessor

    predecessor

    currentNode

    Deleting a Node from a Threaded Binary Tree (Contd.)

    1. Locate the node to be deleted. Mark it ascurrentNode and its parent as parent.

    2. If currentNode has a left subtree:a. Mark the left child of current node aschild.

    b. Go to step 4.3. If currentNode has a right subtree:

    a. Mark the right child of currentNode aschild.

    b. Go to step 4.4. If currentNode is the left child of parent:

    a. Make the left child of parent point to

    child.b. Go to step 6.

    5. If currentNode is the right child of parent:a. Make the right child of parent point to

    child.b. Go to step 6.

    6. Find the inorder successor and inorderpredecessor of currentNode. Mark them assuccessor and predecessor, respectively.

    7. If currentNode has a right child:a. Make the left child field of its successor

    point to its predecessor and set the leftthread of successor to zero.

    b. Go to step 9.8. If currentNode has a left child:

    a. Make the right child of its predecessorpoint to its successor and set the rightthread of predecessor to zero.

    9. Release the memory for currentNode.

    Data Structures and Algorithms

  • 8/2/2019 11 DS and Algorithm Session 16

    45/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    30 50 80

    60

    72

    69

    40. ..

    65.

    .

    header node

    . .

    .

    Delete node 50

    parent

    childsuccessor

    predecessor

    currentNodeDeletion

    complete

    Deleting a Node from a Threaded Binary Tree (Contd.)

    1. Locate the node to be deleted. Mark it ascurrentNode and its parent as parent.

    2. If currentNode has a left subtree:a. Mark the left child of current node as

    child.b. Go to step 4.

    3. If currentNode has a right subtree:a. Mark the right child of currentNode as

    child.b. Go to step 4.

    4. If currentNode is the left child of parent:a. Make the left child of parent point to

    child.b. Go to step 6.

    5. If currentNode is the right child of parent:a. Make the right child of parent point to

    child.b. Go to step 6.

    6. Find the inorder successor and inorderpredecessor of currentNode. Mark them assuccessor and predecessor, respectively.

    7. If currentNode has a right child:a. Make the left child field of its successor

    point to its predecessor and set the leftthread of successor to zero.

    b. Go to step 9.8. If currentNode has a left child:

    a. Make the right child of its predecessorpoint to its successor and set the rightthread of predecessor to zero.

    9. Release the memory for currentNode.

    Data Structures and Algorithms

  • 8/2/2019 11 DS and Algorithm Session 16

    46/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    Write an algorithm to delete a node, which has two childrenfrom a threaded binary tree.Algorithm to delete a node havingtwo children from a threaded binarytree.

    1. Locate the position of the node to

    be deleted. Mark it ascurrentNode.

    2. Locate the inorder successor ofcurrentNode. Mark it asInorder_suc and its parent asInorder_parent.

    3. Overwrite the data contained in

    currentNode with the datacontained in Inorder_suc.

    4. Delete the node markedInorder_suc. This node wouldhave at most one child and cantherefore be deleted by using thealgorithm for one of the following:

    a. Deleting a leaf node.

    b. Deleting a node with onechild.

    Deleting a Node from a Threaded Binary Tree (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 11 DS and Algorithm Session 16

    47/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    30 50 80

    45

    72

    69

    40. ..

    65.

    .

    header node

    .

    .

    Delete node 40 1. Locate the position of the node to

    be deleted. Mark it ascurrentNode.

    2. Locate the inorder successor ofcurrentNode. Mark it asInorder_suc and its parent asInorder_parent.

    3. Overwrite the data contained in

    currentNode with the datacontained in Inorder_suc.

    4. Delete the node markedInorder_suc. This node wouldhave at most one child and cantherefore be deleted by using thealgorithm for one of the following:

    a. Deleting a leaf node.

    b. Deleting a node with onechild.

    Deleting a Node from a Threaded Binary Tree (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 11 DS and Algorithm Session 16

    48/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    30 50 80

    45

    72

    69

    40. ..

    65.

    .

    header node

    .

    .

    Delete node 40

    currentNode

    Deleting a Node from a Threaded Binary Tree (Contd.)

    1. Locate the position of the node to

    be deleted. Mark it ascurrentNode.

    2. Locate the inorder successor ofcurrentNode. Mark it asInorder_suc and its parent asInorder_parent.

    3. Overwrite the data contained in

    currentNode with the datacontained in Inorder_suc.

    4. Delete the node markedInorder_suc. This node wouldhave at most one child and cantherefore be deleted by using thealgorithm for one of the following:

    a. Deleting a leaf node.

    b. Deleting a node with onechild.

    Data Structures and Algorithms

  • 8/2/2019 11 DS and Algorithm Session 16

    49/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    30 50 80

    45

    72

    69

    40. ..

    65.

    .

    header node

    .

    .

    Delete node 40

    currentNode

    Inorder_suc

    Inorder_parent

    Deleting a Node from a Threaded Binary Tree (Contd.)

    1. Locate the position of the node to

    be deleted. Mark it ascurrentNode.

    2. Locate the inorder successor ofcurrentNode. Mark it asInorder_suc and its parent asInorder_parent.

    3. Overwrite the data contained in

    currentNode with the datacontained in Inorder_suc.

    4. Delete the node markedInorder_suc. This node wouldhave at most one child and cantherefore be deleted by using thealgorithm for one of the following:

    a. Deleting a leaf node.

    b. Deleting a node with onechild.

    Data Structures and Algorithms

  • 8/2/2019 11 DS and Algorithm Session 16

    50/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    30 50 80

    45

    72

    69

    40. ..

    65.

    .

    header node

    .

    .

    Delete node 40

    currentNode

    Inorder_suc

    Inorder_parent

    Deleting a Node from a Threaded Binary Tree (Contd.)

    1. Locate the position of the node to

    be deleted. Mark it ascurrentNode.

    2. Locate the inorder successor ofcurrentNode. Mark it asInorder_suc and its parent asInorder_parent.

    3. Overwrite the data contained in

    currentNode with the datacontained in Inorder_suc.

    4. Delete the node markedInorder_suc. This node wouldhave at most one child and cantherefore be deleted by using thealgorithm for one of the following:

    a. Deleting a leaf node.

    b. Deleting a node with onechild.

    Data Structures and Algorithms

  • 8/2/2019 11 DS and Algorithm Session 16

    51/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    30 50 80

    45

    72

    69

    45. ..

    65.

    .

    header node

    .

    .

    Delete node 40

    currentNode

    Inorder_suc

    Inorder_parent

    Deleting a Node from a Threaded Binary Tree (Contd.)

    1. Locate the position of the node to

    be deleted. Mark it ascurrentNode.

    2. Locate the inorder successor ofcurrentNode. Mark it asInorder_suc and its parent asInorder_parent.

    3. Overwrite the data contained in

    currentNode with the datacontained in Inorder_suc.

    4. Delete the node markedInorder_suc. This node wouldhave at most one child and cantherefore be deleted by using thealgorithm for one of the following:

    a. Deleting a leaf node.

    b. Deleting a node with onechild.

    Data Structures and Algorithms

  • 8/2/2019 11 DS and Algorithm Session 16

    52/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    30 50 80

    45

    72

    69

    45. ..

    65.

    .

    header node

    .

    .

    Delete node 40

    currentNode

    Inorder_suc

    Inorder_parent

    Deleting a Node from a Threaded Binary Tree (Contd.)

    1. Locate the position of the node to

    be deleted. Mark it ascurrentNode.

    2. Locate the inorder successor ofcurrentNode. Mark it asInorder_suc and its parent asInorder_parent.

    3. Overwrite the data contained in

    currentNode with the datacontained in Inorder_suc.

    4. Delete the node markedInorder_suc. This node wouldhave at most one child and cantherefore be deleted by using thealgorithm for one of the following:

    a. Deleting a leaf node.

    b. Deleting a node with onechild.

    Data Structures and Algorithms

  • 8/2/2019 11 DS and Algorithm Session 16

    53/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    30 50 80

    45

    72

    69

    45. ..

    65.

    .

    header node

    .

    .

    Delete node 40

    currentNode

    Inorder_suc

    Inorder_parent

    Deleting a Node from a Threaded Binary Tree (Contd.)

    1. Locate the position of the node to

    be deleted. Mark it ascurrentNode.

    2. Locate the inorder successor ofcurrentNode. Mark it asInorder_suc and its parent asInorder_parent.

    3. Overwrite the data contained in

    currentNode with the datacontained in Inorder_suc.

    4. Delete the node markedInorder_suc. This node wouldhave at most one child and cantherefore be deleted by using thealgorithm for one of the following:

    a. Deleting a leaf node.

    b. Deleting a node with onechild.

    Data Structures and Algorithms

  • 8/2/2019 11 DS and Algorithm Session 16

    54/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    30 50 80

    45

    72

    69

    45. ..

    65.

    .

    header node

    .

    Delete node 40

    currentNode

    Inorder_suc

    Inorder_parent

    Deletion complete

    Deleting a Node from a Threaded Binary Tree (Contd.)

    1. Locate the position of the node to

    be deleted. Mark it ascurrentNode.

    2. Locate the inorder successor ofcurrentNode. Mark it asInorder_suc and its parent asInorder_parent.

    3. Overwrite the data contained in

    currentNode with the datacontained in Inorder_suc.

    4. Delete the node markedInorder_suc. This node wouldhave at most one child and cantherefore be deleted by using thealgorithm for one of the following:

    a. Deleting a leaf node.

    b. Deleting a node with onechild.

    Data Structures and Algorithms

  • 8/2/2019 11 DS and Algorithm Session 16

    55/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    Implementing a Height Balanced Tree

    In a binary search tree, the time required to search for aparticular value depends upon its height.

    The shorter the height, the faster is the search.

    However, binary search trees tend to attain large heightsbecause of continuous insert and delete operations.

    Data Structures and Algorithms

  • 8/2/2019 11 DS and Algorithm Session 16

    56/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    After inserting values in the specified order, the binarysearch tree appears as follows:Consider an example in which you want to insert somenumeric values in a binary search tree in the following order:

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

    Defining Height Balanced Tree

    12

    34

    56

    78

    910

    1112

    1314

    15

    Data Structures and Algorithms

  • 8/2/2019 11 DS and Algorithm Session 16

    57/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    As a result, the binary search tree attains a linear structure.

    In this case, the height of the binary search tree is 15.

    12

    34

    56

    78

    910

    1112

    1314

    15

    Now if you want to search for a value 14 in the given binarysearch tree, you will have to traverse all its preceding nodesbefore you reach node 14. In this case, you need to make14 comparisons.

    In such a case, the binary search tree becomes equivalentto a linked list.

    Defining Height Balanced Tree (Contd.)

    This process can be very time consuming if the number of valuesstored in a binary search tree is large.

    Data Structures and Algorithms

  • 8/2/2019 11 DS and Algorithm Session 16

    58/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    Therefore, such a structure loses its property of a binarysearch tree in which after every comparison, the searchoperations are reduced to half.

    12

    34

    56

    78

    910

    1112

    1314

    15

    Defining Height Balanced Tree (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 11 DS and Algorithm Session 16

    59/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    To solve this problem, it is desirable to keep the height ofthe tree to a minimum.

    Therefore, the following binary search tree can be modifiedto reduce its height.

    12

    34

    56

    78

    910

    1112

    1314

    15

    Defining Height Balanced Tree (Contd.)

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    Data Structures and Algorithms

  • 8/2/2019 11 DS and Algorithm Session 16

    60/129

    Data Structures and Algorithms

    Session 16Ver. 1.0

    The height of the binary search tree has now been reducedto 4.Now if you want to search for a value 14, you just need totraverse nodes 8 and 12, before you reach node 14.In this case, the total number of comparisons to be made tosearch for node 14 is three.

    This approach reduces the time to search for a particularvalue in a binary search tree.

    Defining Height Balanced Tree (Contd.)

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    Data Structures and Algorithms

  • 8/2/2019 11 DS and Algorithm Session 16

    61/129

    ata St uctu es a d go t s

    Session 16Ver. 1.0

    This can be implemented with the help of a height balancedtree.

    Defining Height Balanced Tree (Contd.)

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    Data Structures and Algorithms

  • 8/2/2019 11 DS and Algorithm Session 16

    62/129

    g

    Session 16Ver. 1.0

    A height balanced tree is a binary tree in which thedifference between the heights of the left subtree and rightsubtree of a node is not more than one.

    In a height balanced tree, each node has a Balance Factor(BF) associated with it.

    For the tree to be balanced, BF can have three values:

    0: A BF value of 0 indicates that the height of the left subtree ofa node is equal to the height of its right subtree.

    1: A BF value of 1 indicates that the height of the left subtree isgreater than the height of the right subtree by one. A node in

    this state is said to be left heavy.1: A BF value of 1 indicates that the height of the rightsubtree is greater than the height of the left subtree by one. Anode in this state is said to be right heavy.

    Defining Height Balanced Tree (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 11 DS and Algorithm Session 16

    63/129

    g

    Session 16Ver. 1.0

    50

    40

    0

    30

    0

    60

    -1

    -1

    50

    40

    0

    30

    0

    0

    50

    40

    1

    30

    0

    1

    010

    Balanced Binary Search Trees

    Defining Height Balanced Tree (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 11 DS and Algorithm Session 16

    64/129

    g

    Session 16Ver. 1.0

    After inserting a new node in a height balanced tree, thebalance factor of one or more nodes may attain a valueother than 1, 0, or 1.

    This makes the tree unbalanced.

    In such a case, you first need to locate the pivot node.

    A pivot node is the nearest ancestor of the newly insertednode, which has a balance factor other than 1, 0 or 1.

    To restore the balance, you need to perform appropriaterotations around the pivot node.

    Defining Height Balanced Tree (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 11 DS and Algorithm Session 16

    65/129

    g

    Session 16Ver. 1.0

    50

    40

    0

    30

    1

    60

    -2

    -2

    50

    40

    2

    30

    0

    1

    55

    0

    35

    1

    34

    0

    55

    0

    45

    0

    Unbalanced Binary Search Trees

    Defining Height Balanced Tree (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 11 DS and Algorithm Session 16

    66/129

    g

    Session 16Ver. 1.0

    In a height balanced tree, the maximum difference betweenthe height of left and right subtree of a node canbe _________.

    Just a minute

    Answer:

    One

    Data Structures and Algorithms

  • 8/2/2019 11 DS and Algorithm Session 16

    67/129

    g

    Session 16Ver. 1.0

    Inserting Nodes in a Height Balanced Tree

    Insert operation in a height balanced tree is similar to that ina simple binary search tree.

    However, inserting a node can make the tree unbalanced.

    To restore the balance, you need to perform appropriaterotations around the pivot node.

    This involves two cases:

    When the pivot node is initially right heavy and the new node isinserted in the right subtree of the pivot node.

    When the pivot node is initiallyleft heavy and the new node isinserted in the left subtree of the pivot node.

    Data Structures and Algorithms

  • 8/2/2019 11 DS and Algorithm Session 16

    68/129

    g

    Session 16Ver. 1.0

    Let us first consider a case in which the pivot node is initiallyright heavy and the new node is inserted in the right subtreeof the pivot node.

    In this case, after the insertion of a new node, the balancefactor of pivot node becomes 2.

    Now there can be two situations in this case:

    If a new node is inserted in the right subtree of the right child ofthe pivot node.

    If the new node is inserted in the left subtree of the right childof the pivot node.

    Inserting Nodes in a Height Balanced Tree (Contd.)

  • 8/2/2019 11 DS and Algorithm Session 16

    69/129

    Data Structures and Algorithms

  • 8/2/2019 11 DS and Algorithm Session 16

    70/129

    Session 16Ver. 1.0

    After insertion of a node, the binary tree appears as:

    Inserting Nodes in a Height Balanced Tree (Contd.)

    The tree now becomes unbalanced.

    P

    X

    0

    Pl

    -1

    Xl Xr

    -1

    -2

    Data Structures and Algorithms

  • 8/2/2019 11 DS and Algorithm Session 16

    71/129

    Session 16Ver. 1.0

    To restore the balance, you need to perform a single leftrotation around the pivot node.

    Xr

    P

    X

    Xl

    Pl

    Xr

    0

    -1

    Xr

    X

    P

    XlPl Xr

    After rotation

    The tree is now balanced.

    Inserting Nodes in a Height Balanced Tree (Contd.)

    -1

    -2

    Data Structures and Algorithms

  • 8/2/2019 11 DS and Algorithm Session 16

    72/129

    Session 16Ver. 1.0

    In the second case, a new node is inserted in the leftsubtree of X.

    In this case, the binary tree initially appears as:

    P

    -1

    Yl Yr

    Pl

    X

    0

    0

    Y

    Xr

    Inserting Nodes in a Height Balanced Tree (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 11 DS and Algorithm Session 16

    73/129

    Session 16Ver. 1.0

    When a new node is inserted in the left subtree of X, thebinary tree becomes unbalanced.

    P

    -1

    Yl Yr

    Pl

    X0

    Y

    Xr

    Yl

    -2

    -10

    1

    Inserting Nodes in a Height Balanced Tree (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 11 DS and Algorithm Session 16

    74/129

    Session 16Ver. 1.0

    To restore the balance, you need to perform a doublerotation.In the first rotation, the subtree with root X is rotated right insuch a way so that the right child of Y now points to X.

    Inserting Nodes in a Height Balanced Tree (Contd.)

    P

    Yr

    Pl

    X

    Y

    Xr

    -1

    Yl

    1

    -2

    Data Structures and Algorithms

  • 8/2/2019 11 DS and Algorithm Session 16

    75/129

    Session 16Ver. 1.0

    P

    Yr

    Pl

    X

    Y

    Xr

    -1

    Yl

    1

    -2

    P

    -2

    Yl

    Yr

    Pl

    X-1

    -1

    Y

    Xr

    After single right rotation

    Inserting Nodes in a Height Balanced Tree (Contd.)

    In the first rotation, the subtree with root X is rotated right insuch a way so that the right child of Y now points to X.

    Data Structures and Algorithms

  • 8/2/2019 11 DS and Algorithm Session 16

    76/129

    Session 16Ver. 1.0

    P

    Yr

    Pl

    X

    Y

    Xr

    -1

    Yl

    1

    -2

    In the second rotation, the subtree with root P is rotated leftso that Y is moved upwards to form the root.

    The left child of Y now becomes the right child of P.

    P

    -2

    Yl

    Yr

    Pl

    X-1

    -1

    Y

    Xr

    After single right rotation

    Inserting Nodes in a Height Balanced Tree (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 11 DS and Algorithm Session 16

    77/129

    Session 16Ver. 1.0

    In the second rotation, the subtree with root P is rotated leftso that Y is moved upwards to form the root.

    The left child of Y now points to P.

    P

    -2

    Yl

    Yr

    Pl

    X-1

    -1

    Y

    Xr

    P

    0

    Yl YrPl

    X-1

    0

    Y

    Xr

    After single left rotation

    The tree is now balanced.

    Inserting Nodes in a Height Balanced Tree (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 11 DS and Algorithm Session 16

    78/129

    Session 16Ver. 1.0

    Let us now consider another case in which the pivot node isleft heavy and the new node is inserted in the left subtree ofthe pivot node.

    This further involves two cases:

    If a new node is inserted in the left subtree of the left child of

    the pivot node.If the new node is inserted in the right subtree of the left childof the pivot node.

    Inserting Nodes in a Height Balanced Tree (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 11 DS and Algorithm Session 16

    79/129

    Session 16Ver. 1.0

    Let us first consider the case in which the new node isinserted in the left subtree of the left child of the pivot node.

    Initially, the binary tree appears as:

    P

    X

    0

    1

    Xr

    Pr

    Xl

    Inserting Nodes in a Height Balanced Tree (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 11 DS and Algorithm Session 16

    80/129

    Session 16Ver. 1.0

    When a new node is inserted in the left subtree of the leftchild of the pivot node (P), the tree becomes unbalanced.

    P

    X

    0

    1

    Xr

    Pr

    Xl

    1

    2

    Inserting Nodes in a Height Balanced Tree (Contd.)

    Xl

    Data Structures and Algorithms

  • 8/2/2019 11 DS and Algorithm Session 16

    81/129

    Session 16Ver. 1.0

    To restore the balance, you need to perform a single rightrotation.

    P

    X

    Xr

    Pr

    2

    1

    Inserting Nodes in a Height Balanced Tree (Contd.)

    Xl

    Data Structures and Algorithms

  • 8/2/2019 11 DS and Algorithm Session 16

    82/129

    Session 16Ver. 1.0

    P

    X

    Xr

    Pr

    Xl

    2

    1

    P

    X

    0

    0

    Xr Pr

    Xl

    After single right rotation

    The tree is now balanced.

    Inserting Nodes in a Height Balanced Tree (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 11 DS and Algorithm Session 16

    83/129

    Session 16Ver. 1.0

    Let us now consider a case in which the new node is insertedin the right subtree of the left child of the pivot node.

    Initially, the tree appears as:

    P

    X

    1

    Yl Yr

    Xl

    YPr

    0

    0

    Inserting Nodes in a Height Balanced Tree (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 11 DS and Algorithm Session 16

    84/129

    Session 16Ver. 1.0

    When a new node is inserted in the right subtree of the leftchild of pivot node, P, the tree becomes unbalanced.

    P

    X0

    1

    Yl

    Xl

    YPr

    0

    YrYr

    2

    -1

    -1

    Inserting Nodes in a Height Balanced Tree (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 11 DS and Algorithm Session 16

    85/129

    Session 16Ver. 1.0

    In this case, a double rotation is required to restore thebalance.

    P

    X

    Yl

    Xl

    YPr

    Yr

    2

    -1

    -1

    Inserting Nodes in a Height Balanced Tree (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 11 DS and Algorithm Session 16

    86/129

    Session 16Ver. 1.0

    A left rotation is performed first in which the subtree withroot X is rotated towards left.

    P

    X

    Yl

    Xl

    YPr

    Yr

    2

    -1

    -1

    Inserting Nodes in a Height Balanced Tree (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 11 DS and Algorithm Session 16

    87/129

    Session 16Ver. 1.0

    A left rotation is performed first in which the subtree withroot X is rotated towards left.

    P

    X

    Yl

    Xl

    YPr

    Yr

    2

    -1

    -1

    P

    X

    -1

    2

    Yl

    Yr

    Xl

    Y

    Pr

    -1

    After single left rotation

    Inserting Nodes in a Height Balanced Tree (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 11 DS and Algorithm Session 16

    88/129

    Session 16Ver. 1.0

    In the next step, a single right rotation is performed in whichthe subtree with root P is rotated right.

    P

    X

    Yl

    Xl

    YPr

    Yr

    2

    -1

    -1

    P

    X

    -1

    2

    Yl

    Yr

    Xl

    Y

    Pr

    -1

    After single left rotation

    Inserting Nodes in a Height Balanced Tree (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 11 DS and Algorithm Session 16

    89/129

    Session 16Ver. 1.0

    In the next step, a single right rotation is performed in whichthe subtree with root P is rotated right.

    P

    X

    -1

    2

    Yl

    Yr

    Xl

    Y

    Pr

    -1

    After single right rotation

    PX

    0

    0

    YlYrXl

    Y

    1

    Pr

    The tree is now balanced.

    Inserting Nodes in a Height Balanced Tree (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 11 DS and Algorithm Session 16

    90/129

    Session 16Ver. 1.0

    Let us consider an example to insert values in a binarysearch tree and restore its balance whenever required.50 40 30 60 55 80 10 35 32

    Inserting Nodes in a Height Balanced Tree (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 11 DS and Algorithm Session 16

    91/129

    Session 16Ver. 1.0

    50 40 30 60 55 80 10 35 32

    Insert 50

    0

    50

    Tree is balanced

    Inserting Nodes in a Height Balanced Tree (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 11 DS and Algorithm Session 16

    92/129

    Session 16Ver. 1.0

    50 40 30 60 55 80 10 35 32

    Insert 40

    0

    50

    Tree is balanced

    0

    40

    1

    Inserting Nodes in a Height Balanced Tree (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 11 DS and Algorithm Session 16

    93/129

    Session 16Ver. 1.0

    50 40 30 60 55 80 10 35 32

    Insert 30

    50

    Tree is balanced

    0

    40

    1

    0

    30

    2

    1

    Tree becomes

    unbalanced

    A single right rotation restores the balance

    Inserting Nodes in a Height Balanced Tree (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 11 DS and Algorithm Session 16

    94/129

    Session 16Ver. 1.0

    50 40 30 60 55 80 10 35 32

    Insert 30

    50

    40

    030

    0

    0

    Tree is now

    balanced

    Inserting Nodes in a Height Balanced Tree (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 11 DS and Algorithm Session 16

    95/129

    Session 16Ver. 1.0

    50 40 30 60 55 80 10 35 32

    Insert 60

    50

    40

    030

    0

    0

    Tree is now

    balanced

    0

    60

    -1

    -1

    Tree is balanced

    Inserting Nodes in a Height Balanced Tree (Contd.)

    Data Structures and Algorithms

  • 8/2/2019 11 DS and Algorithm Session 16

    96/129

    Session 16Ver. 1.0

    50 40 30 60 55 80 10 35 32

    Insert 55

    50

    40

    030

    0

    60

    Tree is balanced

    -1

    -1

    0

    55

    -2

    -2

    1

    Tree becomes

    unbalanced

    A double rotation restores the balance

    Inserting Nodes in a Height Balanced Tree (Contd.)

    Data Structures and Algorithms

    i d i i h l d ( d )

  • 8/2/2019 11 DS and Algorithm Session 16

    97/129

    Session 16Ver. 1.0

    50 40 30 60 55 80 10 35 32

    Insert 55

    50

    40

    030

    60

    0

    55

    A double rotation restores the balance

    Tree becomes

    unbalanced

    1

    -2

    -2

    Inserting Nodes in a Height Balanced Tree (Contd.)

    Data Structures and Algorithms

    I i N d i H i h B l d T (C d )

  • 8/2/2019 11 DS and Algorithm Session 16

    98/129

    Session 16Ver. 1.0

    50 40 30 60 55 80 10 35 32

    50

    40

    030

    55

    0

    60

    A double rotation restores the balance

    Tree becomes

    unbalanced

    -1

    -2

    -2

    60

    0

    55

    1

    Inserting Nodes in a Height Balanced Tree (Contd.)

    Data Structures and Algorithms

    I ti N d i H i ht B l d T (C td )

  • 8/2/2019 11 DS and Algorithm Session 16

    99/129

    Session 16Ver. 1.0

    50 40 30 60 55 80 10 35 32

    55

    40

    030

    60

    0

    50

    A double rotation restores the balance

    Tree becomes

    unbalanced

    -1

    -2

    -2

    0

    0

    -1

    0

    60

    Tree is now

    balanced

    Inserting Nodes in a Height Balanced Tree (Contd.)

    Data Structures and Algorithms

    I ti N d i H i ht B l d T (C td )

  • 8/2/2019 11 DS and Algorithm Session 16

    100/129

    Session 16Ver. 1.0

    50 40 30 60 55 80 10 35 32

    Insert 80

    55

    40

    030

    60

    0

    50

    Tree is now

    balanced

    0

    0

    -1

    80

    0

    -1

    -1

    Tree becomes

    unbalanced

    A single left rotation restores the balance

    Inserting Nodes in a Height Balanced Tree (Contd.)

    -2

    Data Structures and Algorithms

    I ti N d i H i ht B l d T (C td )

  • 8/2/2019 11 DS and Algorithm Session 16

    101/129

    Session 16Ver. 1.0

    50 40 30 60 55 80 10 35 32

    60

    55

    040

    8050

    0

    -1

    0

    30

    0 0

    A single left rotation restores the balance

    Tree is now

    balanced

    55

    40

    030

    60

    0

    50

    80

    0

    -1

    -1

    Inserting Nodes in a Height Balanced Tree (Contd.)

    -2

  • 8/2/2019 11 DS and Algorithm Session 16

    102/129

    Data Structures and Algorithms

    Inse ting Nodes in a Height Balanced T ee (Contd )

  • 8/2/2019 11 DS and Algorithm Session 16

    103/129

    Session 16Ver. 1.0

    50 40 30 60 55 80 10 35 32

    60

    55

    40

    8050

    0

    -1

    30

    0

    Insert 35

    10

    0

    Tree is balanced

    1

    1

    1

    35

    0

    0

    Inserting Nodes in a Height Balanced Tree (Contd.)

    Data Structures and Algorithms

    Inserting Nodes in a Height Balanced Tree (Contd )

  • 8/2/2019 11 DS and Algorithm Session 16

    104/129

    Session 16Ver. 1.0

    50 40 30 60 55 80 10 35 32

    60

    55

    40

    8050

    0

    -1

    30

    0

    Insert 32

    10

    0

    Tree is balanced

    1

    1

    35

    0

    0

    32

    0

    1

    -1

    2

    2

    Tree is becomes

    unbalanced

    A double rotation is required torestore the balance

    Inserting Nodes in a Height Balanced Tree (Contd.)

    Data Structures and Algorithms

    Inserting Nodes in a Height Balanced Tree (Contd )

  • 8/2/2019 11 DS and Algorithm Session 16

    105/129

    Session 16Ver. 1.0

    50 40 30 60 55 80 10 35 32

    60

    55

    40

    8050

    0

    -1

    30

    0

    Insert 32

    10

    0

    35

    32

    0

    A double rotation is required torestore the balance

    1

    -1

    2

    2

    Tree is becomesunbalanced

    Inserting Nodes in a Height Balanced Tree (Contd.)

    Data Structures and Algorithms

    Inserting Nodes in a Height Balanced Tree (Contd )

  • 8/2/2019 11 DS and Algorithm Session 16

    106/129

    Session 16Ver. 1.0

    50 40 30 60 55 80 10 35 32

    60

    55

    40

    8050

    0

    -1

    35

    0

    30

    0

    35

    32

    0

    A double rotation is required torestore the balance

    1

    -1

    2

    2

    10

    0

    2

    Inserting Nodes in a Height Balanced Tree (Contd.)

    Data Structures and Algorithms

    Inserting Nodes in a Height Balanced Tree (Contd )

  • 8/2/2019 11 DS and Algorithm Session 16

    107/129

    Session 16Ver. 1.0

    50 40 30 60 55 80 10 35 32

    60

    55

    35

    8040

    0

    -1

    30

    -1

    10

    0

    32

    A double rotation is required torestore the balance

    0

    0

    0

    50

    00

    Tree is nowbalanced

    Inserting Nodes in a Height Balanced Tree (Contd.)

    Data Structures and Algorithms

    Just a minute

  • 8/2/2019 11 DS and Algorithm Session 16

    108/129

    Session 16Ver. 1.0

    In which situations do you need to implement a doublerotation to balance the tree?

    Just a minute

    Answer:

    There can be two situations where a double rotation is requiredto balance the tree:

    If the pivot node is right heavy and the right child of pivot node isleft heavy

    If the pivot node is left heavy and the left child of pivot node isright heavy

    Data Structures and Algorithms

    Deleting Nodes from a Height Balanced Tree

  • 8/2/2019 11 DS and Algorithm Session 16

    109/129

    Session 16Ver. 1.0

    Deleting Nodes from a Height Balanced Tree

    Delete operation in a height balanced tree is same as that ina normal binary tree.

    However, if deletion of a node makes the tree unbalanced,then you need to perform appropriate rotations to restorethe balance.

    This process involves the same concept as used in insertionof a node.

    Data Structures and Algorithms

    Deleting Nodes from a Height Balanced Tree (Contd )

  • 8/2/2019 11 DS and Algorithm Session 16

    110/129

    Session 16Ver. 1.0

    Consider the following height balanced binary tree.

    55

    40 70

    30 50 60 80

    10 35 53 90

    32

    1

    1 -1

    -1 -1

    0 1

    0

    0

    0 -1

    0

    Deleting Nodes from a Height Balanced Tree (Contd.)

    Data Structures and Algorithms

    Deleting Nodes from a Height Balanced Tree (Contd )

  • 8/2/2019 11 DS and Algorithm Session 16

    111/129

    Session 16Ver. 1.0

    Let us implement a few delete operations.

    55

    40 70

    30 50 60 80

    10 35 53 90

    32

    1

    1 -1

    -1 -1

    0 1

    0

    0

    0 -1

    0

    Deleting Nodes from a Height Balanced Tree (Contd.)

    Data Structures and Algorithms

    Deleting Nodes from a Height Balanced Tree (Contd )

  • 8/2/2019 11 DS and Algorithm Session 16

    112/129

    Session 16Ver. 1.0

    55

    40 70

    30 50 60 80

    10 35 53 90

    32

    1

    1 -1

    -1 -1

    0 1

    0

    0

    0 -1

    0

    2

    0

    Deleting Nodes from a Height Balanced Tree (Contd.)

    Delete 53

    Data Structures and Algorithms

    Deleting Nodes from a Height Balanced Tree (Contd )

  • 8/2/2019 11 DS and Algorithm Session 16

    113/129

    Session 16Ver. 1.0

    55

    40 70

    30 50 60 80

    10 35 90

    32

    1

    -1

    -1

    0 1

    0

    0 -1

    0

    2

    0

    Tree becomes unbalanced

    A double rotation is required to restore the balance

    Deleting Nodes from a Height Balanced Tree (Contd.)

    Data Structures and Algorithms

    Deleting Nodes from a Height Balanced Tree (Contd.)

  • 8/2/2019 11 DS and Algorithm Session 16

    114/129

    Session 16Ver. 1.0

    55

    40 70

    30 50 60 80

    10 35 90

    32

    1

    -1

    -1

    0 1

    0

    0 -1

    0

    2

    0

    Tree becomes unbalanced

    Rotate left around node 30

    Deleting Nodes from a Height Balanced Tree (Contd.)

    Data Structures and Algorithms

    Deleting Nodes from a Height Balanced Tree (Contd.)

  • 8/2/2019 11 DS and Algorithm Session 16

    115/129

    Session 16Ver. 1.0

    55

    40 70

    35 50 60 80

    30 90

    32

    1

    -1

    2

    0

    0

    0 -1

    0

    2

    0

    Rotate right around node 40

    10

    0

    Deleting Nodes from a Height Balanced Tree (Contd.)

    Data Structures and Algorithms

    Deleting Nodes from a Height Balanced Tree (Contd.)

  • 8/2/2019 11 DS and Algorithm Session 16

    116/129

    Session 16Ver. 1.0

    55

    35 70

    40 60 8030

    9032

    0

    -1

    0

    0

    0 -1

    0

    0

    -1

    Rotate right around node 40

    10

    0

    50

    0

    Tree is now balanced

    Deleting Nodes from a Height Balanced Tree (Contd.)

    Data Structures and Algorithms

    Deleting Nodes from a Height Balanced Tree (Contd.)

  • 8/2/2019 11 DS and Algorithm Session 16

    117/129

    Session 16Ver. 1.0

    55

    35 70

    40 60 8030

    9032

    0

    -1

    0

    0

    0 -1

    0

    0

    -1

    10

    0

    50

    0

    Tree becomes unbalancedDelete 60

    -2

    A single left rotation restores the balance

    Deleting Nodes from a Height Balanced Tree (Contd.)

    Data Structures and Algorithms

    Deleting Nodes from a Height Balanced Tree (Contd.)

  • 8/2/2019 11 DS and Algorithm Session 16

    118/129

    Session 16Ver. 1.0

    55

    35 80

    40 9030 70

    32

    1

    0

    0

    00

    0

    -1

    10

    0

    50

    0

    0

    Tree becomes balanced

    Deleting Nodes from a Height Balanced Tree (Contd.)

    Data Structures and Algorithms

    Deleting Nodes from a Height Balanced Tree (Contd.)

  • 8/2/2019 11 DS and Algorithm