Download presentation

Presentation is loading. Please wait.

Published byAbby Watling Modified over 2 years ago

1
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.

2
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.

3
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

4
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…

5
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:

6
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

7
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

8
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.

Similar presentations

OK

AVL Trees Balancing. The AVL Tree An AVL tree is a balanced binary search tree. What does it mean for a tree to be balanced? It means that for every node.

AVL Trees Balancing. The AVL Tree An AVL tree is a balanced binary search tree. What does it mean for a tree to be balanced? It means that for every node.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on earth dam breach Ppt on retail marketing mix Ppt on electricity from wastewater A ppt on time management Ppt on solar energy in india Ppt on carbon and its compounds class Economics ppt on demand and supply Ppt on history of badminton in china Thyroid gland anatomy and physiology ppt on cells Ppt on library management system project