Presentation is loading. Please wait.

Presentation is loading. Please wait.

AVL Trees CSE, POSTECH.

Similar presentations


Presentation on theme: "AVL Trees CSE, POSTECH."— Presentation transcript:

1 AVL Trees CSE, POSTECH

2 Problems with BST With random insertions and deletions BST has Θ ( log N ) times for search, insert and remove But worst case behaviour is Θ ( N ) Problem is that BST’s can become unbalanced We need a rebalance operation on a BST to restore the balance property and regain Θ ( log N ) Rebalancing should be cheap enough that we could do it dynamically on every insert and remove » Preference is to have Θ ( 1 ) rebalance time

3 Balanced Binary Search Trees
If the height of a binary tree is always O(log n), we can guarantee O(log n) performance for each search tree operation Trees with a worst-case height of O(log n) are called balanced trees An example of a balanced tree is AVL (Adelson-Velsky and Landis) tree The following balance definition is used » The empty tree is balanced For every node in a non-empty tree | height ( left_sub_tree ) – height ( right_sub_tree ) | ≤ 1

4 AVL Tree Definition Binary tree.
If T is a nonempty binary tree with TL and TR as its left and right subtrees, then T is an AVL tree iff TL and TR are AVL trees, and |hL – hR|  1 where hL and hR are the heights of TL and TR, respectively

5 AVL Search Trees An AVL search tree is a binary search tree that is also an AVL tree

6 Properties of AVL Tree The height of an AVL tree with n nodes is O(log n) For every value of n, n  0, there exists an AVL tree An n-node AVL search tree can be searched in O(height) = O(log n) time A new node can be inserted into an n-node AVL search tree so that the result is an n+1 node AVL tree and insertion can be done in O(log n) time A node can be deleted from an n-node AVL search tree, n>0, so that the result is an n-1 node AVL tree and deletion can be done in O(log n) time

7 Balance Factor AVL trees are normally represented using the linked representation To facilitate insertion and deletion, a balance factor (bf) is associated with each node The balance factor bf(x) of a node x is defined as height(xleftChild) – height(xrightChild) Balance factor of each node in an AVL tree must be –1, 0, or 1

8 AVL Tree with Balance Factors
-1 1 10 40 30 45 20 35 25 60 7 3 8 1 5 Is this an AVL tree? What is the balance factor for each node in this AVL tree? Is this an AVL search tree?

9 Searching an AVL Search Trees
To search an AVL search tree, we can use the code for searching a binary search tree without any change What would be the search time complexity? O(log n)

10 Inserting into an AVL Search Trees
If we use the strategy of Program 14.5 to insert an element into an AVL search tree, the result may not be an AVL tree That is, the tree may become unbalanced If the tree becomes unbalanced, we must adjust the tree to restore balance - this adjustment is called rotation Restructure the tree by moving the nodes around while preserving the order property The operation is called a rotation » Make use of the property that a node has one parent and two direc descendents

11 Inserting into an AVL Search Tree
-1 1 10 40 30 45 20 35 25 60 7 3 8 5 9 Where is 9 going to be inserted into? After the insertion, is the tree still an AVL search tree? (i.e., still balanced?)

12 Imbalance Types After an insertion, when the balance factor of node X is –2 or 2, the node X is one of the following four imbalance types LL: new node is in the left subtree of the left subtree of X LR: new node is in the right subtree of the left subtree of X RL: new node is in the left subtree of the right subtree of X RR: new node is in the right subtree of the right subtree of X

13 Rotation Definition To switch children and parents among two or three adjacent nodes to restore balance of a tree. A rotation may change the depth of some nodes, but does not change their relative ordering.

14 Names of Rotations There are four of them
Single Left (LL) and Single Right (RR) Double Left (LR) and Double Right (RL) The names describe which way the node moves. For example, a left Single rotation moves the node down and left. Nodes always move down when rotated A LR double rotation is to rotate something to the left, and then its former parent to the right.

15 Right Rotation Definition
In a binary search tree, pushing a node A down and to the right to balance the tree. A's left child replaces A, and the left child's right child becomes A's left child. 15 22 9 12 4 A 9 4 15 12 22 Right Rotation

16 Implementation of algorithm for Case 1

17 An example of the Case 1 (Left subtree (LS) of Left child (LC))

18 Left Rotation Definition
In a binary search tree, pushing a node A down and to the left to balance the tree. A's right child replaces A, and the right child's left child becomes A's right child. 9 4 15 12 22 A 15 22 9 12 4 Left Rotation Animated rotation example:

19 Implementation of algorithm for Case 4

20 An example of the Case 4 (Left subtree of Right child)

21 AVL Trees, Double Rotation (Case 2)

22 double rotation does work
first rotate k1 and k2 counter-clockwise, then rotate k2 and k3 clockwise

23 An example of Double rotation

24 Java implementation of Case 2 & 3

25 AVL Trees, Rotation Summary
Call the node where the imbalance is detected the grandparent Call the grandchild with greatest height (where the imbalance occurs), node Call node’s parent, parent

26 Single and Double Rotations
Single rotations: the transformations done to correct LL and RR imbalances Double rotations: the transformations done to correct LR and RL imbalances The transformation to correct LR imbalance can be achieved by an RR rotation followed by an LL rotation The transformation to correct RL imbalance can be achieved by an LL rotation followed by an RR rotation (do Exercise 15.13)

27 Inserting into an AVL Search Tree
-1 1 10 40 30 45 20 35 25 60 7 3 8 5 Insert(29) Where is 29 going to be inserted into? - use the AVL-search-tree-insertion algorithm After the insertion, is the tree still an AVL search tree? (i.e., still balanced?) 29

28 Inserting into an AVL Search Tree
29 -1 1 10 40 30 45 20 35 25 60 7 3 8 5 -2 -1 What are the new balance factors for 20, 25, 29? What type of imbalance do we have? RR imbalance  new node is in the right subtree of right subtree of node 20 (node with bf = -2)  what rotation do we need? What would the left subtree of 30 look like after RR rotation?

29 After RR Rotation -1 1 10 40 30 45 35 60 7 3 8 5 25 20 29 After the RR rotation, is the resulting tree an AVL search tree? Do Exercise 15.1 - see the solution on the Web

30 Deletion from an AVL Search Tree
To delete an element from an AVL search tree, we can use Program 14.6 Deletion of a node may also produce an imbalance Imbalance incurred by deletion is classified into the types R0, R1, R-1, L0, L1, and L-1 Rotation is also needed for rebalancing Read the observations after deleting a node from an AVL search tree Read Section for deletion from an AVL search tree


Download ppt "AVL Trees CSE, POSTECH."

Similar presentations


Ads by Google