Splay Trees

Post on 02-Dec-2014

51 views 1 download

Tags:

Transcript of 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

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

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 )

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)

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)

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

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

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

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

• Rotating 1 and 2

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

• Rotating 1 and 3

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

• Rotating 1 and 4

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

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

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

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

• Rotate around 4

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

• Rotate around 5

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

• Rotate around 1

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

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)

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)

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

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

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. 

ExamplesExamples

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

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

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

...and another

...and another

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

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

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

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

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

The height of this tree is only 4

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

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

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

Example: Find 5Example: Find 5

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

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

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

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

Example: Find 7Example: Find 7

Accessing 7 will require two zig-zag operationsp

The first zig-zag moves it to depth 2

7 is promoted to the root through a zig-zagoperationp

Example: Find 2Example: Find 2

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

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

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

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

ExamplesExamples

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

ExamplesExamples

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

ExamplesExamples

Delete the node 6

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

L R

• 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

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

• 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

• 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

Top down: ZigTop‐down: Zig

• Zig Rotation– Bring Y to rootg

Top down: Zig ZigTop‐down: Zig‐Zig

• Zig‐zig Rotation– Bring Z to rootg

Top down: Zig ZagTop‐down: Zig‐Zag

• Zig‐zag Rotation– Bring Z to rootg

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

Re attachementRe‐attachement

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

ExampleExample

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

• Perform a zig‐zig

• Perform a zig

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

• Reassemble

• Final splay tree

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

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

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

33

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

3 Zig 13

1

Zig 1

33

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

1 Zig‐zig4

3

Zig zig

3

41

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

Zig4 5

Zig

3 5 4

31 3

1

• 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

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

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

33

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

3L R L R

31

1L R

133

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

1L R

3 4

LR

1

3 1

3

44

3

1

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

L R L R4 54 5

3 5

4

34

1 11

3

1

• 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