Presentation is loading. Please wait.

Presentation is loading. Please wait.

AVL Tree.

Similar presentations


Presentation on theme: "AVL Tree."— Presentation transcript:

1 AVL Tree

2 Motivation: Unbalanced Tree
Various binary search trees The time complexity of binary search trees is so different. 14 13 11 10 9 7 5 10 9 5 7 11 14 13 10 7 5 9 13 11 14

3 Motivation: Unbalanced Tree
The time complexity of binary search tree Average case: 𝑂 log 𝑛 Worst case: 𝑂(𝑛) Note: The time complexity highly depends on the order of elements to be inserted. How to improve the worst case of the BST? Maintain the BST as a balanced binary tree for every insertion.

4 What is AVL Tree? Description
Invented by Georgy Adelson-Velsky and Evgenii Landis in 1962 The first self-balancing tree It guarantees that the heights of two child subtrees of any node differ by at most one. If the height of two child subtrees differs by more than one, the rebalancing operation is performed. R L R 𝑻 𝐢𝐬 𝐛𝐚𝐥𝐚𝐧𝐜𝐞𝐝 𝒊𝒇 𝑯 𝑳 − 𝑯 𝑹 ≤𝟏 𝐟𝐨𝐫 𝐚𝐧𝐲 𝐧𝐨𝐝𝐞 𝒊𝒏 𝑻. 𝐻 𝐿 𝐻 𝑅

5 What is AVL Tree? Definition
The balance factor of a node 𝑁 is defined to be the height difference: 𝐵𝑎𝑙𝑎𝑛𝑐𝑒 𝑁 =−𝐻𝑒𝑖𝑔ℎ𝑡 𝐿𝑒𝑓𝑡𝑆𝑢𝑏𝑡𝑟𝑒𝑒 𝑁 +𝐻𝑒𝑖𝑔ℎ𝑡(𝑅𝑖𝑔ℎ𝑡𝑆𝑢𝑏𝑡𝑟𝑒𝑒 𝑁 ) A binary tree is defined to be an AVL tree if 𝐵𝑎𝑙𝑎𝑛𝑐𝑒 𝑁 ∈{−1, 0,+1} holds for every node 𝑁 in the tree. 11 15 12 13 16 18 17 19 21 7 4 6 3 +1 -1 Yellow color indicates that the balance factor of the node is ±1.

6 What is AVL Tree? Examples 6 6 6 4 7 4 4 7 5 10 10 7 13 7 13 5 14 5 9
11 14 4 6 8

7 Searching in AVL Tree Description: This is the same way in the BST.
1. Begin by examining the root node. 1.1. If the node is NULL, the element does not exist. 2. Compare the key of the root with the element. 2.1 If the element is equal to the key, the element is found. 2.2 If the element is less than the key, search a left subtree. 2.3 If the element is greater than the key, search a right subtree. 3. Repeat steps 1-2 until the element is found or the root is NULL.

8 Searching in AVL Tree Searching 8 in the AVL Tree
Compare 8 with 10. Because 8 < 10, traverse a left subtree. Compare 8 with 7. Because 8 > 7, traverse a right subtree. Compare 8 with 9, Because 8 < 9, traverse a left subtree. 10 7 13 5 9 11 14 4 6 8

9 Insertion in AVL Tree Description
Insert a node in the AVL tree in the same way as the insertion in the BST. Unlike the BST, check the balance factor of every node. If the tree becomes unbalanced, the rebalancing operation is performed. -1 10 7 5 13 -2 -1 4 10 5 4 13 -1 7 10 Inserting 4 -1 7 13 Rebalancing 5 Red color indicates that the balance factor of the node is ±2.

10 When is Balancing Violated?
Let the node that needs rebalancing be 𝜶. There are four cases: Outside cases (requiring single rotation): Insertion into left subtree of left child of 𝜶. Insertion into right subtree of right child of 𝜶. Inside cases (requiring double rotation): Insertion into right subtree of left child of 𝜶. Insertion into left subtree of right child of 𝜶. Rebalancing is performed through four separate rotation algorithms.

11 When is Balancing Violated?
Four possible cases to be unbalanced: Outside cases (requiring single rotation): Insertion into left subtree of left child of 𝜶. Insertion into right subtree of right child of 𝜶. Inside cases (requiring double rotation): Insertion into right subtree of left child of 𝜶. Insertion into left subtree of right child of 𝜶. 3 2 1 -2 -1 1 2 3 +2 +1 3 1 2 -2 +1 1 3 2 +2 -1

12 Right-Right (RR) Rotation
Insertion into left subtree of left child of 𝜶 3 2 1 -2 -1 3 2 1 -2 -1 2 1 3 Tree is imbalanced. To make balanced, we use right-right rotation which moves node one position to right. After right-right rotation, tree is balanced.

13 Right-Right (RR) Rotation
Two nodes A and B is rotated to the right from the current position. A B T1 T2 -1 T3 A B T1 T2 -1 ℎ+1 T3 -2 Inserting a node to T1

14 Right-Right (RR) Rotation
Description: Rotate A and B to the right. To rotate A, the link of its left subtree is disconnected. To rotate B, the link of its right subtree is disconnected. T2 is linked as the left subtree of A. A is linked as the right subtree of B. A B T1 T2 -1 ℎ+1 T3 -2 B T1 ℎ+1 A T2 T3

15 Right-Right (RR) Rotation
B T1 T2 -1 ℎ+1 T3 -2 A B T1 T2 -1 ℎ+1 T3 -2 A is rotated right. B T1 ℎ+1 A T2 T3 ℎ+1 B T1 -1 A T2 T3 B is rotated right. T2 is linked to A B is linked to A.

16 Left-Left (LL) Rotation
Insertion into right subtree of right child of 𝜶 1 2 3 +2 +1 1 2 3 +2 +1 2 1 3 Tree is imbalanced. To make balanced, we use left-left rotation which moves node one position to left. After left-left rotation, tree is balanced.

17 Left-Left (LL) Rotation
Two nodes A and B is rotated to the left from the current position. A B T2 T3 +1 T1 A B T2 +1 -2 ℎ+1 T1 Inserting a node to T3

18 Left-Left (LL) Rotation
Description: Rotate A and B to the left. To rotate A, the link of its right subtree is disconnected. To rotate B, the link of its left subtree is disconnected. T2 is linked as the right subtree of A. A is linked as the left subtree of B. A B T2 +1 -2 ℎ+1 T1 A T1 B T2 ℎ+1

19 Left-Left (LL) Rotation
B T2 +1 -2 ℎ+1 T1 A -2 T1 B T2 +1 ℎ+1 A is rotated left. A T1 B T2 ℎ+1 B is linked to A. A T1 B T2 +1 ℎ+1 B is rotated left. T2 is linked to A

20 Left-Right (LR) Rotation
Insertion into right subtree of left child of 𝜶. 3 2 1 -2 -1 3 1 2 -2 +1 3 1 2 -2 +1 2 1 3 Tree is imbalanced LL rotation RR rotation After left-right rotation, tree is balanced.

21 Left-Right (LR) Rotation
First, rotate B and C to the left from the current position, and then rotate A an C to the right. -2 B C T2 ℎ+1 T1 T3 T4 A -1 A +1 B -1 T4 C ℎ+1 T1 ℎ+1 T2 T3 ℎ+1 Inserting a node to T2

22 Left-Right (LR) Rotation
Description Perform LL rotation for B and C. Perform RR rotation for A and C. B C -1 +1 ℎ+1 T1 T3 T4 A -2 T2 B C ℎ+1 T1 T3 T4 A T2 +1

23 Left-Right (LR) Rotation
B C -1 ℎ+1 T1 T3 T2 B C -1 ℎ+1 T1 T3 T2 B is rotated left. B C -2 ℎ+1 T1 T3 T2 B C -1 ℎ+1 T1 T3 T2 C is rotated left. T2 is linked to B. B is linked to C.

24 Left-Right (LR) Rotation
C -2 T3 T2 ℎ+1 T4 A ℎ+2 C -2 T3 T2 ℎ+1 T4 A ℎ+2 B A is rotated right. B B C ℎ+1 T1 T3 T4 A T2 +1 C -2 T3 T2 ℎ+1 T4 A +1 ℎ+2 B C is rotated right. T3 is linked to A. A is linked to C.

25 Right-Left (RL) Rotation
Insertion into left subtree of right child of 𝜶. 1 3 2 +2 -1 1 3 2 +2 -1 1 2 3 +2 +1 2 1 3 Tree is imbalanced RR rotation LL rotation After right-left rotation, tree is balanced.

26 Right-Left (RL) Rotation
First, rotate B and C to the right from the current position, and then rotate A an C to the left. B C T2 ℎ+1 T4 T3 T1 A +1 +2 A -1 B +1 T1 C ℎ+1 T4 T2 ℎ+1 T3 ℎ+1 Inserting a node to T3

27 Right-Left (RL) Rotation
Description Perform RR rotation for B and C. Perform LL rotation for A and C. +2 A C -1 -1 B +1 A B T1 C ℎ+1 T4 T1 T2 T3 T4 ℎ+1 ℎ+1 T2 ℎ+1 T3 ℎ+1 ℎ+1

28 Right-Left (RL) Rotation
B C T2 +1 -1 ℎ+1 T4 T3 B C T2 +1 -1 ℎ+1 T4 T3 B is rotated right. B C T2 +2 ℎ+1 T4 T3 B C T2 +1 ℎ+1 T4 T3 C is rotated right. T3 is linked to B. B is linked to C.

29 Right-Left (RL) Rotation
C -2 T2 T3 ℎ+1 T1 A ℎ+2 B C -2 T2 T3 ℎ+1 T1 A ℎ+2 B A is rotated left. C -1 T2 T3 ℎ+1 T1 A -2 ℎ+2 B C -1 T2 ℎ+1 T1 A B T4 T3 C is rotated left. T2 is linked to A. A is linked to C.

30 Insertion Example in AVL Tree
Inserting 1, 2, 3, 4, 5, 6, 7, 8, and 9

31 Insertion Example in AVL Tree
Inserting 3, 1, 4, 8, 6, 9, 7, and 5

32 Deletion in AVL Tree This is similar to deletions in the BST, but need to perform rebalancing operation if necessary. Possible cases for deletion Case 1: Deleting a node with no children Simply remove the node from the tree. Case 2: Deleting a node with one child Remove the node and replace it with its child node. Case 3: Deleting a node with two children Choose either its inorder predecessor node or successor node as a replacement node.

33 Summary of AVL Tree Time complexity of AVL tree
The operations for searching, insertion, and deletion are bound by 𝑂(ℎ), where ℎ is the height of AVL tree. where 𝑛 is the number of nodes in the AVL tree. Algorithm Average case Worst case Searching 𝑂( log 𝑛 ) Insertion Deletion

34

35

36

37

38


Download ppt "AVL Tree."

Similar presentations


Ads by Google