1 / 26

Lecture 20: Screw that, I want It All

CSC 213 – Large Scale Programming. Lecture 20: Screw that, I want It All. Implementing Map with a Tree. Accessing root much faster than going to leaves In real-world, should place important data near root Which key best at root of Tree of NHL teams?. Implementing Map with a Tree.

melba
Download Presentation

Lecture 20: Screw that, I want It All

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSC 213 – Large Scale Programming Lecture 20:Screw that, I want It All

  2. Implementing Map with a Tree • Accessing root much faster than going to leaves • In real-world, should place important data near root • Which key best at root of Tree of NHL teams?

  3. Implementing Map with a Tree • Accessing root much faster than going to leaves • In real-world, should place important data near root • Which key best at root of Tree of NHL teams? • BST: Key for Entry added first

  4. Implementing Map with a Tree • Accessing root much faster than going to leaves • In real-world, should place important data near root • Which key best at root of Tree of NHL teams? • BST: Key for Entry added first • AVLTree:RandomEntry near midpoint

  5. Implementing Map with a Tree • Accessing root much faster than going to leaves • In real-world, should place important data near root • Which key best at root of Tree of NHL teams? • BST: Key for Entry added first • AVLTree:RandomEntry near midpoint • SplayTree:Most recently used key

  6. Building a SplayTree • Another approach which builds upon BST • Not anAVLTree, however, but a new BST subclass

  7. Concept Behind SplayTree • Splay trees do NOT maintain balance • Recently used nodes clustered near top of BST • Most recently accessed nodes take O(1) time • Other nodes may need O(n) time to find, however

  8. Concept Behind SplayTree • Splay trees do NOT maintain balance • Recently used nodes clustered near top of BST • Most recently accessed nodes take O(1) time • Other nodes may need O(n) time to find, however

  9. SplayTree Complexity • Without balancing, keeps BST'sO(n) complexity • Worst-case performance is like all unbalanced trees • But splaying gives expected O(logn) complexity • Averages complexity of O(1) & O(n) operations • If work concentrated on small subset, time is faster • Worst-case hard to create without knowing tree

  10. Be Kind: Splay Your Tree • Assumes nodes reused soon after initial use • At end of each method, moves node up to root • Using node now O(1) and will only slowly drop in tree • Splay tree with each find, insert& remove • AVL-like restructuring to reorganize nodes in tree • But continues rotations until node becomes root

  11. How To Splay

  12. How To Splay • Uses trinode restructuringbut not like AVL does • Not balancing: selects node, parent, & grandparent • Always move node to root of subtree when splaying • When splaying, new types restructures also exist • Node & parent always used in these rotations • Rotations will also use grandparent, if it exists • Moving node to tree root is goal of splaying • May get a balanced tree, but WANT IT ALL, ASAP

  13. When To Use Splay Tree • What applications are good for a splay tree? • Where would splay trees be a BAD IDEA?

  14. Splay Node Rotations • Uses different nodes than previous rotations • AVLTreemoves node down to balance tree's tao • Can now be greedy: make it SplayTree's root

  15. Splay Node Rotations • Uses different nodes than previous rotations • AVLTreemoves node down to balance tree's tao • Can now be greedy: make it SplayTree's root AVLTree

  16. Splay Node Rotations • Uses different nodes than previous rotations • AVLTreemoves node down to balance tree's tao • Can now be greedy: make it SplayTree's root AVLTree SplayTree

  17. Zig-Zag When Splaying a Tree • When node median of parent & grandparent • Just like in AVL tree, perform trinode restructuring • Use 7(+1) variables to set node's parent & children node grandparent grandparent parent parent T1 node T1 T2 T3 T4 T4 T2 T3

  18. Zig-Zig When Splaying a Tree • When node, parent, & grandparent in a line • Rotation differs from AVL tree's trinode restructure grandparent parent T1 node T2 T4 T3

  19. Zig-Zig When Splaying a Tree • When node, parent, & grandparent in a line • Rotation differs from AVL tree's trinode restructure • Greed is very good: splay node to subtree root node grandparent parent parent T1 T4 node grandparent T2 T3 T4 T1 T2 T3

  20. SplayingRight Child Of Root • Simplest process is when parent is the root • Single rotation completes splaying process • Splaying does not stop until you reach the top • Rotation not always used, may only need restructures root node T1 T3 T2

  21. SplayingRight Child Of Root • Simplest process is when parent is the root • Single rotation completes splaying process • Splaying does not stop until you reach the top • Rotation not always used, may only need restructures root node T1 T3 T2

  22. SplayingRight Child Of Root • Simplest process is when parent is the root • Single rotation completes splaying process • Splaying does not stop until you reach the top • Rotation not always used, may only need restructures node root root node T1 T3 T1 T2 T3 T2

  23. SplayingLeft Child Of Root • Simplest process is when parent is the root • Single rotation completes splaying process • Splaying does not stop until you reach the top • Rotation not always used, may only need restructures root node node root T1 T3 T3 T1 T2 T2

  24. Which Node Gets Splayed?

  25. Which Node Gets Splayed? • Only internal nodes can be splayed • When operation end with leaf, splay its parent

  26. For Next Lecture • Weekly assignment available to test skills • Assignment due at regular time next Tuesday • Lab project phase #2 due tomorrow as well • When group ready to submit, send me e-mail • Midterm #1 in class on Friday • Open-book, open-note exam (as usual) • Use any lecture’s slides IF you have notes on them • No computers, calculators, or friends, however

More Related