www.delorie.com/gnu/docs/avl/libavl_189.html | search |
Buy GNU books! | |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Our usual algorithm for deletion looks at the right subtree of the node to be deleted, so we call it "right-looking." The outline for this kind of deletion is the same as in TBST deletion (see section 8.7 Deletion):
if (p->rtbst_rtag == RTBST_THREAD) |
Each of the four cases, presented below, is closely analogous to the same case in TBST deletion.
In this case, node p has a right thread and a left child. As in a TBST, this means that after deleting p we must update the right thread in p's former left subtree to point to p's replacement. The only difference from <@xref{\NODE\, , Case 1 in TBST deletion.>,260} is in structure members:
struct rtbst_node *t = p->rtbst_link[0]; while (t->rtbst_rtag == RTBST_CHILD) t = t->rtbst_link[1]; t->rtbst_link[1] = p->rtbst_link[1]; q->rtbst_link[dir] = p->rtbst_link[0]; |
If node p is a leaf, then there are two subcases, according to whether p is a left child or a right child of its parent q. If dir is 0, then p is a left child and the pointer from its parent must be set to NULL. If dir is 1, then p is a right child and the link from its parent must be changed to a thread to its successor.
In either of these cases we must set q->rtbst_link[dir]: if dir is 0, we set it to NULL, otherwise dir is 1 and we set it to p->rtbst_link[1]. However, we know that p->rtbst_link[0] is NULL, because p is a leaf, so we can instead unconditionally assign p->rtbst_link[dir]. In addition, if dir is 1, then we must tag q's right link as a thread.
If q is the pseudo-root, then dir is 0 and everything works out fine with no need for a special case.
q->rtbst_link[dir] = p->rtbst_link[dir]; if (dir == 1) q->rtbst_rtag = RTBST_THREAD; |
Code for this case, where p has a right child r that itself has no left child, is almost identical to <@xref{\NODE\, , Case 3 in TBST deletion.>,262}. There is no left tag to copy, but it is still necessary to chase down the right thread in r's new left subtree (the same as p's former left subtree):
r->rtbst_link[0] = p->rtbst_link[0]; if (r->rtbst_link[0] != NULL) |
Code for case 4, the most general case, is very similar to <@xref{\NODE\, , \TITLE\}.>{Case 4 in TBST deletion,263}. The only notable difference is in the subcase where s has a right thread: in that case we just set r's left link to NULL instead of having to set it up as a thread.
struct rtbst_node *s; for (;;) |
Exercises:
1. Rewrite <@xref{\NODE\, , Case 4 in right-looking RTBST deletion.>,387} to replace the deleted node's rtavl_data by its successor, then delete the successor, instead of shuffling pointers. (Refer back to Exercise 4.8-3 for an explanation of why this approach cannot be used in libavl.) [answer]
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
webmaster | delorie software privacy |
Copyright © 2003 by The Free Software Foundation | Updated Jun 2003 |