AVL Trees

CSE 373 Data Structures Lecture 8

¾ Section 4.4,

12/26/03

AVL Trees - Lecture 8

2

Binary Search Tree - Best Time
 All BST operations are O(d), where d is tree depth  minimum d is d ! ­log2N½ for a binary tree with N nodes
¾ What is the best case tree? ¾ What is the worst case tree?

 So, best case running time of BST operations is O(log N)
12/26/03 AVL Trees - Lecture 8 3

Binary Search Tree - Worst Time
 Worst case running time is O(N)
¾ What happens when you Insert elements in ascending order?
 Insert: 2, 4, 6, 8, 10, 12 into an empty BST

¾ Problem: Lack of ³balance´:
 compare depths of left and right subtree

¾ Unbalanced degenerate tree

12/26/03

AVL Trees - Lecture 8

4

Balanced and unbalanced BST 1 2 3 4 4 2 1 12/26/03 4 2 1 5 6 6 7 AVL Trees .Lecture 8 5 3 Is this ³balanced´? 3 5 7 5 .

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 12/26/03 AVL Trees .Lecture 8 6 .

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 12/26/03 AVL Trees .Lecture 8 7 .

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 4 1 12/26/03 5 9 Insert 2 & complete tree 2 4 6 8 9 8 5 8 1 AVL Trees .Lecture 8 .

heights of left and right subtree can differ by no more than 1 ¾ Store current heights in each node 12/26/03 AVL Trees .Good but not Perfect Balance  AVL trees are height-balanced binary search trees  Balance factor of a node ¾ height(left subtree) .Lecture 8 9 .AVL .height(right subtree)  An AVL tree has balance factor calculated at every node ¾ For every node.

62) 12/26/03 AVL Trees .  Basis ¾ N(0) = 1.Height of an AVL Tree  N(h) = minimum number of nodes in an AVL tree of height h.Lecture 8 h-1 h-2 10 . N(1) = 2  Induction ¾ N(h) = N(h-1) + N(h-2) + 1 h  Solution (recall Fibonacci analysis) ¾ N(h) > Jh (J } 1.

Find takes O(logn)) 12/26/03 AVL Trees .44 log2n (i..e. ¾ n > N(h) (because N(h) was the minimum) ¾ n > Jh hence logJ n > h (relatively well balanced tree!!) ¾ h < 1.Height of an AVL Tree  N(h) > Jh (J } 1.Lecture 8 11 .62)  Suppose we have n nodes in an AVL tree of height h.

Node Heights Tree A (AVL) height=2 BF=1-0=1 Tree B (AVL) 2 1 6 1 0 6 0 0 1 4 0 0 9 0 4 1 5 8 9 1 5 height of node = h balance factor = hleft-hright empty height = -1 12/26/03 AVL Trees .Lecture 8 12 .

Node Heights after Insert 7 Tree A (AVL) 2 Tree B (not AVL) 3 1 6 1 1 6 0 1 balance factor 1-(-1) = 2 2 4 0 0 0 9 0 4 1 5 0 9 -1 1 5 7 8 7 height of node = h balance factor = hleft-hright empty height = -1 12/26/03 AVL Trees .Lecture 8 13 .

updating heights ¾ If a new balance factor (the difference hlefthright) is 2 or ±2.Lecture 8 14 . adjust tree by rotation around the node 12/26/03 AVL Trees .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.

Lecture 8 15 .Single Rotation in an AVL Tree 2 2 6 1 2 1 6 1 4 0 0 1 9 0 4 0 0 8 0 1 5 0 8 7 1 5 7 9 12/26/03 AVL Trees .

2. Inside Cases (require double rotation) : 3.Lecture 8 16 .Insertions in AVL Trees Let the node that needs rebalancing be E. Insertion into right subtree of left child of E. Insertion into right subtree of right child of E. 12/26/03 AVL Trees . Insertion into left subtree of left child of E. There are 4 cases: Outside Cases (require single rotation) : 1. Insertion into left subtree of right child of E. 4. The rebalancing is performed through four separate rotation algorithms.

Lecture 8 .AVL Insertion: Outside Case Consider a valid AVL subtree j h h k h Z 17 X 12/26/03 Y AVL Trees .

AVL Insertion: Outside Case j k h+1 h h Inserting into X destroys the AVL property at node j Z Y X 12/26/03 AVL Trees .Lecture 8 18 .

Lecture 8 19 .AVL Insertion: Outside Case j k h+1 h Do a ³right rotation´ h Z Y X 12/26/03 AVL Trees .

Lecture 8 20 .Single right rotation j k h+1 h Do a ³right rotation´ h Z Y X 12/26/03 AVL Trees .

Outside Case Completed k h+1 h ³Right rotation´ done! (³Left rotation´ is mirror symmetric) j h X 12/26/03 Y AVL Trees .Lecture 8 Z 21 AVL property has been restored! .

AVL Insertion: Inside Case Consider a valid AVL subtree j h h k h Z 22 X 12/26/03 Y AVL Trees .Lecture 8 .

Lecture 8 23 .AVL Insertion: Inside Case Inserting into Y destroys the AVL property at node j j k h+1 Does ³right rotation´ restore balance? h h Z X 12/26/03 Y AVL Trees .

AVL Insertion: Inside Case k h j h+1 ³Right rotation´ does not restore balance« now k is out of balance h X Y 12/26/03 Z AVL Trees .Lecture 8 24 .

Lecture 8 25 .AVL Insertion: Inside Case Consider the structure of subtree Y« j h h+1 k h Z X 12/26/03 Y AVL Trees .

Lecture 8 26 .AVL Insertion: Inside Case Y = node i and subtrees V and W j h k h i V h+1 Z X 12/26/03 h or h-1 W AVL Trees .

AVL Insertion: Inside Case j k i X V 12/26/03 We will do a left-right ³double rotation´ .Lecture 8 27 . Z W AVL Trees . . .

Lecture 8 28 .Double rotation : first rotation j i k W X 12/26/03 left rotation complete Z V AVL Trees .

Lecture 8 29 .Double rotation : second rotation j i k W X 12/26/03 Now do a right rotation Z V AVL Trees .

Double rotation : second rotation right rotation complete i k h h or h-1 Balance has been restored j h X 12/26/03 V W AVL Trees .Lecture 8 Z 30 .

i.0. just the difference in height.-1) key left right No need to keep the height.e.Implementation balance (1. 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 12/26/03 AVL Trees .Lecture 8 31 .

right := p. n n. p.right.left := n. n := p } You also need to modify the heights or balance factors of n and p X Insert Y Z 12/26/03 AVL Trees .Lecture 8 32 .Single Rotation RotateFromRight(n : reference node pointer) { p : node pointer. p := n.left.

Lecture 8 V W 33 .Double Rotation  Implement Double Rotation in two lines. DoubleRotateFromRight(n : reference node pointer) { ???? n } X Z 12/26/03 AVL Trees .

Insertion in AVL Trees  Insert at the leaf (as for all BST) ¾ 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.Lecture 8 34 . adjust tree by rotation around the node 12/26/03 AVL Trees . updating heights ¾ If a new balance factor (the difference hlefthright) is 2 or ±2.

data = x : return 0.data < x : return Insert(T. x). T. return 1.data > x : return Insert(T.//the links to //children are null case T.left. //Duplicate do nothing T.right. x).Insert in BST Insert(T : reference tree pointer.Lecture 8 35 . T. x : element) : integer { if T = null then T := new tree.data := x. endcase } 12/26/03 AVL Trees .

x : element) : { if T = null then {T := new tree.Insert in AVL trees Insert(T : reference tree pointer. return.data := x.left. x). T. x).data > x : Insert(T.right. if ((height(T. else //inside case T = DoubleRotatefromLeft (T). height := 0.} case T.height := max(height(T.data = x : return .left).left). //Duplicate do nothing T.data > x ) then //outside case T = RotatefromLeft (T).height(T.height(T.data < x : Insert(T.left.} T.right)) +1. } 12/26/03 AVL Trees .right)) = 2){ if (T. code similar to the left case Endcase T. return.Lecture 8 36 .

40 0 10 0 30 25 35 12/26/03 AVL Trees .Lecture 8 37 .Example of Insertions in an AVL Tree 2 20 0 1 Insert 5.

Example of Insertions in an AVL Tree 2 20 1 1 1 3 20 30 0 2 1 10 0 0 30 0 0 10 35 5 Now Insert 45 5 25 25 35 0 40 12/26/03 AVL Trees .Lecture 8 38 .

Single rotation (outside case) 3 20 1 2 1 3 20 30 0 2 10 0 0 30 2 0 10 35 1 40 0 45 Now Insert 34 5 25 Imbalance 5 25 0 40 1 0 35 45 12/26/03 AVL Trees .Lecture 8 39 .

Lecture 8 40 12/26/03 .Double rotation (inside case) 3 20 1 3 1 3 20 35 1 0 25 2 10 0 0 Imbalance 25 30 2 0 10 40 5 30 5 40 1 0 1 35 Insertion of 34 0 45 0 34 45 34 AVL Trees .

AVL Tree Deletion  Similar but more complex than insertion ¾ Rotations and double rotations needed to rebalance ¾ Imbalance may propagate upward so that many rotations may be needed.Lecture 8 41 . 12/26/03 AVL Trees .

g. Arguments against using AVL trees: 1. Splay trees). Search is O(log N) since AVL trees are always balanced. Asymptotically faster but rebalancing costs time.Lecture 8 42 . The height balancing adds no more than a constant factor to the speed of insertion. Most large searches are done in database systems on disk and use other structures (e.Pros and Cons of AVL Trees Arguments for AVL trees: 1. more space for balance factor. 4. Insertion and deletions are also O(logn) 3. 2. 2. 12/26/03 AVL Trees . May be OK to have O(N) for a single operation if total run time for many consecutive operations is fast (e. 3. Difficult to program & debug.g. B-trees).

Lecture 8 W 43 .right). } X Z V 12/26/03 AVL Trees .Double Rotation Solution DoubleRotateFromRight(n : reference node pointer) { RotateFromLeft(n. n RotateFromRight(n).