next next up down toc toc mail

7.14.  Deleting an Element from a BST

Deleting an element x from a BST is a little more complicated than lookup or insert. To begin, we may locate the node n containing x. If their is no such a node we are done. If n is a leaf we can simply delete the leaf. If x is at an interior node n, we cannot delete that node, because to do so would disconnect the tree.

We must rearrange the tree in some way so that the BST property is maintained and yet x is not longer in the tree. There are two cases:

First, if n has only one child, we can replace n by that child.

Second, n has two children present. One strategy is to find the node m with label y, the smallest element in the right subtree of n, and replace x by y in node n and remove the node m (see first).

[picture]

The algorithm as a Nassi Shneidermann diagram:

Procedure deleteElement(node n_r, label x):

[picture]

Procedure delete_min(node root): label

[picture]

Examples:


back next up down toc toc mail


Created by unroff & hp-tools. © by Hans-Peter Bischof. All Rights Reserved (1998).

Last modified: 27/July/98 (12:14)