260 likes | 509 Views
Outline. IntroductionDefinitionsHeight-Balanced TreesPicking Out AVL TreesWhy AVL Trees?Added ComplexityViolationsThe Balance FactorWhat is a rotation?Single RotationsDouble Rotations. Introduction. AVL tree is the first balanced binary search tree (name after its discovers, Adelson-Vels
E N D
1. Presented by:
Chien-Pin Hsu
CS146
Prof. Sin-Min Lee
2. Outline Introduction
Definitions
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)
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.
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
7. Picking Out AVL Trees (Cont’d)
8. Why AVL Trees? The worst case for Binary Search Trees:
Insert: O(n).
Delete: O(n).
Search :O(n).
The worst case for AVL trees:
Insert: O(log n).
Delete: O(log n).
Search:O(log n).
Recall when comparing the time complexity
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
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
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
14. The Balance Factor (Cont’d)
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
2) Double Rotation
When to perform rotations?
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
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)
20. Double Rotations
21. Double Rotations (Cont’d)
22. Double Rotations (Cont’d) Algorithm rotateLeftRight(nodeN)
nodeC = left child of nodeNSet nodeN’s left child to the sub-tree produced by
rotateLeft(nodeC) rotateRight(nodeN)
23. Double Rotations (Cont’d)
24. Double Rotations (Cont’d) Algorithm rotateRightLeft(nodeN)
nodeC = right child of nodeNSet nodeN’s right child to the sub-tree produced by
rotateRight(nodeC) rotateLeft(nodeN)
25. Double Rotations (Cont’d)
26. The End