Download presentation
Presentation is loading. Please wait.
Published byMoris Griffin Modified over 9 years ago
1
D. ChristozovCOS 221 Intro to CS II AVL Trees 1 AVL Trees: Balanced BST Binary Search Trees Performance Height Balanced Trees Rotation AVL: insert, delete
2
D. ChristozovCOS 221 Intro to CS II AVL Trees 2 Binary Search Trees: Performance The number of operations in function ‘search’ is equal to the length of the path from the root to the leaf. Therefore the worst case of search for (unbalanced) BST is O(n) The worst case of balanced BST is O(log 2 n)
3
D. ChristozovCOS 221 Intro to CS II AVL Trees 3 Height-balanced trees n n-1 A height-balanced binary tree has the property that for each node the difference in the heights of the right and left child is no larger than one. Full and Complete binary tree are height-balanced tree.
4
D. ChristozovCOS 221 Intro to CS II AVL Trees 4 Height-balanced trees Theorem: The paths in height-balanced binary trees have logarithmic length. 1.The largest number of nodes in a binary tree with n levels (of depth n) is 2 n-1. 2.Let us denote with M n the minimal number of nodes in height-balanced tree of depth n. 3.M 0 = 1M 1 = 2M n+1 = M n-1 + M n + 1 4.Similar to Fibonacci numbers: f n+1 = f n-1 + f n and therefore we can approximate M n 5.and n ≈ 1.44 log 2 M n
5
D. ChristozovCOS 221 Intro to CS II AVL Trees 5 Rotation Algorithms to maintain the Height-Balance Property in a Binary Search Tree is based on Rotation X YZ A B Let only for the node A the height-balanced tree property is violated and its depth is n+1. Let the right child B is heavier – its depth is n. The depth of X is n-2; and the depth of Y and Z are either n-1 or n-2. We can assume that rotation to left will restore the height-balance property of the tree. X Y Z A B Left rotation is performed with two assignments: A->right(B->left); B->left(&A) After Rotation: Before Rotation:
6
D. ChristozovCOS 221 Intro to CS II AVL Trees 6 Rotation X YZ A B X Y Z A B Left rotation Abf = 2 Bbf = {0, 1, -1} Bbf 0 Bbf < 0 An+1 Bnn Xn-2 Y n-1 Z n-2 Bbf 0 Bbf < 0 An-1n Bnn+1 Xn-2 Y n-1 Z n-2 Heights before rotationHeights after rotation Bbf = (n-2) - n =-2
7
D. ChristozovCOS 221 Intro to CS II AVL Trees 7 AVL Tree The bf = {-1, 0, 1} for every node in the tree If bf > 1 and the bf of the root of the right-sub-tree is >=0 single left rotation restores the balance. If bf > 1 and the bf of the root of the right-sub-tree is <0 to restore the balance double rotation is required: 1.single right rotation for the right sub-tree root; and 2.single left rotation for the current node. for bf <-1 operation are symmetrical. Value = {Key, Data} int bf leftright bf – balance factor AVL tree node: Adelson-Velskii and Landis AVL tree: 1.The AVL tree is a Binary Search Tree 2.The AVL tree is a Height-Balanced Tree
8
D. ChristozovCOS 221 Intro to CS II AVL Trees 8 AVL tree: insertion 11:0 5:0 3:0 2:04:0 7:1 8:0 15:0 12:-1 10:0 18:0 16:020:0 9:0 11:? new 10: 1 12 :- 2 Unbalanced: Double rotation Insertion of the new node as right child increments the balance factor if(left->balFac != oldbf && left->balFac) balFac--; Update of balance factors follows the way back of the path of insertion.
9
D. ChristozovCOS 221 Intro to CS II AVL Trees 9 AVL tree: insertion 1 12:-2 10:0 11:-1 12:-2 2 Double rotation: 1: Rotation around the left child Double rotation: 2: Rotation around the unbalanced node. Direction of rotation depends on the sign of the balance factor: positive left rotation; negative right rotation. 10:0 11:0 12:0 10:1 11:0 12:-2
10
D. ChristozovCOS 221 Intro to CS II AVL Trees 10 AVL tree: removal 1.Follows the algorithm for removal in a BST 2.Updates the balance factors 3.Restores balance, when needed Let us remove 15 9:1 5:-1 7:1 8:0 15:1 18:-1 16:-1 25 17 15.8:-1 15.9:0 11:-1 10:-112:0 11.5:0 3:1 2:04:-1 3.5 20:1 15.8:0 15.9:0 The parent’s balance factor is incremented: the left sub-tree has one level less 16:0 18:0 9:0 Further update of balance factors follows the way back of the path of node removal. (the left-most descendent) if(left->balFac != oldbf && left->balFac == 0) balFac--; The left-most descendent of the right sub-tree is 15.8 It’s right child will become the left child of its parent.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.