25.11.2014 Views

Algorithms and Data Structures

Algorithms and Data Structures

Algorithms and Data Structures

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

N.Wirth. <strong>Algorithms</strong> <strong>and</strong> <strong>Data</strong> <strong>Structures</strong>. Oberon version 160<br />

4.4.4 Tree Deletion<br />

We now turn to the inverse problem of insertion: deletion. Our task is to define an algorithm for deleting,<br />

i.e., removing the node with key x in a tree with ordered keys. Unfortunately, removal of an element is not<br />

generally as simple as insertion. It is straightforward if the element to be deleted is a terminal node or one<br />

with a single descendant. The difficulty lies in removing an element with two descendants, for we cannot<br />

point in two directions with a single pointer. In this situation, the deleted element is to be replaced by either<br />

the rightmost element of its left subtree or by the leftmost node of its right subtree, both of which have at<br />

most one descendant. The details are shown in the recursive procedure delete. This procedure<br />

distinguishes among three cases:<br />

1. There is no component with a key equal to x.<br />

2. The component with key x has at most one descendant.<br />

3. The component with key x has two descendants.<br />

PROCEDURE delete (x: INTEGER; VAR p: Node); (* ADenS444_Deletion *)<br />

PROCEDURE del (VAR r: Node);<br />

BEGIN<br />

IF r.right # NIL THEN del(r.right)<br />

ELSE p.key := r.key; p.count := r.count;<br />

r := r.left<br />

END<br />

END del;<br />

BEGIN (*delete*)<br />

IF p = NIL THEN (*word is not in tree*)<br />

ELSIF x < p.key THEN delete(x, p.left)<br />

ELSIF x > p.key THEN delete(x, p.right)<br />

ELSE (*delete p^:*)<br />

IF p.right = NIL THEN p := p.left<br />

ELSIF p.left = NIL THEN p := p.right<br />

ELSE del(p.left)<br />

END<br />

END<br />

END delete<br />

The auxiliary, recursive procedure del is activated in case 3 only. It descends along the rightmost branch of<br />

the left subtree of the element p^ to be deleted, <strong>and</strong> then it replaces the relevant information (key <strong>and</strong><br />

count) in p^ by the corresponding values of the rightmost component r^ of that left subtree, whereafter r^<br />

may be disposed.<br />

We note that we do not mention a procedure that would be the inverse of NEW, indicating that storage is<br />

no longer needed <strong>and</strong> therefore disposable <strong>and</strong> reusable. It is generally assumed that a computer system<br />

recognizes a disposable variable through the circumstance that no other variables are pointing to it, <strong>and</strong> that<br />

it therefore can no longer be referenced. Such a system is called a garbage collector. It is not a feature of<br />

a programming language, but rather of its implementations.<br />

In order to illustrate the functioning of procedure delete, we refer to Fig. 4.28. Consider the tree (a);<br />

then delete successively the nodes with keys 13, 15, 5, 10. The resulting trees are shown in Fig. 4.28 (be).

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!