CSC211 · 2081 · 10 marks
What is AVL tree? How heap differ from tree? Construct an AVL tree for data 24,12,8,15,35,30,57,40,45 and 78.
What is AVL tree? How heap differ from tree? Construct an AVL tree for data 24,12,8,15,35,30,57,40,45 and 78.[10]
AVL Tree, Heap versus Tree, and Construction
What is an AVL tree?
An AVL tree, named after Adelson-Velsky and Landis who proposed it in 1962, is a self-balancing binary search tree. It keeps the ordering of a binary search tree, every key in the left subtree of a node being smaller than the node and every key in the right subtree larger, and adds a height condition at every node: the heights of the left and right subtrees may differ by at most one.
The condition is expressed through the balance factor
$$BF = h(\text{left subtree}) - h(\text{right subtree})$$
which must remain one of $-1$, $0$ or $+1$. If an insertion or deletion drives some balance factor to $+2$ or $-2$, the tree is repaired by a rotation, a local rearrangement of a few links that preserves the ordering while reducing the height. There are four cases, named by the two links leading from the unbalanced node towards the newly inserted key: LL is fixed by a single right rotation, RR by a single left rotation, LR by a left rotation followed by a right rotation, and RL by a right rotation followed by a left rotation. Because the height of an AVL tree on $n$ nodes stays $O(\log n)$, search, insertion and deletion all run in $O(\log n)$ time.
How a heap differs from a tree
| Feature | Heap | Tree (BST or general) |
|---|---|---|
| Structure | Always a complete binary tree, every level full except possibly the last, which fills from the left | May or may not be complete |
| Ordering | Parent is greater than or equal to its children (max-heap) or less than or equal to them (min-heap) | In a BST, keys in the left subtree are smaller than the node and keys in the right subtree are larger |
| Order among siblings | None, only the parent-child relation is constrained | Strict left and right positions carry meaning in a BST |
| Search for an arbitrary key | Inefficient, $O(n)$ | $O(\log n)$ in a balanced BST |
| Typical use | Priority queues, heap sort, finding the extreme element | Searching, ordered traversal, hierarchical data |
| Usual implementation | An array, with children of index $i$ at $2i+1$ and $2i+2$ | Linked nodes holding child pointers |
| Balance | Balanced by definition | May degenerate unless a balancing scheme such as AVL is used |
The essential point is that a heap constrains only the relation between a parent and its children and therefore supports fast access to the minimum or maximum, while a binary search tree constrains the whole ordering left to right and therefore supports fast search for any key.
Constructing an AVL tree for 24, 12, 8, 15, 35, 30, 57, 40, 45, 78
Throughout, the height of a leaf is counted as 1 and the height of an empty subtree as 0, and
$$BF(x) = h(\text{left subtree of } x) - h(\text{right subtree of } x)$$
must stay in ${-1, 0, +1}$. After each insertion the balance factors are checked upward from the new node, so that the lowest unbalanced ancestor is the one rotated.
Insert 24
24
The first key becomes the root and $BF(24) = 0$.
Insert 12
Since $12 < 24$, the key goes to the left of the root.
24
/
12
$BF(24) = 1 - 0 = +1$, still within range.
Insert 8
Since $8 < 24$ and $8 < 12$, the key becomes the left child of 12.
24
/
12
/
8
Now $BF(12) = 1 - 0 = +1$ is fine, but $BF(24) = 2 - 0 = +2$, so 24 is unbalanced. From 24 the path to the new key goes left and then left again, an LL case, repaired by a single right rotation at 24.
12
/ \
8 24
$BF(12) = 1 - 1 = 0$, so the tree is balanced again.
Insert 15
Since $15 > 12$ the key goes right to 24, and $15 < 24$ makes it the left child of 24.
12
/ \
8 24
/
15
$BF(24) = 1 - 0 = +1$ and $BF(12) = 1 - 2 = -1$. No rotation is needed.
Insert 35
Since $35 > 12$ and $35 > 24$, the key becomes the right child of 24.
12
/ \
8 24
/ \
15 35
$BF(24) = 1 - 1 = 0$ and $BF(12) = 1 - 2 = -1$. No rotation is needed.
Insert 30
Since $30 > 12$ the key goes right to 24, $30 > 24$ sends it right to 35, and $30 < 35$ makes it the left child of 35.
12
/ \
8 24
/ \
15 35
/
30
Checking upward from the new node, $BF(35) = 1 - 0 = +1$ is fine and
$$BF(24) = h(15) - h(35) = 1 - 2 = -1$$
is also fine, so node 24 is balanced. The root, however, gives
$$BF(12) = h(8) - h(24) = 1 - 3 = -2$$
so the lowest unbalanced node is the root 12, not 24. From 12 the path to the new key goes right to 24 and then right again into the subtree rooted at 35, which is the RR case, repaired by a single left rotation at 12. In that rotation 24 moves up to become the root and hands its left child 15 to 12.
24
/ \
12 35
/ \ /
8 15 30
Now $BF(12) = 1 - 1 = 0$, $BF(35) = 1 - 0 = +1$ and $BF(24) = 2 - 2 = 0$, so the tree is balanced.
Insert 57
Since $57 > 24$ and $57 > 35$, the key becomes the right child of 35.
24
/ \
12 35
/ \ / \
8 15 30 57
$BF(35) = 1 - 1 = 0$ and $BF(24) = 2 - 2 = 0$. No rotation is needed.
Insert 40
Since $40 > 24$ the key goes right to 35, $40 > 35$ sends it right to 57, and $40 < 57$ makes it the left child of 57.
24
/ \
12 35
/ \ / \
8 15 30 57
/
40
Here $BF(57) = 1 - 0 = +1$,
$$BF(35) = h(30) - h(57) = 1 - 2 = -1, \qquad BF(24) = h(12) - h(35) = 2 - 3 = -1$$
Every balance factor is in range, so no rotation is needed.
Insert 45
The key travels right to 35, right to 57, left to 40, and since $45 > 40$ it becomes the right child of 40.
24
/ \
12 35
/ \ / \
8 15 30 57
/
40
\
45
Now $BF(40) = 0 - 1 = -1$ is fine, but
$$BF(57) = h(40) - 0 = 2 - 0 = +2$$
so 57 is the lowest unbalanced node. From 57 the path goes left to 40 and then right to 45, which is the LR case: first a left rotation at 40, then a right rotation at 57. The subtree rooted at 57 therefore becomes
45
/ \
40 57
and the whole tree is
24
/ \
12 35
/ \ / \
8 15 30 45
/ \
40 57
with $BF(45) = 1 - 1 = 0$, $BF(35) = 1 - 2 = -1$ and $BF(24) = 2 - 3 = -1$, all in range.
Insert 78
The key travels right to 35, right to 45, right to 57, and becomes the right child of 57.
24
/ \
12 35
/ \ / \
8 15 30 45
/ \
40 57
\
78
Checking upward, $BF(57) = 0 - 1 = -1$ and $BF(45) = h(40) - h(57) = 1 - 2 = -1$ are fine, but
$$BF(35) = h(30) - h(45) = 1 - 3 = -2$$
so 35 is the lowest unbalanced node. From 35 the path goes right to 45 and then right again into the subtree rooted at 57, the RR case, repaired by a single left rotation at 35, in which 45 moves up and hands its left child 40 to 35.
24
/ \
12 45
/ \ / \
8 15 35 57
/ \ \
30 40 78
Now $BF(35) = 1 - 1 = 0$, $BF(57) = 0 - 1 = -1$, $BF(45) = 2 - 2 = 0$ and $BF(24) = 2 - 3 = -1$, so the tree is balanced.
Final AVL tree
24
/ \
12 45
/ \ / \
8 15 35 57
/ \ \
30 40 78
The balance factors are $0$ at 8, 15, 30, 40 and 78, $0$ at 12, $0$ at 35, $-1$ at 57, $0$ at 45 and $-1$ at the root 24, every one of them inside ${-1, 0, +1}$, so the tree satisfies the AVL property. It stands four levels deep, the fewest possible for 10 nodes, and four rotations were needed in all: a right rotation at 24 (LL) when 8 was inserted, a left rotation at 12 (RR) when 30 was inserted, a left-right double rotation at 57 when 45 was inserted, and a left rotation at 35 (RR) when 78 was inserted.
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.