Balanced Search Trees
Balanced Search Trees
Balanced Search Trees
1a 1b
5
Solution for Case 1a
We can rotate the left sub-tree to the right with respect to ‘3’
Solution for Case 1
xr − xl ≤ 1,
Therefore:
1. We make T2 (the right subtree of node a) the left subtree of node b.
2. We make node b the right child of node a.
3. Node a becomes the root node of the reconstructed tree, as shown in Figure
AVL-16.
• In Figure AVL-18, the dotted rectangle shows that a new item is inserted in the
subtree, T2 or T3, causing the subtree to grow in height.
• All keys in T3 are smaller than the key in node c.
• All keys in T3 are larger than the key in node b.
• All keys in T2 are smaller than the key in node b.
• All keys in T2 are larger than the key in node a.
• After insertion, the subtrees with root nodes a and b are still AVL trees.
• The balance criteria is violated at the root node, c, of the tree.
• The balance factors of node c, bf(c) = −1, and node a, bf(a) = 1, are opposite.
• This is an example of double rotation.
• One rotation is required at node a and another rotation is required at node c.
• If the balance factor of the node where the tree is to be reconstructed and the
balance factor of the higher subtree are opposite, that node requires a double
rotation.
• First we rotate the tree at node a and then at node c.
• Now the tree at node a is right high, so we make a left rotation at a. Next,
because the tree at node c is left high, we make a right rotation at c.
• Figure AVL-18 shows the resulting tree (which is to the right of the tree after
insertion). Figure AVL-19, however, shows both rotations in sequence.
• Suppose that the tree is to be reconstructed, by rotation, at node x. Then the subtree
with the root node x requires either a single or a double rotation.
1. Suppose that the balance factor of the node x and the balance factor of the root
node of the higher subtree of x have the same sign, that is, both positive or both
negative.
a. If these balance factors are positive, make a single left rotation at x.
b. If these balance factors are negative, make a single right rotation at x.
2. Suppose that the balance factor of the node x and the balance factor of the higher
subtree of x are opposite in sign. To be specific, suppose that the balance factor
of the node x prior to insertion was −1 and suppose that y is the root node of
the left subtree of x. After insertion, the balance factor of the node y is 1. That is,
after insertion, the right subtree of node y grew in height. In this case, we
require a double rotation at x. First we make a left rotation at y (because y is right
high). Then we make a right rotation at x.
• The following steps describe the function insertIntoAVL:
1. Create a node and assign the item to be inserted to the info field of this
node.
2. Search the tree and find the place for the new node in the tree.
3. Insert the new node in the tree.
4. Backtrack the path, which was constructed to find the place for the new
node in the tree, to the root node. If necessary, adjust the balance factors
of the nodes, or reconstruct the tree at a node on the path.
• The function insertIntoAVL also uses a bool member variable,
isTaller, to indicate to the parent whether the subtree grew in height
or not.
To delete an item from an AVL tree, first we find the node containing the item to
be deleted. The following four cases arise:
Case 1: The node to be deleted is a leaf.
Case 2: The node to be deleted has no right child, that is, its right subtree is
empty.
Case 3: The node to be deleted has no left child, that is, its left subtree is empty.
Case 4: The node to be deleted has a left child and a right child.