# CPSC 252 AVL Trees Page 1 AVL Trees Motivation: We have seen that when data is inserted into a BST in sorted order, the BST contains only one branch (it.

## Presentation on theme: "CPSC 252 AVL Trees Page 1 AVL Trees Motivation: We have seen that when data is inserted into a BST in sorted order, the BST contains only one branch (it."— Presentation transcript:

CPSC 252 AVL Trees Page 1 AVL Trees Motivation: We have seen that when data is inserted into a BST in sorted order, the BST contains only one branch (it is essentially a linked list) and the search time degrades to O(N). In this section we will consider a variation of a BST that ensures that the tree remains “balanced”. The AVL tree (named for its inventors: Adelson-Velskii and Landis) was the first balanced binary search tree and was invented in 1962. Definition: An AVL tree is a binary search tree with the additional balance property that, for any node in the tree, the height of its left and right sub-trees differs by at most 1. The height of an empty tree is taken to be –1. It can be shown that provided this balance property is maintained, the search operation is O( log N ) in the worst case.

CPSC 252 AVL Trees Page 2 It is important to realize that after an insertion into a tree that is already balanced, the only nodes that have their balances altered are those that are on the path from the insertion point to the root. Consider what happens if we insert a 1 into the BST below: 11 9 105 7 2 14 12 We will show that the entire tree can be rebalanced by rebalancing the lowest sub-tree that becomes unbalanced due to the insertion.

CPSC 252 AVL Trees Page 3 How do we determine if a node is unbalanced? We store additional data in each node that indicates one of the following possible states: E – the left and right sub-trees have equal height RH – the right sub-tree is higher (has a longer path to a leaf) LH – the left sub-tree is higher 11 9 105 7 2 14 12

CPSC 252 AVL Trees Page 4 When we perform an insertion into a tree, we update the balance indicator for each node on the path from the insertion point to the root until we find the first node that is not balanced: 11 9 105 7 2 14 12 1 We now attempt to balance the sub-tree that has this unbalanced node as its root. This can be done using what is called a tree rotation…

CPSC 252 AVL Trees Page 5 We rotate the unbalanced node with its left child: 111412 The code to perform this operation is very straightforward. Assume that ubNode is a pointer to the unbalanced node:

CPSC 252 AVL Trees Page 6 To this point, we have examined only one special case. We must now show that we can balance a tree just as easily in every case. Suppose that we need to rebalance a node N. Such rebalancing may occur because of any one of the following operations: 1. N is currently LH and we insert into the left sub-tree of the left child of N 2. N is currently LH and we insert into the right sub-tree of the left child of N

CPSC 252 AVL Trees Page 7 3. N is currently RH and we insert into the left sub-tree of the right child of N 4. N is currently RH and we insert into the right sub-tree of the right child of N

CPSC 252 AVL Trees Page 8 Note that cases 1 and 4 are mirror images of each other – the insertion occurs on the “outside” of the tree: either to the left sub- tree of the left child (which we will call the LL case) or to the right sub-tree of the right child (which we will call the RR case). Similarly, cases 2 and 3 are mirror images of each other – the insertion occurs on the “inside” of the tree: either to the right sub- tree of the left child (which we will call the LR case) or to the left sub-tree of the right child (which we will call the RL case). We will see that balancing the tree in the LL and RR cases is fairly straightforward while the LR and RL cases require a little more work.

Download ppt "CPSC 252 AVL Trees Page 1 AVL Trees Motivation: We have seen that when data is inserted into a BST in sorted order, the BST contains only one branch (it."

Similar presentations