Presentation is loading. Please wait.

Presentation is loading. Please wait.

Presented by: Chien-Pin Hsu CS146 Prof. Sin-Min Lee.

Similar presentations


Presentation on theme: "Presented by: Chien-Pin Hsu CS146 Prof. Sin-Min Lee."— Presentation transcript:

1 Presented by: Chien-Pin Hsu CS146 Prof. Sin-Min Lee

2 Outline IntroductionDefinitions Height-Balanced Trees Picking Out AVL Trees Why AVL Trees? Added Complexity Violations The Balance Factor What is a rotation? Single Rotations Double Rotations

3 Introduction AVL tree is the first balanced binary search tree (name after its discovers, Adelson-Velskii and Landis). The AVL tree is a balanced search tree that has an additional balance condition. The balance condition must be easy to maintain and ensures that the depth of the tree is O(logN) The balance condition must be easy to maintain and ensures that the depth of the tree is O(logN)

4 Definitions An AVL tree is a binary search tree that is height balanced meaning that it has these two properties.. 1. The sub-tree of every node must differ in height by at most one. 1. The sub-tree of every node must differ in height by at most one. 2. Every sub-tree is an AVL tree 2. Every sub-tree is an AVL tree

5 Height-Balanced Trees Height of a tree is the length of the longest path from a root to a leaf. A binary search tree T is a height-balanced k-tree or HB[k]-tree, if each node in the tree has the HB[k] property. A node has the HB[k] property if the height of the left and right sub-trees of the node differ in height by at most k. A HB[1] tree is called an AVL tree.

6 Picking Out AVL Trees Notation: NODE = Left-Ht, Right-Ht Yes! 5 = 2, 1 4 = 1, 0 3 = 0, 0 7 = 0, 0 No! 10 = 3, 1

7 Picking Out AVL Trees (Cont’d) Notation: NODE = Left-Ht, Right-Ht No! 8 = 4, 2 4 = 3, 1 3 = 2, 0

8 Why AVL Trees? The worst case for Binary Search Trees: Insert: O(n). Insert: O(n). Delete: O(n). Delete: O(n). Search :O(n). Search :O(n). The worst case for AVL trees: The worst case for AVL trees: Insert: O(log n). Insert: O(log n). Delete: O(log n). Delete: O(log n). Search:O(log n). Search:O(log n). Recall when comparing the time complexity Recall when comparing the time complexity O(log n) < O(n) O(log n) < O(n)

9 Why AVL Trees? (Cont’d) AVL trees allow for logarithmic Inserts and Deletes, and they allow for easy traversals, such as the “In-order” traversal if we want to sort our elements.

10 Added Complexity Although we have made the Insertion, Deletion & Searching better, an additional amount of complexity does arise during the Insertions and the Deletions. After Inserting and Deleting for an AVL tree, the new tree might violate the two properties of the AVL tree.

11 Violations No! 5 = 2, 0 No! 5 = 3,1 4 = 2,0 Notation: NODE = Left-Ht, Right-Ht

12 The Balance Factor In order to detect when a “violation” of a AVL tree occurs, we need to have each node keep track of the difference in height between its right and left sub-trees. Notation for the balance factor: bf (node) = Left-Ht – Right- Ht bf (node) = Left-Ht – Right- Ht To be a valid AVL Tree, -1 <= bf(x) <= 1 for all nodes.

13 The Balance Factor (Cont’d) Notation for the balance factor: bf (node) = Left-Ht – Right- Ht bf (node) = Left-Ht – Right- Ht bf(8) = 3 – 3 = 0 bf(4) = 2 – 1 = 1 bf(10) = 1 – 2 = -1 bf(3) = 1 – 0 = 1 bf(5) = 0 – 0 = 0 bf(9) = 0 – 0 = 0 bf(11) = 0 – 1 = -1 bf(1) = 0 – 0 = 0 bf(12) = 0 – 0 = 0 Valid Tree!!

14 The Balance Factor (Cont’d) Notation for the balance factor: bf (node) = Left-Ht – Right- Ht bf (node) = Left-Ht – Right- Ht bf(8) = 2 – 0 = 2 bf(4) = 1 – 0 = 1 Bf(3) = 0 – 0 = 0 Invalid Tree!!

15 What is a rotation? A rotation is an operation to rearrange nodes to maintain balance of an AVL tree after adding and removing a node There are two case to perform the rotation 1) Single Rotation 1) Single Rotation 2) Double Rotation 2) Double Rotation When to perform rotations? we need to perform a rotation anytime a node’s balance factor is 2 or -2 we need to perform a rotation anytime a node’s balance factor is 2 or -2

16 Single Rotations (Cont’d) Algorithm for right rotation Algorithm rotateRight(nodeN) nodeC = left child of nodeN Set nodeN’s left child to nodeC’s right child Set nodeC’s right child to nodeN

17 Single Rotations After inserting (a) 60; (b) 50; (c) 20 into an initially empty binary search tree, the tree is not balanced; d) a corresponding AVL tree rotates its nodes to restore balance.

18 Single Rotations (Cont’d) Algorithm for left rotation Algorithm rotateLeft(nodeN) nodeC = right child of nodeN Set nodeN’s right child to nodeC’s left child Set nodeC’s left child to nodeN

19 Single Rotations (Cont’d) (a) Adding 80 to tree does not change the balance of the tree; (b) a subsequent addition of 90 makes tree unbalanced; (c) left rotation restores balance.

20 Double Rotations Before and after an addition to an AVL sub-tree that requires both a right rotation and a left rotation to maintain its balance.

21 Double Rotations (Cont’d) Before and after an addition to an AVL sub-tree that requires both a left and right rotation to maintain its balance.

22 Double Rotations (Cont’d) Algorithm rotateLeftRight(nod eN) nodeC = left child of nodeN Set nodeN’s left child to the sub-tree produced by rotateLeft(nodeC) rotateRight(nodeN)

23 Double Rotations (Cont’d) (a) Inserting 10,40,and 55 maintain its balance (b) Inserting 35 destroys the balance (c) After a left rotation (d) After a right rotation

24 Double Rotations (Cont’d) Algorithm rotateRightLeft(nodeN) nodeC = right child of nodeN Set nodeN’s right child to the sub-tree produced by rotateRight(nodeC) rotateLeft(nodeN)

25 Double Rotations (Cont’d) (a) Adding 70 to the tree destroy its balance. To restore the balance, perform both (b) a right rotation and (c) a left rotation.

26 The End


Download ppt "Presented by: Chien-Pin Hsu CS146 Prof. Sin-Min Lee."

Similar presentations


Ads by Google