In this chapter, we study a binary search tree data structure, the
. This structure is based on the common wisdom that,
when something goes wrong, the first thing people tend to do is find
someone to blame (the scapegoat).
Once blame is firmly
established, we can leave the scapegoat to fix the problem.
A
keeps itself balanced by partial rebuilding
operations.
During a partial rebuilding operation, an entire subtree is
deconstructed and rebuilt into a perfectly balanced subtree. There are
many ways of rebuilding a subtree rooted at node
into a perfectly
balanced tree. One of the simplest is to traverse
's subtree,
gathering all its nodes into an array,
, and then to recursively
build a balanced subtree using
. If we let
,
then the element
becomes the root of the new subtree,
get stored recursively in the left subtree
and
get stored recursively in the
right subtree.
void rebuild(Node *u) { int ns = BinaryTree<Node>::size(u); Node *p = u->parent; Node **a = new Node*[ns]; packIntoArray(u, a, 0); if (p == nil) { r = buildBalanced(a, 0, ns); r->parent = nil; } else if (p->right == u) { p->right = buildBalanced(a, 0, ns); p->right->parent = p; } else { p->left = buildBalanced(a, 0, ns); p->left->parent = p; } delete[] a; } int packIntoArray(Node *u, Node **a, int i) { if (u == nil) { return i; } i = packIntoArray(u->left, a, i); a[i++] = u; return packIntoArray(u->right, a, i); }A call to