{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

AVL Tree

# AVL Tree - AVL Tree AVL Adelson Velski-Landis Height of a...

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

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 \ ‘_’

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

View Full Document
Parent 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.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 7

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

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

View Full Document
Ask a homework question - tutors are online