Question 12.1.5: Write a procedure that deletes an element t from a set repr......

Write a procedure that deletes an element t from a set represented as an ordered tree. (If the element is not in the set, nothing should be done.)

Step-by-Step
The 'Blue Check Mark' means that this solution was answered by an expert.
Learn more on how do we answer questions.
if root = null then begin

{the tree is empty, there is nothing to do}

end else begin

x := root;
{it remains to delete t from the subtree rooted at x;
since it may require changes in the father node,
we introduce the variables  father: 1 . . n and
direction:  (l, r) with the following
invariant: if x is not the root, then father
is (the number of) x’s father node, direction is
equal to 1/r if x is the left/right son of its father)
while ((t < val [x]) and (left [x] <> null)) or
((t > val [x]) and (right [x] <> null)) do begin

if t < val [x] then begin

 father := x; direction := l;
x := left [x];

end else begin {t > val [x]}

father := x; direction := r;
x := right [x];

end;

end;
{t = val [x] or t is not in the tree)
if t = val [x] then begin

..delete the node x with a known father and direction

end;

end;

The deletion of a vertex uses the procedure

procedure make_free (i: integer);
begin

left [i] := free;
free := i;

end

which adds the number i to the free list. While deleting a vertex, we should distinguish between four cases depending on whether the vertex has a left/right son or not.

if (left [x] = null) and (right [x] = null) then begin

{x is a leaf, no sons)
make free (x);
if x = root then begin

root := null;

end else if direction = l then begin

left [father]  := null;

end else begin {direction = r)

right [fathers  := null;

end;
else if (left [x] =null) and (right[x] <> null) then begin

{when x is deleted, right[x] occupies its place)
make_free (x) ;
if x = root then begin

root := right [x];

end else if direction = l then begin

left [fathers  := right [x];

end else begin {direction = r)
right [father]  := right Ix];

end;

end else if (left [x] <> null) and (right [xl=null) then begin

…the symmetrical code

end else begin {left [x] <> null, right [x] <> null)

..delete a vertex with two sons

end ;

The deletion of a vertex with two sons is the most difficult case. Here we should exchange it with an immediately following vertex (in the sense of label ordering).

y := right [x]; father := x; direction := r;
{now father and direction refer to vertex y)
while left [y] <> null do begin

father := y; direction := l;
y := left [y];

end

 

{val[y]  is minimal element of the set larger
than val[x], y has no left son}
val [x] := val [y];
..delete the vertex y (we already know how to do
that for a vertex without the left son)

Related Answered Questions

Question: 12.2.2

Verified Answer:

By induction over n, we prove that Φ_{n+2} ...
Question: 12.2.1

Verified Answer:

The maximal number of vertices is equal to ...
Question: 12.1.3

Verified Answer:

val [null]  := t; x := root ; while t <> val...
Question: 12.1.2

Verified Answer:

if root = null then begin ..t is not in the tree e...