data structures in java
play

Data Structures in Java Session 10 Instructor: Bert Huang - PowerPoint PPT Presentation

Data Structures in Java Session 10 Instructor: Bert Huang http://www1.cs.columbia.edu/~bert/courses/3134 Announcements Homework 3 due 10/20 Review AVL Trees Single rotate for left-left imbalance Double rotate for left-right


  1. Data Structures in Java Session 10 Instructor: Bert Huang http://www1.cs.columbia.edu/~bert/courses/3134

  2. Announcements • Homework 3 due 10/20

  3. Review • AVL Trees • Single rotate for left-left imbalance • Double rotate for left-right imbalance • Running time Analysis • Depth always O(log N) • Constant cost for rotations

  4. Today ʼ s Plan • Amortized Running time • Splay Trees • Tries

  5. Amortized Running Time • So far, we measure the worst-case running time of each operation • Usually we perform many operations • Sometimes M O ( f ( N )) operations can run provably faster than O (M f ( N )) • Then we can guarantee better average running time, aka amortized

  6. Comparing Models • Amortized and Average case average running time of many operations • Amortized and Standard: adversary chooses input values and operations • Average analysis, analyst chooses randomization scheme

  7. Splay Trees • Like AVL trees, use the standard binary search tree property • After any operation on a node, make that node the new root of the tree • Make the node the root by repeating one of two moves that make the tree more spread out

  8. Informal Justification • Similar to caching . • Heuristically, data that is accessed tends to be accessed often. • Easier to implement than AVL trees • No height bookkeeping

  9. Easy cases • If node is root, do nothing • If node is child of root, do single AVL rotation • Otherwise, node has a grandparent, and there are two cases

  10. Case 1: zig-zag a c b a b z c w w x y z x y • Use when the node is the right child of a left child (or left-right) • Double rotate, just like AVL tree

  11. Case 2: zig-zig • We can ʼ t use the single-rotation strategy like AVL trees • Instead we use a different process, and we ʼ ll compare this to single-rotation

  12. Case 2: zig-zig a c z b b w a c y x y z w x • Use when node is the right-right child (or left-left) • Reverse the order of grandparent->parent->node • Make it node->parent->grandparent

  13. Case 2 versus Single Rotations 1 7 7 6 6 5 5 4 4 3 3 2 2 1 1 zig-zig single-rotate

  14. Case 2 versus Single Rotations 2 7 7 6 6 5 5 4 4 1 1 2 3 3 2 zig-zig single-rotate

  15. Case 2 versus Single Rotations 3 7 7 6 6 1 1 5 4 4 2 5 3 3 2 zig-zig single-rotate

  16. Case 2 versus Single Rotations 4 1 7 1 6 6 7 4 5 2 4 5 3 3 2 zig-zig single-rotate

  17. Splay Analysis (Informal) • We can make a chain by inserting nodes that make the tree its left child • Each of these operations is cheap • Then we can search for deepest node • Splay operation squishes the tree; can only bad operations once before they become cheap • M operations take O ( M log N ), so amortized O (log N ) per operation (fyi, not proved)

  18. Prefix Trees (Tries) • Nicknamed “Trie”, short for re trie val • Efficiently store objects for fast retrieval via keys • Usually key is a String • Basic strategy: • split into sub-tries based on current letter

  19. Trie Example • “cat”, “cow”, “dog”, “doberman”, “duck” Root c d a o o u duck cow cat g b doberman dog

  20. Trie Analysis • In the worst case, inserting a key of length k or (looking up) is O(k) • This is not dependent on N (this is shocking!) • Much better than log(N) for huge data like dictionaries

  21. Reading • Splay Trees: 4.5

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend