300 likes | 382 Views
Dive deep into implementing Maps with Trees, exploring the power of Splay Trees, and optimizing data retrieval using BSTs in large-scale programming. Understand when to use Splay Trees and their unique node rotations.
E N D
CSC 213 – Large Scale Programming Lecture 18:Screw that, I want It All
Today’s Goal • Review Map & Dictionaryimplementations • What do they do well? When would they be used? • Why do they suck so much another is needed? • Discuss how we can get it all NOW with BSTs • Why greedy approach could be useful & helpful • Seeing how idea of splay tree not balanced or Zen • What & when to splay node in a BST
Ideal: Hash Without Withdrawal • Want O(1) times that hash provides • At the same time scared of slowdowns from bad hash • Or lack hash function that can work with your data • BSTs provide safety that array-based hash lacks • Worst-case only O(log n)as long as tree is balanced • Requires way to keep tree balanced (e.g., AVL) and • Need O(1) access to tree’s nodes, but how?
Implementing Map with Tree • Accessing root much faster than going to leaves • In real-world, really want important data near root • Which keybest at root of Tree of NHL teams? • BST:Key for Entry added first
Implementing Map with Tree • Accessing root much faster than going to leaves • In real-world, really want important data near root • Which keybest at root of Tree of NHL teams? • BST:Key for Entry added first • AVLTree: Random Entry near midpoint
Implementing Map with Tree • Accessing root much faster than going to leaves • In real-world, really want important data near root • Which keybest at root of Tree of NHL teams? • BST:Key for Entry added first • AVLTree: Random Entry near midpoint • SplayTree:Most recently usedkey
Building a SplayTree • Another approach which builds upon BST • Not anAVLTree, however, but a new BST subclass
Concept Behind Splay Trees • 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
Concept Behind Splay Trees • 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
SplayTree Complexity • Without balancing, keeps BST's O(n) complexity • Worst-case performance is like all unbalanced trees • But splaying gives expected O(log n) 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
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
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
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
When To Use Splay Tree • What applications are good for a splay tree? • Where would splay trees be a BAD IDEA?
Splay Node Rotations • Uses different nodes than AVL tree rotations • AVLTreemoves node down to balance tree's tao • Hedonistic ideal moves node to SplayTree's root
Splay Node Rotations • Uses different nodes than AVL tree rotations • AVLTreemoves node down to balance tree's tao • Hedonistic ideal moves node to SplayTree's root AVLTree
Splay Node Rotations • Uses different nodes than AVL tree rotations • AVLTreemoves node down to balance tree's tao • Hedonistic ideal moves node to SplayTree's root AVLTree SplayTree
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
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
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
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
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
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
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
Which Node Gets Splayed? • Only internal nodes can be splayed • When operation ends with leaf, splay its parent
For Next Lecture • Project #1 due Monday at midnight • 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