+ All Categories
Home > Documents > Lecture 20: Screw that, I want It All

Lecture 20: Screw that, I want It All

Date post: 23-Feb-2016
Category:
Upload: melba
View: 28 times
Download: 0 times
Share this document with a friend
Description:
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. - PowerPoint PPT Presentation
Popular Tags:
26
LECTURE 20: SCREW THAT, I WANT IT ALL CSC 213 – Large Scale Programming
Transcript
Page 1: Lecture 20: Screw that, I want It All

LECTURE 20:SCREW THAT, I WANT IT ALL

CSC 213 – Large Scale Programming

Page 2: 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?

Page 3: 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? BST: Key for Entry added first

Page 4: 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? BST: Key for Entry added first

AVLTree: Random Entry near midpoint

Page 5: 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? BST: Key for Entry added first

AVLTree: Random Entry near midpoint

SplayTree: Most recently used key

Page 6: Lecture 20: Screw that, I want It All

Building a SplayTree

Another approach which builds upon BST Not an AVLTree, however, but a new BST

subclass

Page 7: Lecture 20: Screw that, I want It All

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

Page 8: Lecture 20: Screw that, I want It All

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

Page 9: Lecture 20: Screw that, I want It All

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

Page 10: Lecture 20: Screw that, I want It All

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

Page 11: Lecture 20: Screw that, I want It All

How To Splay

Page 12: Lecture 20: Screw that, I want It All

How To Splay

Uses trinode restructuring but 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

Page 13: Lecture 20: Screw that, I want It All

When To Use Splay Tree

What applications are good for a splay tree?

Where would splay trees be a BAD IDEA?

Page 14: Lecture 20: Screw that, I want It All

Splay Node Rotations

Uses different nodes than previous rotations AVLTree moves node down to balance

tree's tao Can now be greedy: make it SplayTree's

root

Page 15: Lecture 20: Screw that, I want It All

Splay Node Rotations

Uses different nodes than previous rotations AVLTree moves node down to balance

tree's tao Can now be greedy: make it SplayTree's

root

AVLTree

Page 16: Lecture 20: Screw that, I want It All

Splay Node Rotations

Uses different nodes than previous rotations AVLTree moves node down to balance

tree's tao Can now be greedy: make it SplayTree's

root

AVLTree SplayTree

Page 17: Lecture 20: Screw that, I want It All

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 &

childrenparent

node

T2 T3

T4

grandparent

T1

parent

node

T2 T3 T4

grandparent

T1

Page 18: Lecture 20: Screw that, I want It All

Zig-Zig When Splaying a Tree When node, parent, & grandparent in a

line Rotation differs from AVL tree's trinode

restructure

parent

node

grandparent

T1

T4

T2

T3

Page 19: Lecture 20: Screw that, I want It All

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

rootparent

node

grandparent

T1

T4

T2

T3

parent

node

T2

T3

T4grandparent

T1

Page 20: Lecture 20: Screw that, I want It All

Splaying Right Child Of Root

root

node

T3

T1

T2

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

Page 21: Lecture 20: Screw that, I want It All

Splaying Right Child Of Root

root

node

T3

T1

T2

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

Page 22: Lecture 20: Screw that, I want It All

Splaying Right Child Of Root

root

node root

node

T2

T3

T1T3

T1

T2

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

Page 23: Lecture 20: Screw that, I want It All

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

T2

T3

T1

root

node

T3

T1

T2

node

root

Page 24: Lecture 20: Screw that, I want It All

Which Node Gets Splayed?

Method

Node to splay

findIf found, splay node containing matchIf not found, splay last node in treeSearch

add Splay node for new Entry

remove

If found, splay parent of external node removedIf not found, splay last node in treeSearch

Page 25: Lecture 20: Screw that, I want It All

Which Node Gets Splayed?

Only internal nodes can be splayed When operation end with leaf, splay its

parent

Method

Node to splay

findIf found, splay node containing matchIf not found, splay last node in treeSearch

add Splay node for new Entry

remove

If found, splay parent of external node removedIf not found, splay last node in treeSearch

Page 26: Lecture 20: Screw that, I want It All

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


Recommended