290 likes | 405 Views
Course Outline. Introduction and Algorithm Analysis (Ch. 2) Hash Tables: dictionary data structure (Ch. 5) Heaps: priority queue data structures (Ch. 6) Balanced Search Trees: general search structures (Ch. 4.1-4.5) Union-Find data structure (Ch. 8.1–8.5)
E N D
Course Outline • Introduction and Algorithm Analysis (Ch. 2) • Hash Tables: dictionary data structure (Ch. 5) • Heaps: priority queue data structures (Ch. 6) • Balanced Search Trees: general search structures (Ch. 4.1-4.5) • Union-Find data structure (Ch. 8.1–8.5) • Graphs: Representations and basic algorithms • Topological Sort (Ch. 9.1-9.2) • Minimum spanning trees (Ch. 9.5) • Shortest-path algorithms (Ch. 9.3.2) • B-Trees: External-Memory data structures (Ch. 4.7) • kD-Trees: Multi-Dimensional data structures (Ch. 12.6) • Misc.: Streaming data, randomization
Binary search trees • Values in the left subtree < the value in the node < values in the right subtree • find, findMin, and findMax • insert a new element • Find the location for the new element • Insert a new node • remove an element • Find the element • If the element is a leaf node, delete the node and done • If it has one child, delete the node and fix the link in the parent node • If it has two children, find a replacement node (how?) • Complexity: O(N) for find, insert, and remove
Intuition of Making a Good Tree • How do we guarantee O(log N) complexity for find, insert, and remove? • Constant time effort to determine which branch • Total time is proportional to tree height • If tree is balanced, height is O(log n)
Alternate insertionsand deletions randomly generated
An average case analysis • Ideal case: O(log N) • Average case without remove • All orders of insertions are equally likely a i elements <a n-i-1 elements > a All orders of insertions: Average path length P(i) All orders of insertions: Average path length P(n-i-1)
Tree Rotations d b b d A E C E A C right rotation left rotation
AVL trees • height of a tree: max depth of a node in the tree • AVL tree: • A binary search tree such that at each node X,the height of the leftand right subtreesof X can differ by at most 1 • Bound on the height of an AVL tree with N nodes • S(h) : the min number of nodes in an AVL tree of height h • S(h) = S(h – 1) + S(h –2) +1 • Claim: S(h) = Fh+3 – 1 • h = O(log N) • Maintaining AVL trees upon insert, remove: • Single and double rotations
AVL Trees - Insertion • Member (Find) operation (as in BST) • Insert at leaf • Update balance information upward from insertion • At the first node that |balance|>=2, four cases: • insertion was in left subtree of left child • insertion was in left subtree of right child • insertion was in right subtree of left child • insertion was in right subtree of right child • 1 and 4 are fixed by single rotation • 2 and 3 are fixed by double rotation
Single Rotation (left-left) • Y cannot be at X’s level • Y cannot be at Z’s level either 1 2 2 1 Z X Y Z X Y
Single Rotation (right-right) 2 1 1 2 Z X X Y Y Z • Y cannot be at Z’s level • Y cannot be at X’s level either
Double Rotation (left-right): Single Won’t Work • Single rotation does not work because it does not make Y any shorter 1 2 2 1 Z X Y Z X Y
Double Rotation (left-right): First Step • First rotation between 2 and 1 3 3 2 1 D D 1 2 C A B C A B
Double Rotation (left-right): Second Step • Second rotation between 2 and 3 2 3 3 2 1 D 1 C D B C A B A
Double Rotation (left-right): Summary 2 3 3 1 1 D 2 D B C A A C B
Double Rotation (right-left): First Step • First Rotation between 2 and 3 1 1 A 2 A 3 2 3 B D C D C B
Double Rotation (right-left): Second Step • Second Rotation between 1 and 2 2 1 A 3 2 1 3 D B B C A C D
Double Rotation (right-left): Summary 2 1 3 A 3 1 2 D B C A D C B
Insertion into an AVL tree AvlTree AvlNode element height left right AvlTree::insert(x, t) if (t = NULL) then t = new AvlNode(x, …); elseif (x < telement) then insert (x, tleft); if (height(tleft) – height(tright) = 2) thenif (x < tleftelement ) then rotateWithLeftChild (t); else doubleWithLeftChild (t); else if (telement < x ) then insert (x, tright); if (height(tright) – height(tleft) = 2) thenif (trightelement < x) then rotateWithRightChild (t); else doubleWithRightChild (t); theight = max{height(tleft), height(tright)}+1; • Tree height • Search • Insertion: • search to find insertion point • adjust the tree height • rotation if necessary
Removing an element from an AVL tree • Similar process: • locate & delete the element • adjust tree height • perform necessary rotations (up to log (n+1)) • Example • Complexity of operations: • O(h) £O(log n)n: number of nodes, h: tree height
Deletion Example 15 delete 10 15 10 30 5 30 5 12 17 35 12 17 35 14 20 31 40 14 20 31 40 50 50 15 30 15 35 12 30 12 17 31 40 5 14 17 35 5 14 20 50 20 31 40 50
Splay trees G X P P G D X A A B C D B C G X P D P A X C G B A B C D • Splayat a node: rotate the node up to the root • Basic operations (repeat until node X is at root): • zig-zag: • zig-zig:
Splay trees • Idea: always splay at a node after it is accessed • Details: • after find, splay at the found node (or last node on search path if not found) • after insert, splay at the new node • after delete, splay at last node on search path
Splay trees Starting with an empty tree,any m operations (find, insert, delete)take O(m log n) time,where n is the maximum # of items ever in the tree • Proof: “credit accounting” • just count the cost of splaying • each operation gets 3 lg n + 1 coins • one coin pays for one splay step • each node holds floor(lg(size of subtree)) coins
Splay trees G X P P G D X A A B C D B C G X P D P A X C G B A B C D • Splayat a node: rotate the node up to the root • Basic operations (repeat until node X is at root): • zig-zag: • zig-zig:
Splay trees • Idea: always splay at a node after it is accessed • Details: • after find, splay at the found node (or last node on search path if not found) • after insert, splay at the new node • after delete, splay at last node on search path
Splay trees: the main theorem Starting with an empty tree,any m operations (find, insert, delete)take O(m log n) time,where n is the maximum # of items ever in the tree Lemma: Any sequence of m operations requires at most 4 m log n + 2 splay steps. (The theorem follows immediately from the lemma.)
Splay trees: proving the lemma • Each operation gets 4 log n + 2 coins (3 log n + 1is enough for all but insert ) • One splay step costs one coin, from somewhere • Some of the leftover coins are left “on the tree”to pay for later operations Credit invariant:Every tree node holds log(subtree size) coins (rounded down to an integer)
Splay trees: maintaining the credit invariant G X P P G D X A A B C D B C • lss(x) = log(subtree size) before step; lss’ after step • zig-zag step costs 1 coin (to do the step) plus lss’(x) – lss(x) + lss’(p) – lss(p) + lss’(g) – lss(g)(to maintain the invariant) • this is always <= 3(lss’(x) – lss(x)) [complex argument] • similarly for zig-zig splay step; 1 more for last half step • total cost of splay is <= 3(lss(root))+1 = 3 log n + 1
Splay trees: end of the main proof • maintaining invariant during splay costs <= 3 log n + 1 coins • maintaining invariant during insert may cost an extra log n + 1 coins (because lss goes up) • maintaining credit invariant never costs more than 4 log n + 2 coins • Lemma: Any sequence of m operations requires at most 4 m log n + 2 splay steps. • Theorem: Starting with an empty tree, any m operations (find, insert, delete) take O(m log n) time