Download presentation
Presentation is loading. Please wait.
Published byGavin Parks Modified over 8 years ago
1
AVL trees1 AVL Trees Height of a node : The height of a leaf is 1. The height of a null pointer is zero. The height of an internal node is the maximum height of its children plus 1. Note that this definition of height is different from the tree height definition that we used before. The difference is that the height of a leaf is zero according to the previous definition.
2
AVL trees2 AVL tree property An AVL tree is a binary search tree with the additional AVL tree property : For any node x, the heights of left(x) and right(x) differ by at most 1. 15 1015102013 15 10 20 23 3 18 1 NOT an AVL tree. 1
3
AVL trees3 Height of an AVL tree Let N h represent the number of nodes of an AVL tree of height h. Let n h be the lower bound N h, i.e., N h ≥ n h By definition, we have n 1 = 1, and n 2 = 2, and n 3 = 4, etc, and n h+1 ≥ n h. 15 10 15 10 20 13 n 1 = 1n 2 = 2n 3 = 4
4
AVL trees4 Height of an AVL tree Let x be the root of an AVL Tree. To keep the number of nodes in the AVL tree of height h small, one strategy is to make the height of left(x)=h-1 and the height of right(x)=h-2. x n h-1 n h-2 n h =n h-1 +n h-2 +1 h
5
AVL trees5 Height of an AVL tree We then have : n h = n h-1 + n h-2 + 1 ≥ 2 n h-2 + 1 > 2 n h-2 > 2 2 n h-4 By repeated substitution, we obtain the general form n h > 2 i n h-2i
6
AVL trees6 Height of an AVL tree N h ≥ n h > 2 i n h-2i N h ≥ 2 h/2 (Since n 1 = 1 and n 2 = 2) log 2 (N h )≥ h/2 h ≤ 2 log 2 (N h ) h = O(log N h ) In other words, if n is the number of nodes of an AVL tree, then its height h is at most (≤) O(log n). This implies that operations including search, insert, delete, successor, and predecessor on an AVL tree will take O(h)=O(log n).
7
AVL trees7 Rotations The insert and delete operations of AVL tree are the same as binary search tree (BST). Since an insertion (deletion) involves adding (deleting) a tree node, this can only increase (decrease) the heights of some subtree(s) by 1. Thus, the AVL tree property may be violated. If the AVL tree property is violated at a node x, it means that the heights of left(x) and right(x) differ by exactly 2.
8
AVL trees8 Rotations After the insertion or deletion operations, we need to examine the tree and see if any node violates the AVL tree property. If the AVL tree property is violated at node x, single or double rotation will be applied to x to restore the AVL tree property. Rotation will be applied in a bottom-up manner starting at the place of insertion (deletion). Thus, when we perform a rotation at x, the AVL tree property is restored at all proper descendants of x. This fact is important.
9
AVL trees9 Insertion Perform normal BST insertion. Check and restore AVL tree property. Trace from path of inserted leaf towards the root, and check if the AVL tree property is violated. Check to see if heights of left(x) and right(x) height differ by at most 1. If the AVL tree property is violated, there are 4 rotation cases to restore the AVL tree property.
10
AVL trees10 Restore AVL tree property – Case 1 If the AVL tree property is violated at x, let the height of x be h+3 : If the height of left(x) is h+2 then Case 1: If the height of left(left(x)) is h+1, we single rotate with left child.
11
AVL trees11 Restore AVL tree property – Case 1 Case 1 : Single rotate with left child. h+3 h+2 h+3
12
AVL trees12 Restore AVL tree property – Case 1 Case 1 : Single rotate with left child.
13
AVL trees13 Restore AVL tree property – Case 2 If the AVL tree property is violated at x, let the height of x be h+3 : If the height of left(x) is h+2 then Case 1: If the height of left(left(x)) is h+1, we single rotate with left child. Case 2: Otherwise, the height of right(left(x)) is h+1, then we double rotate with left child.
14
AVL trees14 Restore AVL tree property – Case 2 Case 2 : Double rotate with left child. h+2 h+3
15
AVL trees15 Restore AVL tree property – Case 2 Case 2 : Double rotate with left child.
16
AVL trees16 Restore AVL tree property – Case 3 If the AVL tree property is violated at x, let the height of x be h+3 : If the height of left(x) is h+2 then Case 1: If the height of left(left(x)) is h+1, … Case 2: Otherwise, the height of right(left(x)) is h+1, then we double rotate with left child. Otherwise, height of right(x) is h+2 Case 3: (Mirror image of the case 1) If the height of right(right(x)) is h+1, then we single rotate with right child. Case 4: (Mirror image of the case 2) Otherwise, the height of left(right(x)) is h+1, then we double rotate with right child.
17
AVL trees17 Restore AVL tree property – Case 3 Case 3 : Single rotate with right child. h+2
18
AVL trees18 Restore AVL tree property – Case 3 Case 3 : Single rotate with right child.
19
AVL trees19 Restore AVL tree property – Case 4 If the AVL tree property is violated at x, let the height of x be h+3 : If the height of left(x) is h+2 then Case 1: If the height of left(left(x)) is h+1, … Case 2: Otherwise, the height of right(left(x)) is h+1, then we double rotate with left child. Otherwise, height of right(x) is h+2 Case 3: (Mirror image of the case 1) If the height of right(right(x)) is h+1, then we single rotate with right child. Case 4: (Mirror image of the case 2) Otherwise, the height of left(right(x)) is h+1, then we double rotate with right child.
20
AVL trees20 Restore AVL tree property – Case 4 Case 4 : Double rotate with right child. h+2
21
AVL trees21 Restore AVL tree property – Case 4 Case 4 : Double rotate with right child.
22
AVL trees22 Insertion Trace from path of inserted leaf towards the root, and check if the AVL tree property is violated. Perform rotation if necessary. For insertion, once we perform (single or double) rotation at a node x, the AVL tree property is already restored. We need not to perform any rotation at any ancestor of x. Thus one rotation is enough to restore the AVL tree property. There are 4 different cases (actually 2), so don’t mix up them!
23
AVL trees23 Insertion The time complexity to perform a rotation is O(1). The time complexity to insert, and find a node that violates the AVL property is dependent on the height of the tree, which is O(log(n)). So insertion takes O(log(n)).
24
AVL trees24 Deletion Delete a node x as in ordinary BST (Note that x is either a leaf or x has exactly one child.). Check and restore the AVL tree property. Trace from path of deleted node towards the root, and check if the AVL tree property is violated. Similar to an insertion operation, there are four cases to restore the AVL tree property.
25
AVL trees25 Deletion The only difference from insertion is that after we perform a rotation at x, we may have to perform a rotation at some ancestors of x. It may involve several rotations. Therefore, we must continue to trace the path until we reach the root. The time complexity to delete a node is dependent on the height of the tree, which is also O(log(n)).
26
AVL trees26 Deletion : no rotation No need to rotate.
27
AVL trees27 Deletion : one rotation Case 3 : Single rotate with right child.
28
AVL trees28 Deletion : several rotations Case 3 : Single rotate with right child. Case 4 : Double rotate with right child.
29
AVL trees29 Summary of AVL Trees Maintains a balanced tree by posing an AVL tree property: guarantees the height of the AVL tree be O(log n) implies that functions search, min, and max, insert, and delete will be performed in O(logn) Modifies the insertion and deletion routine Performs single or double rotation to restore the AVL tree property if necessary. Requires a little more work for insertion and deletion.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.