1 / 26

Understanding AVL Trees: Operations and Implementation

Learn about AVL trees, a type of structured binary tree for dynamic applications, with guaranteed O(log n) performance. Discover insertion, deletion, and balancing strategies for efficient storage and retrieval.

deborar
Download Presentation

Understanding AVL Trees: Operations and Implementation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables II

  2. Topics • AVL trees • Definition • Operations • Insertion • Deletion • Traversal • Search

  3. AVL (Adel'son-Vel'skii & Landis) Trees • AVL Tree • Balanced BST: Adelson_Velskii and Landii • Height is guaranteed to be O(log2 n) • After each insertion & deletion, the tree is checked to see if it is still an AVL tree or not • Tree is rebalanced by rearranging its nodes by some rotations around some pivot • Advantages: • Permit insertion, deletion, and retrieval to be done in a dynamic application • Worst-case performance of O(log n)

  4. 60 40 70 30 50 45 56 AVL Trees • Terminology & Definitions • Height-balanced p-tree: • For each node in the tree, the difference in height of its two subtrees is at most p • Height-balanced 1-tree: • For each node in the tree, the difference in height of its two subtrees is at most 1. • AVL-tree is a height-balanced 1-tree

  5. 60 40 70 30 50 45 56 AVL Trees • Terminology & Definitions • Balance Difference (BD) • Height of the node's right subtree - Height of the node's left subtree • Pivot node (first bad node) • Deepest node on a search path, after inserting the new node that has a BD value +2, -2 • Search Path: • Path from the root to the point which a node is to be inserted or found

  6. 6 2 8 4 9 1 3 AVL Trees: Example BST Property At every node X, values in left subtree are smaller than the value in X, and values in right subtree are larger than the value in X.

  7. 6 2 8 4 9 1 3 AVL Trees: Example AVL Balance Property At every node X, the height of the left subtree differs from the height of the right subtree by at most 1.

  8. 6 2 8 4 9 1 3 ADT AVL Tree • Operations: • Same as BST • Only the type of the tree will be AVL-Tree instead of a BST • Insert • Delete • Retrieve • Traverse

  9. 6 2 8 4 9 1 3 ADT AVL Tree • Insertion: • Search path: • Path from the root to the inserted node • Idea: • Attach the new node as in a BST. • Start from the new node • Find the deepest node on the search path that has a BD value = +2 or –2 after insertion of the new node. Call it the Pivot Node. • Adjust the tree so that the values of balance are correct by rotating and it is still an AVL tree. • We are done, and do not need to continue up the tree • Adding a node to the short subtree change the BAL value from the pivot down to the node • Adding a node to the tall subtree of the pivot node requires adjustment to the tree's structure

  10. ADT AVL Tree • Insert operation: • Case 1: • Every node on the search path is balanced, having BD=0. • No possibility that the tree will be non-AVL after insert • Just adjust the tree by assigning new BD values to each node on the search path

  11. ADT AVL Tree • Insertion: • Case 2: • New node is added to the short subtree of the pivot node • No rotation is required • Adjusting tree by changing value of BD for each node on the search path starting with the pivot node

  12. ADT AVL Tree • Insertion: • Case 3: • New node is added to the tall subtree of the pivot node • Adding the new node unbalances the tree (not an AVL tree) • Rotation is required

  13. ADT AVL Tree • Insertion: • Case 3-a: Single Rotation

  14. ADT AVL Tree • Insert operation: • Case 3-b: Double rotation • The new node is added to the tall subtree of the pivot node • Adding the new node unbalances the tree (not an AVL tree) • Example: Insert 35

  15. -2 (Pivot) 40 0 20 -1 20 h 0 40 h+1 h+1 h h h Tree height = h+2 Single rotation ADT AVL Tree • General Rebalancing Idea: • Single Rotation • Longer path is on the outside of the tree • Some rotations decrease tree height 0 Tree height = h+3

  16. -2 (Pivot) 0 20 40 -1 0 20 40 h h+1 h+1 h+1 h+1 h 0 Tree height = h+3 Tree height = h+3 Single rotation ADT AVL Tree • General Rebalancing Idea: • Single Rotation • Some rotations keep the tree height

  17. -2 (Pivot) -1 40 30 +1 20 h 0 0 40 20 -1 30 h +1 h +1 h +1 h h h h +1 Double rotation Tree height = h+3 Tree height = h+4 ADT AVL Tree • General Rebalancing Idea: • Double Rotation • Longer path is on the inside of the tree • 3 nodes are involved

  18. ADT AVL Tree (example) • Insert 1, 2, 3, 4, 5, 6, 7 followed by 10, 9

  19. Node Deletion in AVL Trees • There are 4 cases: • Case 1: • No node in the tree contains the data to be deleted • No deletion occurs • Case 2: • The node to be deleted has no children. • Deletion occurs. Check for BAL of all nodes should be performed. If rebalancing is required, perform it • Case 3: • The node to be deleted has one child • Connect this child to the previous parent of the deleted node and then check for balancing • Case 4: • The node to be deleted has two children • Search for the rightmost (or the leftmost) node in the left (in the right) subtree of the node removed and replace the removed node with this node. Then check for balancing

  20. Efficiency of AVL Trees • Changes required by insertion into or deletion from an AVL tree are limited to a path between the root and the leaf • Time complexity?

  21. AVL Trees • Traversal • Time complexity? • Search

  22. Review • Operations which are used to maintain the balance of AVL trees are known as ______. • revolutions • rotations • hash functions • refractions

  23. Review • A(n) ______ is a balanced binary search tree. • 2-3 tree • proper tree • full tree • AVL

  24. Review • Searching an AVL tree is ______. • O(n) • O(log2n) • O(log2n * n) • O(n2)

  25. Review • Traverse an AVL tree is ______. • O(n) • O(log2n) • O(log2n * n) • O(n2)

  26. Review • Insert a node into an AVL tree is ______. • O(n) • O(log2n) • O(log2n * n) • O(n2)

More Related