AVL Tree

AVL Tree - AVL Tree AVL - Adelson, Velski-Landis Height of...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
AVL Tree AVL - Adelson, Velski-Landis Height of a BST = -1 if empty else it’s 1+ max of (left subtree height, right subtree height) worst case for search/insertion/deletion -- O(n log n) AVL Balance difference in subtree heights at any node <= 1 search in AVL tree = search in non-AVL BST ‘_’ equal high - left and subtree have same height ‘\’ right high - right subtree height is one more than left subtree height ‘/’ left high Parent Left Data Right Balance 40 \ 30 \ ‘_’
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Left Data Right Balance 50 10 20 30 ‘\’ Insert part 1: insert new node as in regular BST part 2: walk back toward root along the insertion path and at each node at each node o change balance factor if already balanced o rebalance if not balanced empty tree -> insert 20 -> insert 25 -> insert 30 rebalance at x let Q be the root of taller subtree of x (child of x on the taller side) case 1: Bf of x = Bf of Q action: rotate Q-x (left rotate if bfs are ‘/’, else right rotate) case 2: rebalancing at a node rotation time is O(1) Algorithm Let Q be taller subtree of x case 1: if bf of x = bf of Q, then rotate Q-x case 2: if bf of x != bf of Q, let R be root of taller subtree of Q rotate R-Q rotate R-x height of an AVL tree = 1*4 log n 1. search = O(log n) 2. insert a. prove that worst case running time is O(log n), given that height is O(log n) a.i. search to failure: O(log n) a.ii. adding new node: O(1) a.iii.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/29/2012 for the course CS 112 taught by Professor Sesh during the Spring '07 term at Rutgers.

Page1 / 7

AVL Tree - AVL Tree AVL - Adelson, Velski-Landis Height of...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online