Top Banner
SPLAY TREES SPLAY TREES Invented in 1985 by Daniel Dominic Sleator and Robert Endre Tarjan Slides copied from website of Prof. D.W. Harder, Univ. of Waterloo
76
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Splay Trees

SPLAY TREESSPLAY TREES

Invented in 1985 by Daniel Dominic Sleator and Robert Endre Tarjan

Slides copied from website of Prof.  D.W. Harder, Univ. of Waterloo

Page 2: Splay Trees

BackgroundBackground• AVL trees and red black trees are binary• AVL trees and red‐black trees are binary search trees with logarithmic height– This ensures all operations are O(ln(n))– However, insertions and deletions are expensive , p

• Splay trees use the 90-10 ruleD t th t h b tl d i– Data that has been recently accessed is more likely to be accessed again in the near future.

– Idea used in memory and web browsers

Page 3: Splay Trees

Amortized Time BoundsAmortized Time Bounds

• In Splay trees we want to avoid balancing the tree but at the same time the worst case performance for a sequence of operations should be boundedshould be bounded.

• Amortized Run‐Time– Worst case performance for a single access can be O(N)

– Worst case performance of any M(≤N) operations bounded by O(M log N)y ( g )

Page 4: Splay Trees

Example (1/2)Example (1/2) 

• Insert the values 1, 2, 3, 4, …, n, in that order into a BST

• Now an access to 1 requires that a linked list be• Now, an access to 1 requires that a linked list be traversed , O(N)

Page 5: Splay Trees

Example 1 (2/2)Example 1 (2/2) 

However, we are interested in amortized run times:– We only require that n accesses have (n ln(n))timetime

– Thus O(ln(n)) of those accesses could still be O(n)

Page 6: Splay Trees

Achieving Amortized Time BoundsAchieving Amortized Time Bounds

• Cost of accessing a node is proportional to the depth of the nodep

• Restructure the tree by moving an accessed items toward the root called the rotate toitems toward the root, called the rotate‐to‐root strategy– If the item is access a second time than the second access is cheaper

Page 7: Splay Trees

Achieving Amortized Time Bounds (2)Achieving Amortized Time Bounds (2)

• How to implement the rotate‐to‐root strategy– Simplest solution would be to use the LL and RR protations of AVL trees

Page 8: Splay Trees

Example 2 (1/10)Example 2 (1/10)

• Consider an splay tree with five entries – 1, 2, 3, 4 and 5 in that order

• Consider accessing 1 and rotate it to root

Page 9: Splay Trees

Example 2 (2/10)Example 2 (2/10)

• Rotating 1 and 2

Page 10: Splay Trees

Example 2 (3/10)Example 2 (3/10)

• Rotating 1 and 3

Page 11: Splay Trees

Example 2 (4/10)Example 2 (4/10)

• Rotating 1 and 4

Page 12: Splay Trees

Example 2 (5/10)Example 2 (5/10)

Rotating 1 and 5– The result still looks like a linked list– When we moved an item to root via rotations, other items are pushed deeperother items are pushed deeper

Page 13: Splay Trees

Example 2 (6/10)Example 2 (6/10)

• Next if we access 2, there is not much improvementp

Page 14: Splay Trees

Example 2 (7/10)Example 2 (7/10)

• Rotate around 4

Page 15: Splay Trees

Example 2 (8/10)Example 2 (8/10)

• Rotate around 5

Page 16: Splay Trees

Example 2 (9/10)Example 2 (9/10)

• Rotate around 1

Page 17: Splay Trees

Example 2 (10/10)Example 2 (10/10)

d i h h i h f• Move 2 to root does not improve the height of tree much• The resulting tree is shallower by only 1

• Conclusion: Single rotations do not help• Converts a linked list into another linked list

Page 18: Splay Trees

Depth 2 rotations (1/2)Depth 2 rotations (1/2)

There are two types of depth 2 rotations. In the first case, two rotations at the root bring A , gto the root

We will call this a zig zig rotation (unique to– We will call this a zig-zig rotation (unique to SPLAY trees)

Page 19: Splay Trees

Depth 2 rotations (2/2)Depth 2 rotations (2/2)

In the second, two rotations bring B to the root– We will call this a zig-zag rotation (same as a

double rotation)double rotation)

Page 20: Splay Trees

Depth 1 rotationDepth 1‐rotation

If the accessed node is a child of the root, we must revert to a single rotation:g– A zig rotation

Page 21: Splay Trees

Operations: InsertionOperations: Insertion

• Splay operation is performed after each access

• Inserting a new element into a splay tree follows the binary search tree model:follows the binary search tree model:– Insert the node as per a standard binary

search tree– Splay the object to the root

Page 22: Splay Trees

Operations: FindOperations: Find

• If the search is successful, the node found is splayed and becomes the new root.p y

• If unsuccessful, the last accessed node prior to reaching null is splayed and becomes the newreaching null is splayed and becomes the new root.– This behaviour is essential because we could repeatedly search for a non‐existent item and performance would drop to linear time. 

Page 23: Splay Trees

ExamplesExamples

With a little consideration, it becomes obvious that inserting 1 through 10, in that g g ,order, will produce the splay tree

Page 24: Splay Trees

Example: Find 1Example: Find 1We begin with a zig-zig rotationWe begin with a zig zig rotation

Page 25: Splay Trees

This is followed by another zig-zig operation...p

Page 26: Splay Trees

...and another

Page 27: Splay Trees

...and another

Page 28: Splay Trees

At this point, this requires a single zig operation to bring 1 to the rootp g

Page 29: Splay Trees

The height of this tree is now 6 and no longer 9g

Page 30: Splay Trees

Example: Find 3Example: Find 3

The deepest node is now 3:– This node must be splayed to the root p y

beginning with a zig-zag operation

Page 31: Splay Trees

The node 3 is rotated up– Next we require a zig-zig operationq g g p

Page 32: Splay Trees

Finally, to bring 3 to the root, we need a zig-zag operationg g p

Page 33: Splay Trees

The height of this tree is only 4

Page 34: Splay Trees

Example: Find 9Example: Find 9

Of th th d t d 9 iOf the three deepest nodes, 9 requires a zig-zig operation, so will access it next– The zig-zig operation will push 6 and its left

sub-tree downsub tree down

Page 35: Splay Trees

This is closer to a linked list; however, we’re not finished– A zig-zag operation will move 9 to the root

Page 36: Splay Trees

In this case, the height of the tree is now greater: 5g

Page 37: Splay Trees

Example: Find 5Example: Find 5

Accessing the deepest node, 5, we must begin with a zig-zag operationg g g p

Page 38: Splay Trees

Next, we require a zig-zag operation to move 5 to the location of 3

Page 39: Splay Trees

Finally, we require a single zig operation to move 5 to the root

Page 40: Splay Trees

The height of the tree is 4; however, 7 of the nodes form a perfect tree at the rootp

Page 41: Splay Trees

Example: Find 7Example: Find 7

Accessing 7 will require two zig-zag operationsp

Page 42: Splay Trees

The first zig-zag moves it to depth 2

Page 43: Splay Trees

7 is promoted to the root through a zig-zagoperationp

Page 44: Splay Trees

Example: Find 2Example: Find 2

Finally, accessing 2, we first require a zig-zag operationg p

Page 45: Splay Trees

This now requires a zig-zig operation to promote 2 to the rootp

Page 46: Splay Trees

In this case with 2 at the root 3 10 mustIn this case, with 2 at the root, 3-10 must be in the right sub-tree– The right sub-tree happens to be AVL

balanced

Page 47: Splay Trees

DeletionDeletion

Removing a node also follows the pattern of a binary search treey– Find the node to be deleted, which brings it to

the root Delete the node We get twothe root. Delete the node. We get two subtrees L and R.Find the minimum of the right sub tree R (or– Find the minimum of the right sub-tree R (or the largest of the left sub-tree L) M k L th l ft bt f R– Make L the left subtree of R

Page 48: Splay Trees

ExamplesExamples

To remove a node, for example, 6, splay it to the root– First we require a zig-zag operation

Page 49: Splay Trees

ExamplesExamples

At this point, we need a zig operation to move 6 to the root

Page 50: Splay Trees

ExamplesExamples

Delete the node 6

Page 51: Splay Trees

• We have two subtrees L and R. Find the minimum of R.

L R

Page 52: Splay Trees

• Make L the left tree of R • Thus we have removed 6 and theThus, we have removed 6 and the

resulting tree is, again, reasonably balancedbalanced

Page 53: Splay Trees

Top down Splay TreesTop‐down Splay Trees

• Bottom‐up require a pass down the tree to access and a second pass back up the tree to p psplay.– Requires more overhead– Requires more overhead

• Top‐down approach: As we descend the tree searching for a node, take the nodes that are on the access path and move them and their psubtrees out of the way

Page 54: Splay Trees

• X: Current Node• L: Tree that stores node less than XL: Tree that stores node less than X• R: Tree that stores node greater than X• Descend the tree two levels at a time, we encounter a pair of nodesp– Depending on whether the nodes are smaller or larger than X place them in L or Rlarger than X, place them in L or R

Page 55: Splay Trees

• The current node is always the root of the middle tree

• When the searched item is finally reached attach L and R to the bottom of the middleattach L and R to the bottom of the middle tree

• Place nodes in L and R and perform reattachmentreattachment

Page 56: Splay Trees

Top down: ZigTop‐down: Zig

• Zig Rotation– Bring Y to rootg

Page 57: Splay Trees

Top down: Zig ZigTop‐down: Zig‐Zig

• Zig‐zig Rotation– Bring Z to rootg

Page 58: Splay Trees

Top down: Zig ZagTop‐down: Zig‐Zag

• Zig‐zag Rotation– Bring Z to rootg

Page 59: Splay Trees

Simplified Top down Zig ZagSimplified Top‐down Zig‐Zag

• Instead of making Z the root make Y the root– Simplifies coding but descent of only one level at a p g ytime results in more iterations

Page 60: Splay Trees

Re attachementRe‐attachement

• L, R and the middle trees are rearranged to get a single treeg g

Page 61: Splay Trees

ExampleExample

• Find 19• Perform a simplified zig‐zagPerform a simplified zig zag

Page 62: Splay Trees

• Perform a zig‐zig

Page 63: Splay Trees

• Perform a zig

Page 64: Splay Trees

• We have reached a null link– Element 19 not found

• Reassemble

Page 65: Splay Trees

• Final splay tree

Page 66: Splay Trees

ReviewReview

• Show the result of inserting 3, 1, 4, 5, 2, 9, 6 and 8 into a– Bottom‐up splay treeTop down splay tree– Top‐down splay tree

Page 67: Splay Trees

Example: Bottom up Splay treeExample: Bottom‐up Splay tree

• Insert 3, 1, 4, 5, 2, 9, 6 and 8

33

Page 68: Splay Trees

• Insert 3, 1, 4, 5, 2, 9, 6 and 8

3 Zig 13

1

Zig 1

33

Page 69: Splay Trees

• Insert 3, 1, 4, 5, 2, 9, 6 and 8

1 Zig‐zig4

3

Zig zig

3

41

Page 70: Splay Trees

• Insert 3, 1, 4, 5, 2, 9, 6 and 8

Zig4 5

Zig

3 5 4

31 3

1

Page 71: Splay Trees

• Insert 3, 1, 4, 5, 2, 9, 6 and 8

Zig‐zag

5 5Zi

251Zig zag4

3

2Zig 51

43

1 24

3

1 33

Page 72: Splay Trees

Example: Top down Splay treeExample: Top‐down Splay tree

• Insert 3, 1, 4, 5, 2, 9, 6 and 8

33

Page 73: Splay Trees

• Insert 3, 1, 4, 5, 2, 9, 6 and 8

3L R L R

31

1L R

133

Page 74: Splay Trees

• Insert 3, 1, 4, 5, 2, 9, 6 and 8

1L R

3 4

LR

1

3 1

3

44

3

1

Page 75: Splay Trees

• Insert 3, 1, 4, 5, 2, 9, 6 and 8

L R L R4 54 5

3 5

4

34

1 11

3

1

Page 76: Splay Trees

• Insert 3, 1, 4, 5, 2, 9, 6 and 8

L R5

4

L R L R

3 44

31 2 5

1

3

2 L R

42 2

5

4

315

4

3

153