Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "COSC 2007 Data Structures II Chapter 12 Advanced Implementation of Tables II."— Presentation transcript:

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

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

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

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

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

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

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

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

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

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 Insert Pivot 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 Double Rotation (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 (Pivot) h h+1 h Tree height = h 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 (Pivot) h+1 h Tree height = h+3 h 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 (Pivot) h +1 h Tree height = h+4 h 30 h h 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


Download ppt "COSC 2007 Data Structures II Chapter 12 Advanced Implementation of Tables II."

Similar presentations


Ads by Google