trees_3 - Treees 3 Deletion -leaf node - node with one...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
Treees – 3 Deletion -leaf node - node with one child - node with two children
Background image of page 1

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

View Full DocumentRight Arrow Icon
Deleting a node from a Binary Search Tree Deletion of a node is not so straightforward as is the case of insertion. It would depend on which particular node is being deleted. In fact, we note that there can be three separate cases and each case needs to be handled somewhat differently. The various cases are: (1) deletion of a leaf node, (2) deletion of an internal node with a single child (either a left or right subtree), (3) deletion of an internal node with two children (having both left subtree and right subtree. ) We’ll examine each case separately:
Background image of page 2
Deletion of a leaf Node Since a leaf node has empty left and right subtrees, deleting a leaf node will render a tree with one less node but which remains a BST. This is illustrated below: A BST with a leaf node Still a BST Marked for deletion. 5 1 6 7 3 2 4 5 1 6 7 3 2
Background image of page 3

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

View Full DocumentRight Arrow Icon
Deletion of a Node with one child In this case, when the node gets deleted, the parent of the node must point to its left child or its right child, as the case may be. The parent’s reference to the node is reset to refer to the deleted node’s child. This has the effect of lifting up the deleted node’s children by one level in the tree. An example is shown below. A BST with an internal node having only one child marked to be deleted The marked internal node has only a right subtree so the parent of the deleted node will now reference the deleted node’s child 5 1 6 7 3 2 5 6 7 3 2
Background image of page 4
Note that it makes no difference if the node to be deleted has only a left or a right child. The previous example illustrated the case when the only child was a right child.
Background image of page 5

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

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

This document was uploaded on 06/12/2011.

Page1 / 16

trees_3 - Treees 3 Deletion -leaf node - node with one...

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

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