
COMP251: Binary search trees, AVL trees & AVL sort Jérôme Waldispühl School of Computer Science McGill University From Lecture notes by E. Demaine (2009) Outline • Review of binary search trees • AVL-trees • Rotaons • BST & AVL sort Binary search trees (BSTs) x ≤x ≥x • T is a rooted binary tree • Key of a node x ≥ keys in its le\ subtree. • Key of a node x ≤ keys in its right subtree. Operaons on BSTs • Search(T,k): Θ(h) • Insert(T,x): Θ(h) • Delete(T,x): Θ(h) Where h is the height of the BST. Height of a tree Height(n): length (#edges) of longest downward path from node n to a leaf. x Height(right(x)) Height(le\(x)) Height(x) = 1 + max( height(le\(x)), height(right(x)) ) Example a h(a) = ? b g = 1+max( h(b) , h(g) ) = 1+max(1+max(h(c),h(d)),1+h(h)) c d h = 1+max(1+max(0,h(d)),1+0) = 1+max(1+max(0,1+h(e)),1) e = 1+max(1+max(0,1+(1+h(f)))),1) = 1+max(1+max(0,1+(1+0))),1) f = 1+max(3,1) = 4 Good vs. Bad BSTs 56 18 26 26 200 h 28 56 h 18 28 190 213 190 200 213 Balanced Unbalanced h=Θ( log n ) h=Θ( n ) AVL trees Definion: BST such that the heights of the two child subtrees of any node differ by at most one. x |h -h |≤1 le\ right • Invented by G. Adelson-Velsky and E.M. Landis in 1962. • AVL trees are self-balanced binary search trees. • Insert, Delete & Search take O(log n) in average and worst cases. Height of a AVL tree Nh = minimum #nodes in an AVL tree of height h. x Nh-2 N h-1 Nh = 1 + Nh-1 + Nh-2 > 2 . N h-2 h/2 => Nh > Θ( 2 ) => h < 2 . Log Nh => h = O( log n ) (a Vghter bound can found using Fibonacci numbers) Insert in AVL trees 1. Insert as in standard BST 2. Re-establish AVL tree properVes x à Nh-2 Nh-1 ß: Le\ tree is higher = : Balanced à : Right tree is higher Insert in AVL trees 36 ß 12 57 à ß 8 27 43 = çß = 20 ß= 15 = Insert(T, 15) How to restore AVL property? Rotaons Right rotaon y x x y C A A B B C Le\ rotaon Rotaons change the tree structure & preserve the BST property. Proof: elements in B are ≥ x and ≤ y… Example (right rotaon) y y x x C C A B A B x x y y A A B C B C Insert in AVL trees Right rotaon at 27 36 36 ß ß 12 57 12 57 à ß à ß 8 27 43 8 20 43 = ç = = = = 20 15 27 ß = = 15 = Insert in AVL trees Right rotaon at 57 36 36 ß 12 57 12 43 à ç è 8 20 43 8 20 57 = = à 15 27 50 15 27 50 = = = Insert(T, 50) RotateRight(T,57) How to restore AVL property? Insert in AVL trees Le\ rotaon at 43 36 36 12 57 12 57 ç ç 50 8 20 43 8 20 à ß 15 27 50 15 27 43 We remove the zig-zag paern RotateLe\(T,43) Insert in AVL trees Right rotaon at 57 36 36 12 50 12 57 ç = 50 8 20 43 57 8 20 ß 15 27 15 27 43 AVL property restored! RotateRight(T,57) Insert in AVL trees 1. Suppose x is lowest node violang AVL 2. If x is right-heavy: • If x’s right child is right-heavy or balanced: Le\ rotaon (case A) • Else: Right followed by le\ rotaon (case B) 3. If x is le\-heavy: • If x’s le\ child is le\-heavy or balanced: Right rotaon (symmetric of case A) • Else: Le\ followed by right rotaon (sym. of case B) 4. then conVnue up to x’s ancestors. Case A è Le\ rotaon = y x à = y h+1 h x h h-1 A C h-1 B C h h-1 A B h-1 è Le\ rotaon ß y x = à y h+1 h+1 x h h-1 A C h B C h h-1 A B h Case B Right rotaon at y & Le\ rotaon at x è = z x ß à ß y h+1 h x y h h-1 A h z h-1 D h-1 h-1 A B C D B C Case B Right rotaon at y è è x ß x à h-1 y h+1 z h+1 A h-1 A h z h-1 D h-1 B y h B C = C D z à ß h x y h h-1 Le\ rotaon at x h-1 A B C D Running Vme AVL inserVon • InserVon in O(h) • At most 2 rotaons in O(1) • Running Vme is O(h) + O(1) = O(h) = O(log n) in AVL trees. In-order traversal & BST inorderTraversal(treeNode x) inorderTraversal(x.leftChild); print x.value; inorderTraversal(x.rightChild); x A B • Print the nodes in the le\ subtree (A), then node x, and then the nodes in the right subtree (B) • In a BST, keys in A ≤ x, and keys in B ≥ x. • In a BST, it prints first keys ≤ x, then x, and then keys ≥ x. In-order traversal & BST 36 12 57 8 27 43 20 15 8, 12, 15, 20, 27, 36, 43, 57 All keys come out sorted! BST sort 1. Build a BST from the list of keys (unsorted) 2. Use in-order traversal on the BST to print the keys. 36 12 8 57 43 27 36 12 57 8 27 43 à 8, 12, 27, 36, 43, 57 Running Vme of BST sort: inserVon of n keys + tree traversal. Running Vme of BST sort • In-order traversal is Θ(n) • Running Vme of inserVon is O(h) Best case: The BST is always balanced for every inserVon. Ω(nlog(n)) Worst case: The BST is always un-balanced. All inserVons on same side. n n ⋅(n −1) 2 ∑i = = O(n ) i=1 2 AVL sort Same as BST sort but use AVL trees and AVL inserVon instead. • Worst case running Vme can be brought to O(n log n) if the tree is always balanced. • Use AVL trees (trees are balanced). • InserVon in AVL trees are O(h) = O(log n) for balanced trees. .
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages27 Page
-
File Size-