0% found this document useful (0 votes)
133 views76 pages

Splay Trees Splay Trees: Invented in 1985 by Daniel Dominic Sleator and Robert Endre Tarjan

Splay trees are self-adjusting binary search trees that provide fast average-case performance for lookup, insertion, and deletion operations. They use a technique called splaying to move frequently accessed elements closer to the root through rotations. This improves future access times by reducing the tree height. Splaying an accessed node involves performing single or double rotations to restructure the tree until the node reaches the root. This amortizes the cost of operations over many accesses and ensures logarithmic time bounds on average.

Uploaded by

vetkolisanket
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
133 views76 pages

Splay Trees Splay Trees: Invented in 1985 by Daniel Dominic Sleator and Robert Endre Tarjan

Splay trees are self-adjusting binary search trees that provide fast average-case performance for lookup, insertion, and deletion operations. They use a technique called splaying to move frequently accessed elements closer to the root through rotations. This improves future access times by reducing the tree height. Splaying an accessed node involves performing single or double rotations to restructure the tree until the node reaches the root. This amortizes the cost of operations over many accesses and ensures logarithmic time bounds on average.

Uploaded by

vetkolisanket
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 76

SPLAYTREES SPLAY TREES

Inventedin1985by DanielDominicSleator andRobert Endre Tarjan

SlidescopiedfromwebsiteofProf.D.W.Harder,Univ.ofWaterloo

Background
AVL trees and red black trees are binary AVLtreesandredblacktreesarebinary searchtreeswithlogarithmicheight
ThisensuresalloperationsareO(ln(n)) However, insertions and deletions are expensive , p

Splay trees use the 90-10 rule


D t th t h b Datathathasbeenrecentlyaccessedismore tl di likelytobeaccessedagaininthenearfuture. Idea used in memory and web browsers

AmortizedTimeBounds Amortized Time Bounds


InSplaytreeswewanttoavoidbalancingthe treebutatthesametimetheworstcase performanceforasequenceofoperations shouldbebounded. should be bounded AmortizedRunTime
Worstcaseperformanceforasingleaccesscanbe O(N) WorstcaseperformanceofanyM(N)operations boundedbyO(MlogN) y ( g )

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


Insertthevalues1,2,3,4,,n,inthatorderintoaBST

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

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


However,weareinterestedinamortizedrun times:
Weonlyrequirethatn accesseshave(n ln(n)) time ThusO(ln(n)) ofthoseaccessescouldstillbeO(n)

AchievingAmortizedTimeBounds Achieving Amortized Time Bounds


Costofaccessinganodeisproportionaltothe p depthofthenode Restructurethetreebymovinganaccessed itemstowardtheroot,calledtherotateto items toward the root called the rotate to root strategy
Iftheitemisaccessasecondtimethanthe secondaccessischeaper

AchievingAmortizedTimeBounds(2) Achieving Amortized Time Bounds (2)


Howtoimplementtherotatetoroot strategy
SimplestsolutionwouldbetousetheLLandRR p rotationsofAVLtrees

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


Consideransplaytreewithfiveentries 1,2, 3,4and5inthatorder Consideraccessing1androtateittoroot

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


Rotating1and2

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


Rotating1and3

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


Rotating1and4

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


Rotating1and5
Theresultstilllookslikealinkedlist Whenwemovedanitemtorootviarotations, otheritemsarepusheddeeper other items are pushed deeper

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


Nextifweaccess2,thereisnotmuch p improvement

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


Rotatearound4

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


Rotatearound5

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


Rotatearound1

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


Move2torootdoesnotimprovetheheightof d i h h i h f treemuch
Theresultingtreeisshallowerbyonly1

Conclusion:Singlerotationsdonothelp
Convertsalinkedlistintoanotherlinkedlist

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


Therearetwotypesofdepth2rotations.In the , g first case, two rotations at the root bring A to the root
We will call this a zig zig rotation (unique to zig-zig SPLAY trees)

Depth2rotations(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)

Depth1rotation Depth 1 rotation


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

Operations:Insertion Operations: Insertion


Splay operation is performed after each access Inserting a new element into a splay tree follows the binary search tree model:
Insert the node as per a standard binary search tree Splay the object to the root

Operations:Find Operations: Find


Ifthesearchissuccessful,thenodefoundis p y splayedandbecomesthenewroot. Ifunsuccessful,thelastaccessednodepriorto reachingnullissplayedandbecomesthenew reaching null is splayed and becomes the new root.
Thisbehaviour isessentialbecausewecould repeatedlysearchforanonexistentitemand performancewoulddroptolineartime.

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

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

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

...and another

...and another

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

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

Example: 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 operation q g g p

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

The height of this tree is only 4

Example: Find 9
Of th th the three deepest nodes, 9 requires a d t d i zig-zig operation, so will access it next
The zig-zig operation will push 6 and its left sub tree sub-tree down

This is closer to a linked list; however, were not finished


A zig-zag operation will move 9 to the root

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

Example: Find 5
Accessing the deepest node, 5, we must g g g p begin with a zig-zag operation

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 p the nodes form a perfect tree at the root

Example: Find 7
Accessing 7 will require two zig-zag p operations

The first zig-zag moves it to depth 2

7 is promoted to the root through a zig-zag p operation

Example: Find 2
Finally, accessing 2, we first require a zigg p zag operation

This now requires a zig-zig operation to p promote 2 to the root

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

Deletion
Removing a node also follows the pattern y of a binary search tree
Find the node to be deleted, which brings it to the root Delete the node We get two root. node. subtrees L and R. Find the minimum of the right sub tree R (or sub-tree the largest of the left sub-tree L) M k L th l ft subtree of R Make the left bt f

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

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

Examples
Delete the node 6

Wehavetwosubtrees LandR.Findthe minimumofR. R L

Make L the left tree of R Thus we have removed 6 and the Thus, resulting tree is, again, reasonably balanced

TopdownSplayTrees Top down Splay Trees


Bottomuprequireapassdownthetreeto p p accessandasecondpassbackupthetreeto splay.
Requires more overhead Requiresmoreoverhead

Topdownapproach:Aswedescendthetree searchingforanode,takethenodesthatare p ontheaccesspathandmovethemandtheir subtrees outoftheway

X:CurrentNode L:TreethatstoresnodelessthanX L: Tree that stores node less than X R:TreethatstoresnodegreaterthanX Descendthetreetwolevelsatatime,we p encounterapairofnodes
Dependingonwhetherthenodesaresmalleror largerthanX,placetheminLorR larger than X place them in L or R

Thecurrentnodeisalwaystherootofthe middletree Whenthesearcheditemisfinallyreached attachLandRtothebottomofthemiddle attach L and R to the bottom of the middle tree PlacenodesinLandRandperform reattachment

Topdown:Zig Top down: Zig


Zig Rotation
BringYtoroot g

Topdown:ZigZig Top down: Zig Zig


Zigzig Rotation
BringZtoroot g

Topdown:ZigZag Top down: Zig Zag


Zigzag Rotation
BringZtoroot g

SimplifiedTopdownZigZag Simplified Top down Zig Zag


InsteadofmakingZtherootmakeYtheroot
Simplifiescodingbutdescentofonlyonelevelata p g y timeresultsinmoreiterations

Reattachement Re attachement
L,Randthemiddletreesarerearrangedto g getasingletree g

Example
Find19 Perform a simplified zigzag Performasimplifiedzig zag

Performazigzig

Performazig

Wehavereachedanulllink
Element19notfound

Reassemble

Finalsplaytree

Review
Showtheresultofinserting3,1,4,5,2,9,6 and8intoa
Bottomupsplaytree Top down splay tree Topdownsplaytree

Example:BottomupSplaytree Example: Bottom up Splay tree


Insert3,1,4,5,2,9,6and8
3

Insert3,1,4,5,2,9,6and8

3 1

Zig

1 3

Insert3,1,4,5,2,9,6and8
1 3 1 4 4
Zig zig Zigzig

Insert3,1,4,5,2,9,6and8
4
Zig

5 5 3 1 4

3 1

Insert3,1,4,5,2,9,6and8
5 4 3 1 2
Zig zag Zigzag

5 2 1 3 4
Zig Zi

2 1 4 3 5

Example:TopdownSplaytree Example: Top down Splay tree


Insert3,1,4,5,2,9,6and8
3

Insert3,1,4,5,2,9,6and8
L R L R

3 1
L

3
R

1 3

Insert3,1,4,5,2,9,6and8
L R L R

1 3 4 1

4 4 3 1

Insert3,1,4,5,2,9,6and8
L

4 3 5

4 3 1

5 4 3 1

Insert3,1,4,5,2,9,6and8
L

5 4 3

3 1
L

4 2 5
R

2 3

2 1 5 3 4 5

4 1

You might also like