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

Info iconThis 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.
Background image of page 1

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

View Full DocumentRight 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
Background 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
Background image of page 3

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

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

This note was uploaded on 05/21/2010 for the course CS CS301 taught by Professor Dr.naveed malik during the Spring '10 term at Virtual University of Pakistan.

Page1 / 18

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

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online