Download presentation

Presentation is loading. Please wait.

Published byLauren O'Connor Modified over 4 years ago

1
COSC 2007 Data Structures II Chapter 12 Advanced Implementation of Tables II

2
2 Topics AVL trees Definition Operations Insertion Deletion Traversal Search

3
3 AVL (Adel'son-Vel'skii & Landis) Trees AVL Tree Balanced BST: Adelson_Velskii and Landii Height is guaranteed to be O(log 2 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
4 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 60 7040 3050 5645

5
5 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 60 7040 3050 5645

6
6 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. 6 2 4 3 1 8 9

7
7 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. 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. 6 2 4 3 1 8 9

8
8 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. height(X) = max(height(left(X)), height(right(X))) + 1 6 2 4 3 1 8 9

9
9 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 6 2 4 3 1 8 9

10
10 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 6 2 4 3 1 8 9

11
11 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 8040 60 0 00 8040 60 20 Insert 20 0 0 70 0 8040 60 20 Insert 70 0 0

12
12 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 70 0 8040 60 20 1- 0 0 70 0 8040 60 20 Insert 50 1- 0 0 0 50 0 70 0 8040 60 20 Insert 90 0 0 0 0 2090 00

13
13 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

14
14 ADT AVL Tree Insertion: Case 3-a: Single Rotation 100 20 50 10 8040 60 30 Insert 5 0 100 20 50 10 8040 60 30 5 0 -2 Pivot 100 20 50 10 80 40 60 30 5 00 0 0 0 0 0 +1 Rebalance

15
15 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 100 30 50 20 80 40 60 3510 0 0 0 0 0 0 +1 Double Rotation 100 20 50 10 8040 60 30 35 0 0 +1 -2 (Pivot) Insert 35

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

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

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

19
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
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
21 AVL Trees Traversal Time complexity? Search

Similar presentations

OK

IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture27.

IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture27.

© 2018 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google