Sunteți pe pagina 1din 8

Deleting a node from a binary search tree

This operation is rather more complicated. When a node is removed, you must ensure
that the tree is still a binary search tree. There are a few ways of doing this. One
approach is as follows:

First search down through the tree to located the node to be deleted, x say. Let p be
its parent node. Set t = x and then locate the node x that is going to replace t. Once x
is found it is linked to p and its left and right subtree are considered.

There are 3 cases to be considered when deleting the node t:

1. t has no right hand child node

t->r == z

2. t has a right hand child but its right hand child node has no left subtree

t->r->l == z

3. t has a right hand child node and the right hand child node has a left hand child
node

t->r->l != z

First of all we locate the node to be deleted with:

node *p, *t, *x, *c;


z->key = v;
p = head; x = head->r;
while( v != x->key)
{ p = x; x = (v < x->key) ? x->l : x->r; }
t = x;

Next we will examine each of the 3 cases in more detail. The first 2 cases are
Deleting a node from a binary search tree

Case 1

p parent node p
p

x
x v node to be deleted
t subtree
x->l

subtree z

if( t->r == z) x = x->l;




delete t;
if (v < p->key) p->l = x; else p->r = x;

2
Deleting a node from a binary search tree

Case 2

p parent node p
p

v node to be deleted
x
t

subtree
subtree subtree
x

z subtree

Here, we move x and its right


subtree to replace node t. The left
subtree of t will become a left
subtree of x. This is ok as x has no
left subtree to begin with

if( t->r == z) x = x->l;


else if (t->r-l == z) { x = x->r; x->l = t->l;}


delete t;
if (v < p->key) p->l = x; else p->r = x;

3
Deleting a node from a binary search tree

In this case we locate the next


Case 3 t->r->l != z biggest node in the right subtree
of the node to be deleted t. We
record its parent with c.
p
Then node c->l replaces t. This
ensures that we still have a binary
search tree

v t = x

subtree t->r

subtree

t->r->l
c

x = c->l

z
c->l->l
x->r

if( t->r == z) x = x->l;


else if (t->r-l == z) { x = x->r; x->l = t->l;}
else {
c = x->r; while(c->l->l != z) c = c->l;
x = c->l; c->l = x->r;
x->l = t->l; x->r = t->r;
}
delete t;
if (v < p->key) p->l = x; else p->r = x;

4
Deleting a node from a binary search tree

The entire removal method can now be written as:

void Dict::remove(itemType v)
{
node *p, *t, *x, *c;
z->key = v;
p = head; x = head->r;
while( v != x->key)
{ p = x; x = (v < x->key) ? x->l : x->r; }
t = x;
if( t->r == z) x = x->l;
else if (t->r-l == z) { x = x->r; x->l = t->l;}
else {
c = x->r; while(c->l->l != z) c = c->l;
x = c->l; c->l = x->r;
x->l = t->l; x->r = t->r;
}
delete t;
if (v < p->key) p->l = x; else p->r = x;
}

5
Deleting a node from a binary search tree

Exercise

Redraw the following trees after the the node with key value 16 has been deleted.

12

8 16

6
10 13 20

14 24

12

8 16

6
10 13 20

14 19 24

17

18

6
Deleting a node from a binary search tree

Solution

12

8 20

6
10 13 24

14

12

8 17

6
10 13 20

14 19 24

18

7
Deleting a node from a binary search tree

x->l
p parent node p
subtree

z
z z subtree

S-ar putea să vă placă și