Download presentation
Presentation is loading. Please wait.
Published byStephen Reynolds Modified over 9 years ago
1
CSC 213 – Large Scale Programming
2
Today’s Goal Review Map & Dictionary implementations 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 BST s 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
3
Map & Dictionary ADT ImplementationSearchingAddingRemoving Ordered List O(log n)O(n)O(n)O(n)O(n) Unordered List O(n)O(n)O(n)/O(1)O(n)/O(1)O(n)O(n) Hash O(n)O(n)O(n)O(n)O(n)O(n) if lucky/good O(1)O(1)O(1)O(1)O(1)O(1) BST O(n)O(n)O(n)O(n)O(n)O(n) AVL / balanced O(log n)
4
Map & Dictionary ADT ImplementationSearchingAddingRemoving Ordered List O(log n)O(n)O(n)O(n)O(n) Unordered List O(n)O(n)O(n)/O(1)O(n)/O(1)O(n)O(n) Hash O(n)O(n)O(n)O(n)O(n)O(n) if lucky/good O(1)O(1)O(1)O(1)O(1)O(1) BST O(n)O(n)O(n)O(n)O(n)O(n) AVL / balanced O(log n)
5
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 BST s 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?
6
Implementing Map with Tree Accessing root much faster than going to leaves In real-world, really want important data near root Which key best at root of Tree of NHL teams? BST : Key for Entry added first
7
Implementing Map with Tree Accessing root much faster than going to leaves In real-world, really want important data near root Which key best at root of Tree of NHL teams? BST : Key for Entry added first AVLTree : Random Entry near midpoint
8
Implementing Map with Tree Accessing root much faster than going to leaves In real-world, really want important data near root Which key best at root of Tree of NHL teams? BST : Key for Entry added first AVLTree : Random Entry near midpoint SplayTree : Most recently used key
9
Building a SplayTree builds upon BST Another approach which builds upon BST Not an AVLTree Not an AVLTree, however, but a new BST subclass
10
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
11
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
12
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
13
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 Splay tree with each find, insert & remove AVL-like restructuring to reorganize nodes in tree But
14
Be Kind: Splay Your Tree
15
How To Splay
16
Uses trinode restructuring but not like AVL does nodeparentgrandparent 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
17
When To Use Splay Tree What applications are good for a splay tree? Where would splay trees be a BAD IDEA ?
18
Splay Node Rotations Uses different nodes Uses different nodes than AVL tree rotations AVLTree moves node down to balance tree's tao Hedonistic ideal moves node to SplayTree 's root
19
Splay Node Rotations Uses different nodes Uses different nodes than AVL tree rotations AVLTree moves node down to balance tree's tao Hedonistic ideal moves node to SplayTree 's root AVLTree
20
Splay Node Rotations Uses different nodes Uses different nodes than AVL tree rotations AVLTree moves node down to balance tree's tao Hedonistic ideal moves node to SplayTree 's root AVLTreeSplayTree
21
Zig-Zag 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 parent node T2T2 T3T3 T4T4 grandparent T1T1 parent node T2T2 T3T3 T4T4 grandparent T1T1
22
Zig-Zig Zig-Zig When Splaying a Tree parent node grandparent T1T1 T4T4 T2T2 T3T3
23
Zig-Zig Zig-Zig When Splaying a Tree parent node T2T2 T3T3 T4T4 grandparent T1T1 parent node grandparent T1T1 T4T4 T2T2 T3T3
24
Right Splaying Right 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 T3T3 T1T1 T2T2
25
Right Splaying Right 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 T3T3 T1T1 T2T2
26
Right Splaying Right 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 T2T2 T3T3 T1T1 root node T3T3 T1T1 T2T2
27
Left Splaying Left 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 T2T2 T3T3 T1T1 root node T3T3 T1T1 T2T2 root
28
Which Node Gets Splayed? MethodNode to splay find If found, splay node containing match If not found, splay last node in treeSearch add Splay node for new Entry remove If found, splay parent of external node removed If not found, splay last node in treeSearch
29
Which Node Gets Splayed? MethodNode to splay find If found, splay node containing match If not found, splay last node in treeSearch add Splay node for new Entry remove If found, splay parent of external node removed If not found, splay last node in treeSearch Only internal nodes Only internal nodes can be splayed When operation end with leaf, splay its parent
30
For Next Lecture
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.