SlideShare a Scribd company logo
1 of 129
Data Structures and Algorithms
Objectives


                In this session, you will learn to:
                   Implement a threaded binary tree
                   Implement a height balanced binary tree
                   Store data in a graph




     Ver. 1.0                                                Session 16
Data Structures and Algorithms
Deleting a Node from a Threaded Binary Tree


                Write an algorithm to locate thebinary to berefers to in a
                Delete operation in a threaded node tree deleted the
                threadedof removing the specified node from the threaded
                process binary tree.
                binary 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 marked
                as the currentNode and its parent as parent.




     Ver. 1.0                                                        Session 16
Data Structures and Algorithms
Deleting a Node from a Threaded Binary Tree (Contd.)
                                                           1.   Mark the left child of the header node as
                                                                currentNode.
         Delete 80                                         2.   Mark the header node as parent.
                   Algorithm       header node
                                  to locate the
                                           node            3.   Repeat steps a, b, c, d, and e until the node
                                                                to be searched is found or currentNode
                   to be deleted and its parent in              becomes NULL:
                                                                  a.   Mark currentNode as parent.
                   a threaded binary tree.                        b.   If the value to be searched is less
                                                                       than that of currentNode, and the left
                                                                       child of the currentNode is a link:
                                  . 65 .                                 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 left
                . 40 .                           . 72 .                child 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 the
                                                                       right child of the currentNode is a link:
    30                   . 50 .             69            80
                                                                         i.    Make currentNode point to its
                                                                               right child and go to step 3.
                                                                  e.   If the value to be searched is greater
                                                                       than that of currentNode, and the
                                                                       right child of the currentNode is a
                                                                       thread:
                                                                         i.    Mark currentNode as NULL
                                       60                                      and go to step 3.


     Ver. 1.0                                                                                    Session 16
Data Structures and Algorithms
Deleting a Node from a Threaded Binary Tree (Contd.)
                                                           •   Mark the left child of the header node as
                                                               currentNode.
                                                           •   Mark the header node as parent.
         Delete 80
                                   header node             •   Repeat steps a, b, c, d, and e until the node
                                                               to be searched is found or currentNode
                                                               becomes NULL:
                                                                 a.   Mark currentNode as parent.
                                                                 b.   If the value to be searched is less
                                                                      than that of currentNode, and the left
                                                                      child of the currentNode is a link:
                currentNode       . 65 .                                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 left
                . 40 .                           . 72 .               child 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 the
                                                                      right child of the currentNode is a link:
    30                   . 50 .             69            80
                                                                        i.    Make currentNode point to its
                                                                              right child and go to step 3.
                                                                 e.   If the value to be searched is greater
                                                                      than that of currentNode, and the
                                                                      right child of the currentNode is a
                                                                      thread:
                                                                        i.    Mark currentNode as NULL
                                       60                                     and go to step 3.


     Ver. 1.0                                                                                   Session 16
Data Structures and Algorithms
Deleting a Node from a Threaded Binary Tree (Contd.)
                                                           •   Mark the left child of the header node as
                                                               currentNode.
                                                           •   Mark the header node as parent.
         Delete 80
                           parent header node
                                                           •   Repeat steps a, b, c, d, and e until the node
                                                               to be searched is found or currentNode
                                                               becomes NULL:
                                                                 a.   Mark currentNode as parent.
                                                                 b.   If the value to be searched is less
                                                                      than that of currentNode, and the left
                                                                      child of the currentNode is a link:
                currentNode       . 65 .                                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 left
                . 40 .                           . 72 .               child 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 the
                                                                      right child of the currentNode is a link:
    30                   . 50 .             69            80
                                                                        i.    Make currentNode point to its
                                                                              right child and go to step 3.
                                                                 e.   If the value to be searched is greater
                                                                      than that of currentNode, and the
                                                                      right child of the currentNode is a
                                                                      thread:
                                                                        i.    Mark currentNode as NULL
                                       60                                     and go to step 3.


     Ver. 1.0                                                                                   Session 16
Data Structures and Algorithms
Deleting a Node from a Threaded Binary Tree (Contd.)
                                                           •   Mark the left child of the header node as
                                                               currentNode.
                                                           •   Mark the header node as parent.
         Delete 80
                           parent header node
                                                           •   Repeat steps a, b, c, d, and e until the node
                                                               to be searched is found or currentNode
                                                               becomes NULL:
                                                                 a.   Mark currentNode as parent.
                                                                 b.   If the value to be searched is less
                                                                      than that of currentNode, and the left
                                                                      child of the currentNode is a link:
                currentNode       . 65 .                                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 left
                . 40 .                           . 72 .               child 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 the
                                                                      right child of the currentNode is a link:
    30                   . 50 .             69            80
                                                                        i.    Make currentNode point to its
                                                                              right child and go to step 3.
                                                                 e.   If the value to be searched is greater
                                                                      than that of currentNode, and the
                                                                      right child of the currentNode is a
                                                                      thread:
                                                                        i.    Mark currentNode as NULL
                                       60                                     and go to step 3.


     Ver. 1.0                                                                                   Session 16
Data Structures and Algorithms
Deleting a Node from a Threaded Binary Tree (Contd.)
                                                           1.   Mark the left child of the header node as
                                                                currentNode.
         Delete 80                                         2.   Mark the header node as parent.
                           parent header node              3.   Repeat steps a, b, c, d, and e until the node
                                                                to be searched is found or currentNode
                                                                becomes NULL:
                                                                  •    Mark currentNode as parent.
                                                                  •    If the value to be searched is less
                                                                       than that of currentNode, and the left
                                                                       child of the currentNode is a link:
                currentNode       . 65 .     parent                      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 left
                . 40 .                           . 72 .                child 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 the
                                                                       right child of the currentNode is a link:
    30                   . 50 .             69            80
                                                                         i.    Make currentNode point to its
                                                                               right child and go to step 3.
                                                                  e.   If the value to be searched is greater
                                                                       than that of currentNode, and the
                                                                       right child of the currentNode is a
                                                                       thread:
                                                                         i.    Mark currentNode as NULL
                                       60                                      and go to step 3.


     Ver. 1.0                                                                                    Session 16
Data Structures and Algorithms
Deleting a Node from a Threaded Binary Tree (Contd.)
                                                           1.   Mark the left child of the header node as
                                                                currentNode.
         Delete 80                                         2.   Mark the header node as parent.
                                   header node             3.   Repeat steps a, b, c, d, and e until the node
                                                                to be searched is found or currentNode
                                                                becomes NULL:
                                                                  •    Mark currentNode as parent.
                                                                  •    If the value to be searched is less
                                                                       than that of currentNode, and the left
                                                                       child of the currentNode is a link:
                currentNode       . 65 .     parent                      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 left
                . 40 .                           . 72 .                child 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 the
                                                                       right child of the currentNode is a link:
    30                   . 50 .             69            80
                                                                         i.    Make currentNode point to its
                                                                               right child and go to step 3.
                                                                  e.   If the value to be searched is greater
                                                                       than that of currentNode, and the
                                                                       right child of the currentNode is a
                                                                       thread:
                                                                         i.    Mark currentNode as NULL
                                       60                                      and go to step 3.


     Ver. 1.0                                                                                    Session 16
Data Structures and Algorithms
Deleting a Node from a Threaded Binary Tree (Contd.)
                                                           1.   Mark the left child of the header node as
                                                                currentNode.
         Delete 80                                         2.   Mark the header node as parent.
                                   header node             3.   Repeat steps a, b, c, d, and e until the node
                                                                to be searched is found or currentNode
                                                                becomes NULL:
                                                                  a.   Mark currentNode as parent.
                                                                  b.   If the value to be searched is less
                                                                       than that of currentNode, and the left
                                                                       child of the currentNode is a link:
                currentNode       . 65 .     parent                      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 left
                . 40 .                           . 72 .                child 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 the
                                                                       right child of the currentNode is a link:
    30                   . 50 .             69            80
                                                                         i.    Make currentNode point to its
                                                                               right child and go to step 3.
                                                                  e.   If the value to be searched is greater
                                                                       than that of currentNode, and the
                                                                       right child of the currentNode is a
                                                                       thread:
                                                                         i.    Mark currentNode as NULL
                                       60                                      and go to step 3.


     Ver. 1.0                                                                                    Session 16
Data Structures and Algorithms
Deleting a Node from a Threaded Binary Tree (Contd.)
                                                           •   Mark the left child of the header node as
                                                               currentNode.
                                                           •   Mark the header node as parent.
         Delete 80
                                   header node             •   Repeat steps a, b, c, d, and e until the node
                                                               to be searched is found or currentNode
                                                               becomes NULL:
                                                                 a.   Mark currentNode as parent.
                                                                 b.   If the value to be searched is less
                                                                      than that of currentNode, and the left
                                                                      child of the currentNode is a link:
                currentNode       . 65 .     parent                     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 left
                . 40 .                           . 72 .               child 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 the
                                                                      right child of the currentNode is a link:
    30                   . 50 .             69            80
                                                                        i.    Make currentNode point to its
                                                                              right child and go to step 3.
                                                                 e.   If the value to be searched is greater
                                                                      than that of currentNode, and the
                                                                      right child of the currentNode is a
                                                                      thread:
                                                                        i.    Mark currentNode as NULL
                                       60                                     and go to step 3.


     Ver. 1.0                                                                                   Session 16
Data Structures and Algorithms
Deleting a Node from a Threaded Binary Tree (Contd.)
                                                               1.   Mark the left child of the header node as
                                                                    currentNode.
         Delete 80                                             2.   Mark the header node as parent.
                                   header node                 3.   Repeat steps a, b, c, d, and e until the node
                                                                    to be searched is found or currentNode
                                                                    becomes NULL:
                                                                      a.   Mark currentNode as parent.
                                                                      b.   If the value to be searched is less
                                                                           than that of currentNode, and the left
                                                                           child of the currentNode is a link:
                currentNode       . 65 .     parent                          i.    Make currentNode point to its
                                                                                   left child and go to step 3.
                                                 currentNode          c.   If the value to be searched is less
                                                                           than that of currentNode, and the left
                . 40 .                            . 72 .                   child 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 the
                                                                           right child of the currentNode is a link:
    30                   . 50 .             69             80
                                                                             i.    Make currentNode point to its
                                                                                   right child and go to step 3.
                                                                      e.   If the value to be searched is greater
                                                                           than that of currentNode, and the
                                                                           right child of the currentNode is a
                                                                           thread:
                                                                             i.    Mark currentNode as NULL
                                       60                                          and go to step 3.


     Ver. 1.0                                                                                        Session 16
Data Structures and Algorithms
Deleting a Node from a Threaded Binary Tree (Contd.)
                                                               •   Mark the left child of the header node as
                                                                   currentNode.
                                                               •   Mark the header node as parent.
         Delete 80
                                   header node                 •   Repeat steps a, b, c, d, and e until the node
                                                                   to be searched is found or currentNode
                                                                   becomes NULL:
                                                                     a.   Mark currentNode as parent.
                                                                     b.   If the value to be searched is less
                                                                          than that of currentNode, and the left
                                                                          child of the currentNode is a link:
                                  . 65 .     parent                         i.    Make currentNode point to its
                                                                                  left child and go to step 3.
                                                 currentNode         c.   If the value to be searched is less
                                                                          than that of currentNode, and the left
                . 40 .                            . 72 .                  child 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 the
                                                                          right child of the currentNode is a link:
    30                   . 50 .             69             80
                                                                            i.    Make currentNode point to its
                                                                                  right child and go to step 3.
                                                                     e.   If the value to be searched is greater
                                                                          than that of currentNode, and the
                                                                          right child of the currentNode is a
                                                                          thread:
                                                                            i.    Mark currentNode as NULL
                                       60                                         and go to step 3.


     Ver. 1.0                                                                                       Session 16
Data Structures and Algorithms
Deleting a Node from a Threaded Binary Tree (Contd.)
                                                               1.   Mark the left child of the header node as
                                                                    currentNode.
         Delete 80                                             2.   Mark the header node as parent.
                                   header node                 3.   Repeat steps a, b, c, d, and e until the node
                                                                    to be searched is found or currentNode
                                                                    becomes NULL:
                                                                      •    Mark currentNode as parent.
                                                                      •    If the value to be searched is less
                                                                           than that of currentNode, and the left
                                                                           child of the currentNode is a link:
                                  . 65 .     parent                          i.    Make currentNode point to its
                                                                                   left child and go to step 3.
                                                 currentNode          c.   If the value to be searched is less
                                                                           than that of currentNode, and the left
                . 40 .                 parent     . 72 .                   child 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 the
                                                                           right child of the currentNode is a link:
    30                   . 50 .             69             80
                                                                             i.    Make currentNode point to its
                                                                                   right child and go to step 3.
                                                                      e.   If the value to be searched is greater
                                                                           than that of currentNode, and the
                                                                           right child of the currentNode is a
                                                                           thread:
                                                                             i.    Mark currentNode as NULL
                                       60                                          and go to step 3.


     Ver. 1.0                                                                                        Session 16
Data Structures and Algorithms
Deleting a Node from a Threaded Binary Tree (Contd.)
                                                               1.   Mark the left child of the header node as
                                                                    currentNode.
         Delete 80                                             2.   Mark the header node as parent.
                                   header node                 3.   Repeat steps a, b, c, d, and e until the node
                                                                    to be searched is found or currentNode
                                                                    becomes NULL:
                                                                      •    Mark currentNode as parent.
                                                                      •    If the value to be searched is less
                                                                           than that of currentNode, and the left
                                                                           child of the currentNode is a link:
                                  . 65 .                                     i.    Make currentNode point to its
                                                                                   left child and go to step 3.
                                                 currentNode          c.   If the value to be searched is less
                                                                           than that of currentNode, and the left
                . 40 .                 parent     . 72 .                   child 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 the
                                                                           right child of the currentNode is a link:
    30                   . 50 .             69             80
                                                                             i.    Make currentNode point to its
                                                                                   right child and go to step 3.
                                                                      e.   If the value to be searched is greater
                                                                           than that of currentNode, and the
                                                                           right child of the currentNode is a
                                                                           thread:
                                                                             i.    Mark currentNode as NULL
                                       60                                          and go to step 3.


     Ver. 1.0                                                                                        Session 16
Data Structures and Algorithms
Deleting a Node from a Threaded Binary Tree (Contd.)
                                                               1.   Mark the left child of the header node as
                                                                    currentNode.
         Delete 80                                             2.   Mark the header node as parent.
                                   header node                 3.   Repeat steps a, b, c, d, and e until the node
                                                                    to be searched is found or currentNode
                                                                    becomes NULL:
                                                                      a.   Mark currentNode as parent.
                                                                      b.   If the value to be searched is less
                                                                           than that of currentNode, and the left
                                                                           child of the currentNode is a link:
                                  . 65 .                                     i.    Make currentNode point to its
                                                                                   left child and go to step 3.
                                                 currentNode          c.   If the value to be searched is less
                                                                           than that of currentNode, and the left
                . 40 .                 parent     . 72 .                   child 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 the
                                                                           right child of the currentNode is a link:
    30                   . 50 .             69             80
                                                                             i.    Make currentNode point to its
                                                                                   right child and go to step 3.
                                                                      e.   If the value to be searched is greater
                                                                           than that of currentNode, and the
                                                                           right child of the currentNode is a
                                                                           thread:
                                                                             i.    Mark currentNode as NULL
                                       60                                          and go to step 3.


     Ver. 1.0                                                                                        Session 16
Data Structures and Algorithms
Deleting a Node from a Threaded Binary Tree (Contd.)
                                                               1.   Mark the left child of the header node as
                                                                    currentNode.
         Delete 80                                             2.   Mark the header node as parent.
                                   header node                 3.   Repeat steps a, b, c, d, and e until the node
                                                                    to be searched is found or currentNode
                                                                    becomes NULL:
                                                                      a.   Mark currentNode as parent.
                                                                      b.   If the value to be searched is less
                                                                           than that of currentNode, and the left
                                                                           child of the currentNode is a link:
                                  . 65 .                                     i.    Make currentNode point to its
                                                                                   left child and go to step 3.
                                                 currentNode          c.   If the value to be searched is less
                                                                           than that of currentNode, and the left
                . 40 .                 parent     . 72 .                   child 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 the
                                                                           right child of the currentNode is a link:
    30                   . 50 .             69             80
                                                                             i.    Make currentNode point to its
                                                                                   right child and go to step 3.
                                                                      e.   If the value to be searched is greater
                                                                           than that of currentNode, and the
                                                                           right child of the currentNode is a
                                                                           thread:
                                                                             i.    Mark currentNode as NULL
                                       60                                          and go to step 3.


     Ver. 1.0                                                                                        Session 16
Data Structures and Algorithms
Deleting a Node from a Threaded Binary Tree (Contd.)
                                                                    1.
                                                                  Mark the left child of the header node as
                                                                  currentNode.
         Delete 80                                             2. Mark the header node as parent.
                                      header node              3. Repeat steps a, b, c, d, and e until the node
                                                                  to be searched is found or currentNode
                                                                  becomes NULL:
                                                                    a.   Mark currentNode as parent.
                                                                    b.   If the value to be searched is less
                                                                         than that of currentNode, and the left
                                                                         child of the currentNode is a link:
                                  .   65  .                                i.    Make currentNode point to its
                                                                                 left child and go to step 3.
                                                  currentNode       c.   If the value to be searched is less
                                                                         than that of currentNode, and the left
                . 40 .                    parent      .
                                                     72     .            child 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 the
                                                                         right child of the currentNode is a link:
    30                   . 50 .               69            80
                                                                           i.    Make currentNode point to its
                                                                                 right child and go to step 3.
                                                                    e.   If the value to be searched is greater
                                                                         than that of currentNode, and the
                                                       currentNode       right child of the currentNode is a
                                                                         thread:
                                                                           i.    Mark currentNode as NULL
                                         60                                      and go to step 3.


     Ver. 1.0                                                                                        Session 16
Data Structures and Algorithms
Deleting a Node from a Threaded Binary Tree (Contd.)
                                                                 •Mark the left child of the header node as
                                                                  currentNode.
         Nodes located                                         •  Mark the header node as parent.
         Delete 80
                                   header node                 •  Repeat steps a, b, c, d, and e until the node
                                                                  to be searched is found or currentNode
                                                                  becomes NULL:
                                                                    a.   Mark currentNode as parent.
                                                                    b.   If the value to be searched is less
                                                                         than that of currentNode, and the left
                                                                         child of the currentNode is a link:
                                  . 65 .                                   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 left
                . 40 .                 parent    .   72  .               child 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 the
                                                                         right child of the currentNode is a link:
    30                   . 50 .             69              80
                                                                           i.    Make currentNode point to its
                                                                                 right child and go to step 3.
                                                                    e.   If the value to be searched is greater
                                                                         than that of currentNode, and the
                                                       currentNode       right child of the currentNode is a
                                                                         thread:
                                                                           i.    Mark currentNode as NULL
                                       60                                        and go to step 3.


     Ver. 1.0                                                                                        Session 16
Data Structures and Algorithms
Deleting a Node from a Threaded Binary Tree (Contd.)


                Once you if the tree is not empty, there can be three cases:
                Before implementingnode to be deleted and its parent, to
                However, locate the a delete operation, you first need you
                can Node to be deleted is isof the node to be deleted after
                     release the memory empty or
                check whether the tree a leaf node not.
                adjustingis empty if thethreads appropriately. node is a
                      tree the links and left child of the header
                The Node to be deleted has one child (left or right)
                thread pointingdeleted has two children
                     Node to be to itself.
                If the tree is empty, an error message is shown.
                             header node




     Ver. 1.0                                                         Session 16
Data Structures and Algorithms
Deleting a Node from a Threaded Binary Tree (Contd.)


                Let us first consider a case in which the node to be deleted
                is the leaf node.
                In this case, you first need to check if there is only one node
                present in the tree.



                                header node




                               65


                             new node




     Ver. 1.0                                                           Session 16
Data Structures and Algorithms
Deleting a Node from a Threaded Binary Tree (Contd.)


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




                                header node




                              65


                            new node




     Ver. 1.0                                                         Session 16
Data Structures and Algorithms
Deleting a Node from a Threaded Binary Tree (Contd.)


                To delete this node, make the left child of the header node
                as a thread pointing to itself.
                Now release the memory of the node to be deleted.




                                header node




                       Delete operation complete
                              65


                            new node




     Ver. 1.0                                                         Session 16
Data Structures and Algorithms
Deleting a Node from a Threaded Binary Tree (Contd.)
                                  header node                 1.   Locate the position of the node to
   Delete node 60                                                  be deleted. Mark it as
                 However, if there are more than                   currentNode and its parent as
                                                                   parent.
                 one nodes in the tree, you need
                                                              3.   If currentNode is the left child of
                 another algorithm to delete a leaf
                 node.      . 65 .                                 parent:
                                                                     a.    Set the left thread field of
                                                                           parent as zero.
                                                                     b.    Make the left child field of
                                                                           parent as a thread pointing
                . 40 .                          . 72 .                     to the inorder predecessor
                                                                           of currentNode.
                                                                     c.    Go to step 4.

                                                              4.   If currentNode is the right child of
                                                                   parent:
      30                 . 50 .           69             80          a.    Set the right thread field of
                                                                           parent as zero.
                                                                     b.    Make the right child field of
                                                                           parent as a thread pointing
                                                                           to the inorder successor of
                                                                           currentNode.

                                     60                       5.   Release the memory for
                                                                   currentNode.



     Ver. 1.0                                                                             Session 16
Data Structures and Algorithms
Deleting a Node from a Threaded Binary Tree (Contd.)
                                   header node                 •    Locate the position of the node to
   Delete node 60                                                   be deleted. Mark it as
                                                                    currentNode and its parent as
                                                                    parent.

                                                               •    If currentNode is the left child of

                                  . 65 .                            parent:
                                                                      a.    Set the left thread field of
                                                                            parent as zero.
                                                                      b.    Make the left child field of
                                                                            parent as a thread pointing
                . 40 .                           . 72 .                     to the inorder predecessor
                                                                            of currentNode.
                                                                      c.    Go to step 4.

                                                               4.   If currentNode is the right child of
                                                                    parent:
      30                 . 50 .             69            80          a.    Set the right thread field of
                                                                            parent as zero.
                                                                      b.    Make the right child field of
                    parent                                                  parent as a thread pointing
                                                                            to the inorder successor of
                                                                            currentNode.

                                       60                      5.   Release the memory for
                                                                    currentNode.

                                  currentNode

     Ver. 1.0                                                                              Session 16
Data Structures and Algorithms
Deleting a Node from a Threaded Binary Tree (Contd.)
                                   header node                 •    Locate the position of the node to
   Delete node 60                                                   be deleted. Mark it as
                                                                    currentNode and its parent as
                                                                    parent.

                                                               •    If currentNode is the left child of

                                  . 65 .                            parent:
                                                                      a.    Set the left thread field of
                                                                            parent as zero.
                                                                      b.    Make the left child field of
                                                                            parent as a thread pointing
                . 40 .                           . 72 .                     to the inorder predecessor
                                                                            of currentNode.
                                                                      c.    Go to step 4.

                                                               4.   If currentNode is the right child of
                                                                    parent:
      30                 . 50 .             69            80          a.    Set the right thread field of
                                                                            parent as zero.
                                                                      b.    Make the right child field of
                    parent                                                  parent as a thread pointing
                                                                            to the inorder successor of
                                                                            currentNode.

                                       60                      5.   Release the memory for
                                                                    currentNode.

                                  currentNode

     Ver. 1.0                                                                              Session 16
Data Structures and Algorithms
Deleting a Node from a Threaded Binary Tree (Contd.)
                                   header node                 1.   Locate the position of the node to
   Delete node 60                                                   be deleted. Mark it as
                                                                    currentNode and its parent as
                                                                    parent.

                                                               3.   If currentNode is the left child of

                                  . 65 .                            parent:
                                                                      a.    Set the left thread field of
                                                                            parent as zero.
                                                                      b.    Make the left child field of
                                                                            parent as a thread pointing
                . 40 .                           . 72 .                     to the inorder predecessor
                                                                            of currentNode.
                                                                      c.    Go to step 4.

                                                               4.   If currentNode is the right child of
                                                                    parent:
      30                 . 50 .             69            80          a.    Set the right thread field of
                                                                            parent as zero.
                                                                      b.    Make the right child field of
                    parent                                                  parent as a thread pointing
                                                                            to the inorder successor of
                                                                            currentNode.

                                       60                      5.   Release the memory for
                                                                    currentNode.

                                  currentNode

     Ver. 1.0                                                                              Session 16
Data Structures and Algorithms
Deleting a Node from a Threaded Binary Tree (Contd.)
                                   header node                 1.   Locate the position of the node to
   Delete node 60                                                   be deleted. Mark it as
                                                                    currentNode and its parent as
                                                                    parent.

                                                               3.   If currentNode is the left child of

                                  . 65 .                            parent:
                                                                      a.    Set the left thread field of
                                                                            parent as zero.
                                                                      b.    Make the left child field of
                                                                            parent as a thread pointing
                . 40 .                           . 72 .                     to the inorder predecessor
                                                                            of currentNode.
                                                                      c.    Go to step 4.

                                                               4.   If currentNode is the right child of
                                                                    parent:
      30                 . 50 .             69            80          •     Set the right thread field of
                                                                            parent as zero.
                                                                      •     Make the right child field of
                    parent                                                  parent as a thread pointing
                                                                            to the inorder successor of
                                                                            currentNode.

                                       60                      5.   Release the memory for
                                                                    currentNode.

                                  currentNode

     Ver. 1.0                                                                              Session 16
Data Structures and Algorithms
Deleting a Node from a Threaded Binary Tree (Contd.)
                                   header node                 1.   Locate the position of the node to
   Delete node 60                                                   be deleted. Mark it as
                                                                    currentNode and its parent as
                                                                    parent.

                                                               3.   If currentNode is the left child of

                                  . 65 .                            parent:
                                                                      a.    Set the left thread field of
                                                                            parent as zero.
                                                                      b.    Make the left child field of
                                                                            parent as a thread pointing
                . 40 .                           . 72 .                     to the inorder predecessor
                                                                            of currentNode.
                                                                      c.    Go to step 4.

                                                               4.   If currentNode is the right child of
                                                                    parent:
      30                 . 50 .             69            80          •     Set the right thread field of
                                                                            parent as zero.
                                                                      •     Make the right child field of
                    parent                                                  parent as a thread pointing
                                                                            to the inorder successor of
                                                                            currentNode.

                                       60                      5.   Release the memory for
                                                                    currentNode.

                                  currentNode

     Ver. 1.0                                                                              Session 16
Data Structures and Algorithms
Deleting a Node from a Threaded Binary Tree (Contd.)
                                   header node                 1.   Locate the position of the node to
   Delete node 60                                                   be deleted. Mark it as
                                                                    currentNode and its parent as
                                                                    parent.

                                                               3.   If currentNode is the left child of

                                  . 65 .                            parent:
                                                                      a.    Set the left thread field of
                                                                            parent as zero.
                                                                      b.    Make the left child field of
                                                                            parent as a thread pointing
                . 40 .                           . 72 .                     to the inorder predecessor
                                                                            of currentNode.
                                                                      c.    Go to step 4.

                                                               4.   If currentNode is the right child of
                                                                    parent:
      30                 . 50               69            80          •     Set the right thread field of
                                                                            parent as zero.
                                                                      •     Make the right child field of
                    parent                                                  parent as a thread pointing
                                                                            to the inorder successor of
                                Deletion complete                           currentNode.

                                       60                      5.   Release the memory for
                                                                    currentNode.

                                  currentNode

     Ver. 1.0                                                                              Session 16
Data Structures and Algorithms
Deleting a Node from a Threaded Binary Tree (Contd.)


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




     Ver. 1.0                                                        Session 16
Data Structures and Algorithms
Deleting a Node from a Threaded Binary Tree (Contd.)
                                                            1.   Locate the node to be deleted. Mark it as
                                header node
  Delete node 50                                                 currentNode and its parent as parent.
                                                            2.   If currentNode has a left subtree:
                      Algorithm to delete aa case in
                      Let us now consider node                     a.    Mark the left child of current node as
                                                                         child.
                      with one child from a deleted
                      which the node to be                         b.    Go to step 4.
                                                            3.   If currentNode has a right subtree:
                      threadedchild (left or right).
                      has one binary tree.                         •     Mark the right child of currentNode as
                               . 65 .                              •
                                                                         child.
                                                                         Go to step 4.
                                                            4.   If currentNode is the left child of parent:
                                                                   a.    Make the left child of parent point to
                                                                         child.
             . 40 .                           . 72 .        5.
                                                                   b.    Go to step 6.
                                                                 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 inorder

  30                  . 50 .             69            80
                                                                 predecessor of currentNode. Mark them as
                                                                 successor 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 left
                                                                         thread of successor to zero.
                                                                   b.    Go to step 9.
                                                            8.   If currentNode has a left child:
                                    60                             a.    Make the right child of its predecessor
                                                                         point to its successor and set the right
                                                                         thread of predecessor to zero.
                                                            9.   Release the memory for currentNode.


       Ver. 1.0                                                                                   Session 16
Data Structures and Algorithms
Deleting a Node from a Threaded Binary Tree (Contd.)
                                                            •    Locate the node to be deleted. Mark it as
                                header node
  Delete node 50                                                 currentNode and its parent as parent.
                                                            •    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
                               . 65 .                              b.
                                                                         child.
                                                                         Go to step 4.
            parent                                          4.   If currentNode is the left child of parent:
                                                                   a.    Make the left child of parent point to
                                                                         child.
             . 40 .                           . 72 .        5.
                                                                   b.    Go to step 6.
                                                                 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 inorder

  30                  . 50 .             69            80
                                                                 predecessor of currentNode. Mark them as
                                                                 successor 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 left
         currentNode                                                     thread of successor to zero.
                                                                   b.    Go to step 9.
                                                            8.   If currentNode has a left child:
                                    60                             a.    Make the right child of its predecessor
                                                                         point to its successor and set the right
                                                                         thread of predecessor to zero.
                                                            9.   Release the memory for currentNode.


       Ver. 1.0                                                                                   Session 16
Data Structures and Algorithms
Deleting a Node from a Threaded Binary Tree (Contd.)
                                                            •    Locate the node to be deleted. Mark it as
                                header node
  Delete node 50                                                 currentNode and its parent as parent.
                                                            •    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
                               . 65 .                              b.
                                                                         child.
                                                                         Go to step 4.
            parent                                          4.   If currentNode is the left child of parent:
                                                                   a.    Make the left child of parent point to
                                                                         child.
             . 40 .                           . 72 .        5.
                                                                   b.    Go to step 6.
                                                                 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 inorder

  30                  . 50 .             69            80
                                                                 predecessor of currentNode. Mark them as
                                                                 successor 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 left
         currentNode                                                     thread of successor to zero.
                                                                   b.    Go to step 9.
                                                            8.   If currentNode has a left child:
                                    60                             a.    Make the right child of its predecessor
                                                                         point to its successor and set the right
                                                                         thread of predecessor to zero.
                                                            9.   Release the memory for currentNode.


       Ver. 1.0                                                                                   Session 16
Data Structures and Algorithms
Deleting a Node from a Threaded Binary Tree (Contd.)
                                                            1.   Locate the node to be deleted. Mark it as
                                header node
  Delete node 50                                                 currentNode 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
                               . 65 .                              b.
                                                                         child.
                                                                         Go to step 4.
            parent                                          4.   If currentNode is the left child of parent:
                                                                   a.    Make the left child of parent point to
                                                                         child.
             . 40 .                           . 72 .        5.
                                                                   b.    Go to step 6.
                                                                 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 inorder

  30                  . 50 .             69            80
                                                                 predecessor of currentNode. Mark them as
                                                                 successor 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 left
         currentNode                                                     thread of successor to zero.
                                                                   b.    Go to step 9.
                                                            8.   If currentNode has a left child:
                                    60                             a.    Make the right child of its predecessor
                                                                         point to its successor and set the right
                                                                         thread of predecessor to zero.
                                                            9.   Release the memory for currentNode.


       Ver. 1.0                                                                                   Session 16
Data Structures and Algorithms
Deleting a Node from a Threaded Binary Tree (Contd.)
                                                             1.   Locate the node to be deleted. Mark it as
                                header node
  Delete node 50                                                  currentNode 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:
                                                                    •     Mark the right child of currentNode as
                               . 65 .                               •
                                                                          child.
                                                                          Go to step 4.
            parent                                           4.   If currentNode is the left child of parent:
                                                                    a.    Make the left child of parent point to
                                                                          child.
             . 40 .                            . 72 .        5.
                                                                    b.    Go to step 6.
                                                                  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 inorder

  30                  . 50 .              69            80
                                                                  predecessor of currentNode. Mark them as
                                                                  successor 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 left
         currentNode                                                      thread of successor to zero.
                                                                    b.    Go to step 9.
                                                             8.   If currentNode has a left child:
                                    60                              a.    Make the right child of its predecessor
                                                                          point to its successor and set the right
                                                                          thread of predecessor to zero.
                                  child                      9.   Release the memory for currentNode.


       Ver. 1.0                                                                                    Session 16
Data Structures and Algorithms
Deleting a Node from a Threaded Binary Tree (Contd.)
                                                             1.   Locate the node to be deleted. Mark it as
                                header node
  Delete node 50                                                  currentNode 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:
                                                                    •     Mark the right child of currentNode as
                               . 65 .                               •
                                                                          child.
                                                                          Go to step 4.
            parent                                           4.   If currentNode is the left child of parent:
                                                                    a.    Make the left child of parent point to
                                                                          child.
             . 40 .                            . 72 .        5.
                                                                    b.    Go to step 6.
                                                                  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 inorder

  30                  . 50 .              69            80
                                                                  predecessor of currentNode. Mark them as
                                                                  successor 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 left
         currentNode                                                      thread of successor to zero.
                                                                    b.    Go to step 9.
                                                             8.   If currentNode has a left child:
                                    60                              a.    Make the right child of its predecessor
                                                                          point to its successor and set the right
                                                                          thread of predecessor to zero.
                                  child                      9.   Release the memory for currentNode.


       Ver. 1.0                                                                                    Session 16
Data Structures and Algorithms
Deleting a Node from a Threaded Binary Tree (Contd.)
                                                             1.   Locate the node to be deleted. Mark it as
                                header node
  Delete node 50                                                  currentNode 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
                               . 65 .                               b.
                                                                          child.
                                                                          Go to step 4.
            parent                                           4.   If currentNode is the left child of parent:
                                                                    a.    Make the left child of parent point to
                                                                          child.
             . 40 .                            . 72 .        5.
                                                                    b.    Go to step 6.
                                                                  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 inorder

  30                  . 50 .              69            80
                                                                  predecessor of currentNode. Mark them as
                                                                  successor 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 left
         currentNode                                                      thread of successor to zero.
                                                                    b.    Go to step 9.
                                                             8.   If currentNode has a left child:
                                    60                              a.    Make the right child of its predecessor
                                                                          point to its successor and set the right
                                                                          thread of predecessor to zero.
                                  child                      9.   Release the memory for currentNode.


       Ver. 1.0                                                                                    Session 16
Data Structures and Algorithms
Deleting a Node from a Threaded Binary Tree (Contd.)
                                                             1.   Locate the node to be deleted. Mark it as
                                header node
  Delete node 50                                                  currentNode 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
                               . 65 .                               b.
                                                                          child.
                                                                          Go to step 4.
            parent                                           4.   If currentNode is the left child of parent:
                                                                    a.    Make the left child of parent point to
                                                                          child.
             . 40 .                            . 72 .        5.
                                                                    b.    Go to step 6.
                                                                  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 inorder

  30                  . 50 .              69            80
                                                                  predecessor of currentNode. Mark them as
                                                                  successor 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 left
         currentNode                                                      thread of successor to zero.
                                                                    b.    Go to step 9.
                                                             8.   If currentNode has a left child:
                                    60                              a.    Make the right child of its predecessor
                                                                          point to its successor and set the right
                                                                          thread of predecessor to zero.
                                  child                      9.   Release the memory for currentNode.


       Ver. 1.0                                                                                    Session 16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16

More Related Content

More from Niit Care

More from Niit Care (20)

Ajs 2 b
Ajs 2 bAjs 2 b
Ajs 2 b
 
Ajs 2 a
Ajs 2 aAjs 2 a
Ajs 2 a
 
Ajs 2 c
Ajs 2 cAjs 2 c
Ajs 2 c
 
Ajs 1 a
Ajs 1 aAjs 1 a
Ajs 1 a
 
Ajs 1 c
Ajs 1 cAjs 1 c
Ajs 1 c
 
Dacj 4 2-c
Dacj 4 2-cDacj 4 2-c
Dacj 4 2-c
 
Dacj 4 2-b
Dacj 4 2-bDacj 4 2-b
Dacj 4 2-b
 
Dacj 4 2-a
Dacj 4 2-aDacj 4 2-a
Dacj 4 2-a
 
Dacj 4 1-c
Dacj 4 1-cDacj 4 1-c
Dacj 4 1-c
 
Dacj 4 1-b
Dacj 4 1-bDacj 4 1-b
Dacj 4 1-b
 
Dacj 4 1-a
Dacj 4 1-aDacj 4 1-a
Dacj 4 1-a
 
Dacj 1-2 b
Dacj 1-2 bDacj 1-2 b
Dacj 1-2 b
 
Dacj 1-3 c
Dacj 1-3 cDacj 1-3 c
Dacj 1-3 c
 
Dacj 1-3 b
Dacj 1-3 bDacj 1-3 b
Dacj 1-3 b
 
Dacj 1-3 a
Dacj 1-3 aDacj 1-3 a
Dacj 1-3 a
 
Dacj 1-2 c
Dacj 1-2 cDacj 1-2 c
Dacj 1-2 c
 
Dacj 1-2 a
Dacj 1-2 aDacj 1-2 a
Dacj 1-2 a
 
Dacj 1-1 c
Dacj 1-1 cDacj 1-1 c
Dacj 1-1 c
 
Dacj 1-1 b
Dacj 1-1 bDacj 1-1 b
Dacj 1-1 b
 
Dacj 1-1 a
Dacj 1-1 aDacj 1-1 a
Dacj 1-1 a
 

Recently uploaded

Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 

Recently uploaded (20)

Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 

11 ds and algorithm session_16

  • 1. Data Structures and Algorithms Objectives In this session, you will learn to: Implement a threaded binary tree Implement a height balanced binary tree Store data in a graph Ver. 1.0 Session 16
  • 2. Data Structures and Algorithms Deleting a Node from a Threaded Binary Tree Write an algorithm to locate thebinary to berefers to in a Delete operation in a threaded node tree deleted the threadedof removing the specified node from the threaded process binary tree. binary 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 marked as the currentNode and its parent as parent. Ver. 1.0 Session 16
  • 3. Data Structures and Algorithms Deleting a Node from a Threaded Binary Tree (Contd.) 1. Mark the left child of the header node as currentNode. Delete 80 2. Mark the header node as parent. Algorithm header node to locate the node 3. Repeat steps a, b, c, d, and e until the node to be searched is found or currentNode to be deleted and its parent in becomes NULL: a. Mark currentNode as parent. a threaded binary tree. b. If the value to be searched is less than that of currentNode, and the left child of the currentNode is a link: . 65 . 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 left . 40 . . 72 . child 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 the right child of the currentNode is a link: 30 . 50 . 69 80 i. Make currentNode point to its right child and go to step 3. e. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a thread: i. Mark currentNode as NULL 60 and go to step 3. Ver. 1.0 Session 16
  • 4. Data Structures and Algorithms Deleting a Node from a Threaded Binary Tree (Contd.) • Mark the left child of the header node as currentNode. • Mark the header node as parent. Delete 80 header node • Repeat steps a, b, c, d, and e until the node to be searched is found or currentNode becomes NULL: a. Mark currentNode as parent. b. If the value to be searched is less than that of currentNode, and the left child of the currentNode is a link: currentNode . 65 . 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 left . 40 . . 72 . child 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 the right child of the currentNode is a link: 30 . 50 . 69 80 i. Make currentNode point to its right child and go to step 3. e. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a thread: i. Mark currentNode as NULL 60 and go to step 3. Ver. 1.0 Session 16
  • 5. Data Structures and Algorithms Deleting a Node from a Threaded Binary Tree (Contd.) • Mark the left child of the header node as currentNode. • Mark the header node as parent. Delete 80 parent header node • Repeat steps a, b, c, d, and e until the node to be searched is found or currentNode becomes NULL: a. Mark currentNode as parent. b. If the value to be searched is less than that of currentNode, and the left child of the currentNode is a link: currentNode . 65 . 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 left . 40 . . 72 . child 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 the right child of the currentNode is a link: 30 . 50 . 69 80 i. Make currentNode point to its right child and go to step 3. e. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a thread: i. Mark currentNode as NULL 60 and go to step 3. Ver. 1.0 Session 16
  • 6. Data Structures and Algorithms Deleting a Node from a Threaded Binary Tree (Contd.) • Mark the left child of the header node as currentNode. • Mark the header node as parent. Delete 80 parent header node • Repeat steps a, b, c, d, and e until the node to be searched is found or currentNode becomes NULL: a. Mark currentNode as parent. b. If the value to be searched is less than that of currentNode, and the left child of the currentNode is a link: currentNode . 65 . 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 left . 40 . . 72 . child 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 the right child of the currentNode is a link: 30 . 50 . 69 80 i. Make currentNode point to its right child and go to step 3. e. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a thread: i. Mark currentNode as NULL 60 and go to step 3. Ver. 1.0 Session 16
  • 7. Data Structures and Algorithms Deleting a Node from a Threaded Binary Tree (Contd.) 1. Mark the left child of the header node as currentNode. Delete 80 2. Mark the header node as parent. parent header node 3. Repeat steps a, b, c, d, and e until the node to be searched is found or currentNode becomes NULL: • Mark currentNode as parent. • If the value to be searched is less than that of currentNode, and the left child of the currentNode is a link: currentNode . 65 . parent 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 left . 40 . . 72 . child 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 the right child of the currentNode is a link: 30 . 50 . 69 80 i. Make currentNode point to its right child and go to step 3. e. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a thread: i. Mark currentNode as NULL 60 and go to step 3. Ver. 1.0 Session 16
  • 8. Data Structures and Algorithms Deleting a Node from a Threaded Binary Tree (Contd.) 1. Mark the left child of the header node as currentNode. Delete 80 2. Mark the header node as parent. header node 3. Repeat steps a, b, c, d, and e until the node to be searched is found or currentNode becomes NULL: • Mark currentNode as parent. • If the value to be searched is less than that of currentNode, and the left child of the currentNode is a link: currentNode . 65 . parent 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 left . 40 . . 72 . child 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 the right child of the currentNode is a link: 30 . 50 . 69 80 i. Make currentNode point to its right child and go to step 3. e. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a thread: i. Mark currentNode as NULL 60 and go to step 3. Ver. 1.0 Session 16
  • 9. Data Structures and Algorithms Deleting a Node from a Threaded Binary Tree (Contd.) 1. Mark the left child of the header node as currentNode. Delete 80 2. Mark the header node as parent. header node 3. Repeat steps a, b, c, d, and e until the node to be searched is found or currentNode becomes NULL: a. Mark currentNode as parent. b. If the value to be searched is less than that of currentNode, and the left child of the currentNode is a link: currentNode . 65 . parent 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 left . 40 . . 72 . child 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 the right child of the currentNode is a link: 30 . 50 . 69 80 i. Make currentNode point to its right child and go to step 3. e. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a thread: i. Mark currentNode as NULL 60 and go to step 3. Ver. 1.0 Session 16
  • 10. Data Structures and Algorithms Deleting a Node from a Threaded Binary Tree (Contd.) • Mark the left child of the header node as currentNode. • Mark the header node as parent. Delete 80 header node • Repeat steps a, b, c, d, and e until the node to be searched is found or currentNode becomes NULL: a. Mark currentNode as parent. b. If the value to be searched is less than that of currentNode, and the left child of the currentNode is a link: currentNode . 65 . parent 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 left . 40 . . 72 . child 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 the right child of the currentNode is a link: 30 . 50 . 69 80 i. Make currentNode point to its right child and go to step 3. e. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a thread: i. Mark currentNode as NULL 60 and go to step 3. Ver. 1.0 Session 16
  • 11. Data Structures and Algorithms Deleting a Node from a Threaded Binary Tree (Contd.) 1. Mark the left child of the header node as currentNode. Delete 80 2. Mark the header node as parent. header node 3. Repeat steps a, b, c, d, and e until the node to be searched is found or currentNode becomes NULL: a. Mark currentNode as parent. b. If the value to be searched is less than that of currentNode, and the left child of the currentNode is a link: currentNode . 65 . parent i. Make currentNode point to its left child and go to step 3. currentNode c. If the value to be searched is less than that of currentNode, and the left . 40 . . 72 . child 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 the right child of the currentNode is a link: 30 . 50 . 69 80 i. Make currentNode point to its right child and go to step 3. e. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a thread: i. Mark currentNode as NULL 60 and go to step 3. Ver. 1.0 Session 16
  • 12. Data Structures and Algorithms Deleting a Node from a Threaded Binary Tree (Contd.) • Mark the left child of the header node as currentNode. • Mark the header node as parent. Delete 80 header node • Repeat steps a, b, c, d, and e until the node to be searched is found or currentNode becomes NULL: a. Mark currentNode as parent. b. If the value to be searched is less than that of currentNode, and the left child of the currentNode is a link: . 65 . parent i. Make currentNode point to its left child and go to step 3. currentNode c. If the value to be searched is less than that of currentNode, and the left . 40 . . 72 . child 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 the right child of the currentNode is a link: 30 . 50 . 69 80 i. Make currentNode point to its right child and go to step 3. e. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a thread: i. Mark currentNode as NULL 60 and go to step 3. Ver. 1.0 Session 16
  • 13. Data Structures and Algorithms Deleting a Node from a Threaded Binary Tree (Contd.) 1. Mark the left child of the header node as currentNode. Delete 80 2. Mark the header node as parent. header node 3. Repeat steps a, b, c, d, and e until the node to be searched is found or currentNode becomes NULL: • Mark currentNode as parent. • If the value to be searched is less than that of currentNode, and the left child of the currentNode is a link: . 65 . parent i. Make currentNode point to its left child and go to step 3. currentNode c. If the value to be searched is less than that of currentNode, and the left . 40 . parent . 72 . child 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 the right child of the currentNode is a link: 30 . 50 . 69 80 i. Make currentNode point to its right child and go to step 3. e. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a thread: i. Mark currentNode as NULL 60 and go to step 3. Ver. 1.0 Session 16
  • 14. Data Structures and Algorithms Deleting a Node from a Threaded Binary Tree (Contd.) 1. Mark the left child of the header node as currentNode. Delete 80 2. Mark the header node as parent. header node 3. Repeat steps a, b, c, d, and e until the node to be searched is found or currentNode becomes NULL: • Mark currentNode as parent. • If the value to be searched is less than that of currentNode, and the left child of the currentNode is a link: . 65 . i. Make currentNode point to its left child and go to step 3. currentNode c. If the value to be searched is less than that of currentNode, and the left . 40 . parent . 72 . child 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 the right child of the currentNode is a link: 30 . 50 . 69 80 i. Make currentNode point to its right child and go to step 3. e. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a thread: i. Mark currentNode as NULL 60 and go to step 3. Ver. 1.0 Session 16
  • 15. Data Structures and Algorithms Deleting a Node from a Threaded Binary Tree (Contd.) 1. Mark the left child of the header node as currentNode. Delete 80 2. Mark the header node as parent. header node 3. Repeat steps a, b, c, d, and e until the node to be searched is found or currentNode becomes NULL: a. Mark currentNode as parent. b. If the value to be searched is less than that of currentNode, and the left child of the currentNode is a link: . 65 . i. Make currentNode point to its left child and go to step 3. currentNode c. If the value to be searched is less than that of currentNode, and the left . 40 . parent . 72 . child 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 the right child of the currentNode is a link: 30 . 50 . 69 80 i. Make currentNode point to its right child and go to step 3. e. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a thread: i. Mark currentNode as NULL 60 and go to step 3. Ver. 1.0 Session 16
  • 16. Data Structures and Algorithms Deleting a Node from a Threaded Binary Tree (Contd.) 1. Mark the left child of the header node as currentNode. Delete 80 2. Mark the header node as parent. header node 3. Repeat steps a, b, c, d, and e until the node to be searched is found or currentNode becomes NULL: a. Mark currentNode as parent. b. If the value to be searched is less than that of currentNode, and the left child of the currentNode is a link: . 65 . i. Make currentNode point to its left child and go to step 3. currentNode c. If the value to be searched is less than that of currentNode, and the left . 40 . parent . 72 . child 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 the right child of the currentNode is a link: 30 . 50 . 69 80 i. Make currentNode point to its right child and go to step 3. e. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a thread: i. Mark currentNode as NULL 60 and go to step 3. Ver. 1.0 Session 16
  • 17. Data Structures and Algorithms Deleting a Node from a Threaded Binary Tree (Contd.) 1. Mark the left child of the header node as currentNode. Delete 80 2. Mark the header node as parent. header node 3. Repeat steps a, b, c, d, and e until the node to be searched is found or currentNode becomes NULL: a. Mark currentNode as parent. b. If the value to be searched is less than that of currentNode, and the left child of the currentNode is a link: . 65 . i. Make currentNode point to its left child and go to step 3. currentNode c. If the value to be searched is less than that of currentNode, and the left . 40 . parent . 72 . child 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 the right child of the currentNode is a link: 30 . 50 . 69 80 i. Make currentNode point to its right child and go to step 3. e. If the value to be searched is greater than that of currentNode, and the currentNode right child of the currentNode is a thread: i. Mark currentNode as NULL 60 and go to step 3. Ver. 1.0 Session 16
  • 18. Data Structures and Algorithms Deleting a Node from a Threaded Binary Tree (Contd.) •Mark the left child of the header node as currentNode. Nodes located • Mark the header node as parent. Delete 80 header node • Repeat steps a, b, c, d, and e until the node to be searched is found or currentNode becomes NULL: a. Mark currentNode as parent. b. If the value to be searched is less than that of currentNode, and the left child of the currentNode is a link: . 65 . 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 left . 40 . parent . 72 . child 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 the right child of the currentNode is a link: 30 . 50 . 69 80 i. Make currentNode point to its right child and go to step 3. e. If the value to be searched is greater than that of currentNode, and the currentNode right child of the currentNode is a thread: i. Mark currentNode as NULL 60 and go to step 3. Ver. 1.0 Session 16
  • 19. Data Structures and Algorithms Deleting a Node from a Threaded Binary Tree (Contd.) Once you if the tree is not empty, there can be three cases: Before implementingnode to be deleted and its parent, to However, locate the a delete operation, you first need you can Node to be deleted is isof the node to be deleted after release the memory empty or check whether the tree a leaf node not. adjustingis empty if thethreads appropriately. node is a tree the links and left child of the header The Node to be deleted has one child (left or right) thread pointingdeleted has two children Node to be to itself. If the tree is empty, an error message is shown. header node Ver. 1.0 Session 16
  • 20. Data Structures and Algorithms Deleting a Node from a Threaded Binary Tree (Contd.) Let us first consider a case in which the node to be deleted is the leaf node. In this case, you first need to check if there is only one node present in the tree. header node 65 new node Ver. 1.0 Session 16
  • 21. Data Structures and Algorithms Deleting a Node from a Threaded Binary Tree (Contd.) To delete this node, make the left child of the header node as a thread pointing to itself. header node 65 new node Ver. 1.0 Session 16
  • 22. Data Structures and Algorithms Deleting a Node from a Threaded Binary Tree (Contd.) To delete this node, make the left child of the header node as a thread pointing to itself. Now release the memory of the node to be deleted. header node Delete operation complete 65 new node Ver. 1.0 Session 16
  • 23. Data Structures and Algorithms Deleting a Node from a Threaded Binary Tree (Contd.) header node 1. Locate the position of the node to Delete node 60 be deleted. Mark it as However, if there are more than currentNode and its parent as parent. one nodes in the tree, you need 3. If currentNode is the left child of another algorithm to delete a leaf node. . 65 . parent: a. Set the left thread field of parent as zero. b. Make the left child field of parent as a thread pointing . 40 . . 72 . to the inorder predecessor of currentNode. c. Go to step 4. 4. If currentNode is the right child of parent: 30 . 50 . 69 80 a. Set the right thread field of parent as zero. b. Make the right child field of parent as a thread pointing to the inorder successor of currentNode. 60 5. Release the memory for currentNode. Ver. 1.0 Session 16
  • 24. Data Structures and Algorithms Deleting a Node from a Threaded Binary Tree (Contd.) header node • Locate the position of the node to Delete node 60 be deleted. Mark it as currentNode and its parent as parent. • If currentNode is the left child of . 65 . parent: a. Set the left thread field of parent as zero. b. Make the left child field of parent as a thread pointing . 40 . . 72 . to the inorder predecessor of currentNode. c. Go to step 4. 4. If currentNode is the right child of parent: 30 . 50 . 69 80 a. Set the right thread field of parent as zero. b. Make the right child field of parent parent as a thread pointing to the inorder successor of currentNode. 60 5. Release the memory for currentNode. currentNode Ver. 1.0 Session 16
  • 25. Data Structures and Algorithms Deleting a Node from a Threaded Binary Tree (Contd.) header node • Locate the position of the node to Delete node 60 be deleted. Mark it as currentNode and its parent as parent. • If currentNode is the left child of . 65 . parent: a. Set the left thread field of parent as zero. b. Make the left child field of parent as a thread pointing . 40 . . 72 . to the inorder predecessor of currentNode. c. Go to step 4. 4. If currentNode is the right child of parent: 30 . 50 . 69 80 a. Set the right thread field of parent as zero. b. Make the right child field of parent parent as a thread pointing to the inorder successor of currentNode. 60 5. Release the memory for currentNode. currentNode Ver. 1.0 Session 16
  • 26. Data Structures and Algorithms Deleting a Node from a Threaded Binary Tree (Contd.) header node 1. Locate the position of the node to Delete node 60 be deleted. Mark it as currentNode and its parent as parent. 3. If currentNode is the left child of . 65 . parent: a. Set the left thread field of parent as zero. b. Make the left child field of parent as a thread pointing . 40 . . 72 . to the inorder predecessor of currentNode. c. Go to step 4. 4. If currentNode is the right child of parent: 30 . 50 . 69 80 a. Set the right thread field of parent as zero. b. Make the right child field of parent parent as a thread pointing to the inorder successor of currentNode. 60 5. Release the memory for currentNode. currentNode Ver. 1.0 Session 16
  • 27. Data Structures and Algorithms Deleting a Node from a Threaded Binary Tree (Contd.) header node 1. Locate the position of the node to Delete node 60 be deleted. Mark it as currentNode and its parent as parent. 3. If currentNode is the left child of . 65 . parent: a. Set the left thread field of parent as zero. b. Make the left child field of parent as a thread pointing . 40 . . 72 . to the inorder predecessor of currentNode. c. Go to step 4. 4. If currentNode is the right child of parent: 30 . 50 . 69 80 • Set the right thread field of parent as zero. • Make the right child field of parent parent as a thread pointing to the inorder successor of currentNode. 60 5. Release the memory for currentNode. currentNode Ver. 1.0 Session 16
  • 28. Data Structures and Algorithms Deleting a Node from a Threaded Binary Tree (Contd.) header node 1. Locate the position of the node to Delete node 60 be deleted. Mark it as currentNode and its parent as parent. 3. If currentNode is the left child of . 65 . parent: a. Set the left thread field of parent as zero. b. Make the left child field of parent as a thread pointing . 40 . . 72 . to the inorder predecessor of currentNode. c. Go to step 4. 4. If currentNode is the right child of parent: 30 . 50 . 69 80 • Set the right thread field of parent as zero. • Make the right child field of parent parent as a thread pointing to the inorder successor of currentNode. 60 5. Release the memory for currentNode. currentNode Ver. 1.0 Session 16
  • 29. Data Structures and Algorithms Deleting a Node from a Threaded Binary Tree (Contd.) header node 1. Locate the position of the node to Delete node 60 be deleted. Mark it as currentNode and its parent as parent. 3. If currentNode is the left child of . 65 . parent: a. Set the left thread field of parent as zero. b. Make the left child field of parent as a thread pointing . 40 . . 72 . to the inorder predecessor of currentNode. c. Go to step 4. 4. If currentNode is the right child of parent: 30 . 50 69 80 • Set the right thread field of parent as zero. • Make the right child field of parent parent as a thread pointing to the inorder successor of Deletion complete currentNode. 60 5. Release the memory for currentNode. currentNode Ver. 1.0 Session 16
  • 30. Data Structures and Algorithms Deleting a Node from a Threaded Binary Tree (Contd.) Write an algorithm to delete a node, which has one child in a threaded binary tree. Ver. 1.0 Session 16
  • 31. Data Structures and Algorithms Deleting a Node from a Threaded Binary Tree (Contd.) 1. Locate the node to be deleted. Mark it as header node Delete node 50 currentNode and its parent as parent. 2. If currentNode has a left subtree: Algorithm to delete aa case in Let us now consider node a. Mark the left child of current node as child. with one child from a deleted which the node to be b. Go to step 4. 3. If currentNode has a right subtree: threadedchild (left or right). has one binary tree. • Mark the right child of currentNode as . 65 . • child. Go to step 4. 4. If currentNode is the left child of parent: a. Make the left child of parent point to child. . 40 . . 72 . 5. b. Go to step 6. 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 inorder 30 . 50 . 69 80 predecessor of currentNode. Mark them as successor 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 left thread of successor to zero. b. Go to step 9. 8. If currentNode has a left child: 60 a. Make the right child of its predecessor point to its successor and set the right thread of predecessor to zero. 9. Release the memory for currentNode. Ver. 1.0 Session 16
  • 32. Data Structures and Algorithms Deleting a Node from a Threaded Binary Tree (Contd.) • Locate the node to be deleted. Mark it as header node Delete node 50 currentNode and its parent as parent. • 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 . 65 . b. child. Go to step 4. parent 4. If currentNode is the left child of parent: a. Make the left child of parent point to child. . 40 . . 72 . 5. b. Go to step 6. 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 inorder 30 . 50 . 69 80 predecessor of currentNode. Mark them as successor 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 left currentNode thread of successor to zero. b. Go to step 9. 8. If currentNode has a left child: 60 a. Make the right child of its predecessor point to its successor and set the right thread of predecessor to zero. 9. Release the memory for currentNode. Ver. 1.0 Session 16
  • 33. Data Structures and Algorithms Deleting a Node from a Threaded Binary Tree (Contd.) • Locate the node to be deleted. Mark it as header node Delete node 50 currentNode and its parent as parent. • 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 . 65 . b. child. Go to step 4. parent 4. If currentNode is the left child of parent: a. Make the left child of parent point to child. . 40 . . 72 . 5. b. Go to step 6. 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 inorder 30 . 50 . 69 80 predecessor of currentNode. Mark them as successor 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 left currentNode thread of successor to zero. b. Go to step 9. 8. If currentNode has a left child: 60 a. Make the right child of its predecessor point to its successor and set the right thread of predecessor to zero. 9. Release the memory for currentNode. Ver. 1.0 Session 16
  • 34. Data Structures and Algorithms Deleting a Node from a Threaded Binary Tree (Contd.) 1. Locate the node to be deleted. Mark it as header node Delete node 50 currentNode 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 . 65 . b. child. Go to step 4. parent 4. If currentNode is the left child of parent: a. Make the left child of parent point to child. . 40 . . 72 . 5. b. Go to step 6. 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 inorder 30 . 50 . 69 80 predecessor of currentNode. Mark them as successor 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 left currentNode thread of successor to zero. b. Go to step 9. 8. If currentNode has a left child: 60 a. Make the right child of its predecessor point to its successor and set the right thread of predecessor to zero. 9. Release the memory for currentNode. Ver. 1.0 Session 16
  • 35. Data Structures and Algorithms Deleting a Node from a Threaded Binary Tree (Contd.) 1. Locate the node to be deleted. Mark it as header node Delete node 50 currentNode 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: • Mark the right child of currentNode as . 65 . • child. Go to step 4. parent 4. If currentNode is the left child of parent: a. Make the left child of parent point to child. . 40 . . 72 . 5. b. Go to step 6. 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 inorder 30 . 50 . 69 80 predecessor of currentNode. Mark them as successor 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 left currentNode thread of successor to zero. b. Go to step 9. 8. If currentNode has a left child: 60 a. Make the right child of its predecessor point to its successor and set the right thread of predecessor to zero. child 9. Release the memory for currentNode. Ver. 1.0 Session 16
  • 36. Data Structures and Algorithms Deleting a Node from a Threaded Binary Tree (Contd.) 1. Locate the node to be deleted. Mark it as header node Delete node 50 currentNode 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: • Mark the right child of currentNode as . 65 . • child. Go to step 4. parent 4. If currentNode is the left child of parent: a. Make the left child of parent point to child. . 40 . . 72 . 5. b. Go to step 6. 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 inorder 30 . 50 . 69 80 predecessor of currentNode. Mark them as successor 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 left currentNode thread of successor to zero. b. Go to step 9. 8. If currentNode has a left child: 60 a. Make the right child of its predecessor point to its successor and set the right thread of predecessor to zero. child 9. Release the memory for currentNode. Ver. 1.0 Session 16
  • 37. Data Structures and Algorithms Deleting a Node from a Threaded Binary Tree (Contd.) 1. Locate the node to be deleted. Mark it as header node Delete node 50 currentNode 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 . 65 . b. child. Go to step 4. parent 4. If currentNode is the left child of parent: a. Make the left child of parent point to child. . 40 . . 72 . 5. b. Go to step 6. 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 inorder 30 . 50 . 69 80 predecessor of currentNode. Mark them as successor 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 left currentNode thread of successor to zero. b. Go to step 9. 8. If currentNode has a left child: 60 a. Make the right child of its predecessor point to its successor and set the right thread of predecessor to zero. child 9. Release the memory for currentNode. Ver. 1.0 Session 16
  • 38. Data Structures and Algorithms Deleting a Node from a Threaded Binary Tree (Contd.) 1. Locate the node to be deleted. Mark it as header node Delete node 50 currentNode 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 . 65 . b. child. Go to step 4. parent 4. If currentNode is the left child of parent: a. Make the left child of parent point to child. . 40 . . 72 . 5. b. Go to step 6. 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 inorder 30 . 50 . 69 80 predecessor of currentNode. Mark them as successor 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 left currentNode thread of successor to zero. b. Go to step 9. 8. If currentNode has a left child: 60 a. Make the right child of its predecessor point to its successor and set the right thread of predecessor to zero. child 9. Release the memory for currentNode. Ver. 1.0 Session 16

Editor's Notes

  1. Tell the students that the given algorithm is for implementing a delete operation in a non empty threaded binary tree. If three is empty, then in that case parent will point to the header node and currentNode will become NULL. Refer to the SG for the same.
  2. Tell the students that the given algorithm is for implementing a delete operation in a non empty threaded binary tree. If three is empty, then in that case parent will point to the header node and currentNode will become NULL. Refer to the SG for the same.
  3. Explain the term “Ancestor of a node”
  4. In this slide you need to show the calculation to determine the sum of an arithmetic progression for bubble sort algorithm. Refer to student guide.
  5. In this slide you need to show the calculation to determine the sum of an arithmetic progression for bubble sort algorithm. Refer to student guide.
  6. Start this by initiating the discussion on the given slide. Tell students that this problem can be solved with the help of a graph.
  7. Define graphs to students and then also explain the types of graphs.