CS301-Lec16 handout - CS301 Data Structures Lecture No 16...

Info icon This preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
CS301 – Data Structures Lecture No. 16 ___________________________________________________________________ Data Structures Lecture No. 16 Reading Material Data Structures and Algorithm Analysis in C++ Chapter. 4 4.3 (all sub sections) Summary Deleting a node in BST C++ code for remove Binary Search Tree Class (BST) Deleting a node in BST In the previous lecture, we talked about deleting a node. Now we will discuss the ways to delete nodes of a BST ( Binary Search Tree ). Suppose you want to delete one of the nodes of BST. There are three cases for deleting a node from the BST. Case I: The node to be deleted is the leaf node i.e. it has no right or left child. It is very simple to delete such node. We make the pointer in the parent node pointing to this node as NULL. If the memory for the node has been dynamically allocated, we will release it. Case II: The node to be deleted has either left child (subtree) or right child (subtree). Case III: The node to be deleted has both the left and right children (subtree). This is the most difficult case. Let’s recap this case. The following figure shows the tree and the node to be deleted. Page 1 of 18 6 2 5 3 1 8 4 Inorder successor Inorder successor will be the left- most node in the right subtree of 2. The inorder successor will not have a left child because if it did, that child would be the left-most node.
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
CS301 – Data Structures Lecture No. 16 ___________________________________________________________________ In the above example, the node to be deleted is 2. It has both right and left subtrees. As this is the BST, so while traversing the tree in inorder, we will have sorted data. The strategy is to find the inorder successor of the node to be deleted. To search the inorder successor, we have to go to its right subtree and find the smallest element. Afterwards we will copy this value into the node to be deleted. In the above example, the number 3 is the smallest in the right subtree of the node 2. A view of data of inorder traversal shows that number 3 is the inorder successor of number 2. We copy the number 3 in the node to be deleted. Now in this transition tree, the number 3 is at two places. Now we have to delete the inorder successor node i.e. node 3. The node 3 has only right child. So this is the Case II of deletion. We will get the inorder successor of node 3 i.e. number 4. Therefore we will connect Page 2 of 18 6 2 5 3 1 8 4 6 3 5 3 1 8 4 6 3 5 3 1 8 4 6 3 5 4 1 8 6 3 5 3 1 8 4
Image of page 2
CS301 – Data Structures Lecture No. 16 ___________________________________________________________________ the node 5 with node 4 that is the left pointer of 5 which was earlier pointing to node 3. But now it is pointing to node 4. We delete the node 3. Remember that number 3 has been copied in the node 2. This way, we will delete the node having right and left both subtrees. C++ code for remove Now we will see the C++ code to carry out this deletion. In the above figures, we have named the method as delete , but delete is a keyword of C++. So we cannot write a method named delete . We have to name it something else, say remove . Keep in mind all the three cases of delete and the figures.
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern