250 likes | 317 Views
Learn about AVL trees, balancing methods, and tree rotations for maintaining balanced binary search trees in data structures.
E N D
2620001Data Structures Nonlinear Data Structure (AVL Tree)
Balanced and unbalanced BST 1 4 2 2 5 3 1 3 4 4 Is this “balanced”? 5 2 6 6 1 3 5 7 7
Approaches to balancing trees • Don't balance • May end up with some nodes very deep • Strict balance • The tree must always be balanced perfectly • Pretty good balance • Only allow a little out of balance • Adjust on access • Self-adjusting
Balancing Binary Search Trees • Many algorithms exist for keeping binary search trees balanced • Adelson-Velskii and Landis (AVL) trees (height-balanced trees) • Splay trees and other self-adjusting trees • B-trees and other multiway search trees
Perfect Balance • Want a complete tree after every operation • tree is full except possibly in the lower right • This is expensive • For example, insert 2 in the tree on the left and then rebuild as a complete tree 6 5 Insert 2 & complete tree 4 9 2 8 1 5 8 1 4 6 9
AVL - Good but not Perfect Balance • AVL trees are height-balanced binary search trees • Balance factor of a node • height(left subtree) - height(right subtree) • An AVL tree has balance factor calculated at every node • For every node, heights of left and right subtree can differ by no more than 1 • Store current heights in each node
Node Heights Tree A (AVL) Tree B (AVL) height=2 BF=1-0=1 2 6 6 1 0 1 1 4 9 4 9 0 0 0 0 0 1 5 1 5 8 height of node = h balance factor = hleft-hright empty height = -1
Node Heights after Insert 7 Tree A (AVL) Tree B (not AVL) balance factor 1-(-1) = 2 2 3 6 6 1 1 1 2 4 9 4 9 -1 0 0 0 0 0 1 7 1 5 1 5 8 0 7 height of node = h balance factor = hleft-hright empty height = -1
Insert and Rotation in AVL Trees • Insert operation may cause balance factor to become 2 or –2 for some node • only nodes on the path from insertion point to root node have possibly changed in height • So after the Insert, go back up to the root node by node, updating heights • If a new balance factor (the difference hleft-hright) is 2 or –2, adjust tree by rotationaround the node
Single Rotation in an AVL Tree 2 2 6 6 1 2 1 1 4 9 4 8 0 0 0 0 1 0 0 7 9 1 5 8 1 5 0 7
AVL Insertion: Outside Case j Consider a valid AVL subtree k h Z h h X Y
AVL Insertion: Outside Case j Inserting into X destroys the AVL property at node j k h Z h+1 h Y X
AVL Insertion: Outside Case j Do a “right rotation” k h Z h+1 h Y X
Single right rotation j Do a “right rotation” k h Z h+1 h Y X
Outside Case Completed “Right rotation” done! (“Left rotation” is mirror symmetric) k j h+1 h h X Z Y AVL property has been restored!
AVL Insertion: Inside Case j Consider a valid AVL subtree k h Z h h X Y
AVL Insertion: Inside Case j Inserting into Y destroys the AVL property at node j Does “right rotation” restore balance? k h Z h h+1 X Y
AVL Insertion: Inside Case k “Right rotation” does not restore balance… now k is out of balance j h X h h+1 Z Y
AVL Insertion: Inside Case j Consider the structure of subtree Y… k h Z h h+1 X Y
AVL Insertion: Inside Case j Y = node i and subtrees V and W k h Z i h h+1 X h or h-1 W V
AVL Insertion: Inside Case j We will do a left-right “double rotation” . . . k Z i X W V
Double rotation : first rotation j left rotation complete i Z k W V X
Double rotation : second rotation j Now do a right rotation i Z k W V X
Double rotation : second rotation right rotation complete Balance has been restored i j k h h h or h-1 V Z W X
Implementation balance (1,0,-1) key left right No need to keep the height; just the difference in height, i.e. the balance factor; this has to be modified on the path of insertion even if you don’t perform rotations Once you have performed a rotation (single or double) you won’t need to go back up the tree