ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/Spla… · Web viewthis data structure...

37
Splay Trees In Theory Non-Balanced(ish) Binary Search Tree o the focus is on finding the most frequently searched for items fast!! not balanced!! o the “ish” makes it faster to build since more work to make perfect Last node PLACED or SEARCHED in tree is rotated to the root node o placed log n on average to find place for node in BST log n on average to rotate to root o searched log n on average to find the node in the BST log n on average to rotate to root no rotation necessary if the node is already at root this data structure is used for SEARCHING repeatedly used items o gives quicker access to data you have recently used o this only works if there is a small set of values that are accessed over and over again over a large set of options o this is where you will see its value o and the tree does NOT have to be balanced!! this saves time!! o building it stinks at times 1

Transcript of ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/Spla… · Web viewthis data structure...

Page 1: ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/Spla… · Web viewthis data structure is used for SEARCHING repeatedly used items gives quicker access to data you have

Splay TreesIn Theory

Non-Balanced(ish) Binary Search Treeo the focus is on finding the most frequently searched for items fast!!

not balanced!!o the “ish” makes it faster to build since more work to make perfect

Last node PLACED or SEARCHED in tree is rotated to the root nodeo placed

log n on average to find place for node in BST log n on average to rotate to root

o searched log n on average to find the node in the BST log n on average to rotate to root no rotation necessary if the node is already at root

this data structure is used for SEARCHING repeatedly used items o gives quicker access to data you have recently usedo this only works if there is a small set of values that are accessed over and

over again over a large set of optionso this is where you will see its valueo and the tree does NOT have to be balanced!!

this saves time!!o building it stinks at times

Splay Trees in Theory

1

Page 2: ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/Spla… · Web viewthis data structure is used for SEARCHING repeatedly used items gives quicker access to data you have

Splay Tree IntroductionCurrent Tree Added 36 Looked up 34

(no numbers added)

What’s “on average mean”?2

Splay Trees are meant for reoccurring searchesAfter hundreds of searches with 15, 23 & 35 predominate

Page 3: ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/Spla… · Web viewthis data structure is used for SEARCHING repeatedly used items gives quicker access to data you have

some of the operations can be slow there is a case where a single operation can be linear in time

o O(n) in the worst case have a few operations that are slow, but the rest are so fast, than they average

out

3

Page 4: ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/Spla… · Web viewthis data structure is used for SEARCHING repeatedly used items gives quicker access to data you have

Building a Splay Tree the node is placed as normal in the BST/Splay Tree then using rotations, that newly inserted node is moved to the root this is done for EVERY node

Building a TreePlaced Rotated to Root

4

Page 5: ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/Spla… · Web viewthis data structure is used for SEARCHING repeatedly used items gives quicker access to data you have

Rotations in General completed when ANY node is accessed (searched) or placed even though it looks like a lot of works, notice how few links are changed in

realityo look at building a tree illustration

recursively done from bottom upo rotations from the bottom continue rotating all the way to the top

rotations must maintain BST when completed (from bottom to top) the tree will NOT be balanced!!!

General Splay Tree Rotations(proof shown below)

Proving the rotations work5

Page 6: ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/Spla… · Web viewthis data structure is used for SEARCHING repeatedly used items gives quicker access to data you have

Zigtoo easy, Child handoff on “B” too easyzig-zagBefore After

zig-zigBefore After

1. Is it still a BST??2. How many links changed? (new parent or child)3. Why did the links have to change?4. In zig-zig, who lost and who gained a child? Why?

Simple Splay Rotations – Level 1 these rotations are only done when the tree is young and on level 1

6

Page 7: ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/Spla… · Web viewthis data structure is used for SEARCHING repeatedly used items gives quicker access to data you have

simple Zig-Left and Zig-Right remember the NEWEST node is

o placed in BST order FIRSTo then rotate to have that newest node up top

Zig-Left and Zig-Right Rotation

A,B,C sub-trees could be many nodes, or NONE!!!

this is of course mirrored

7

Page 8: ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/Spla… · Web viewthis data structure is used for SEARCHING repeatedly used items gives quicker access to data you have

Simple Zig-Left and Zig-Right Rotation TypesOriginal Splay Tree

Zig-Left (add 50)Placement Phase Rotation Phase Steps

Rotate around original parent

Zig-Right (add 10)Placement Phase Rotation Phase Steps

Rotate around original parent

Only works with the need of level 1 nodes

8

Page 9: ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/Spla… · Web viewthis data structure is used for SEARCHING repeatedly used items gives quicker access to data you have

Zig-Zig and Zig-Zag tree makeupIdentifying Zig-Zig vs. Zig-Zag

Zig-Zig Zig-Zag

X = is the lest node to be added

this happens in the tree AFTER level 1 the difference between the two are the links between the IMMEDIATE Parent

and newest node Xo zig-zig

both Parent and X links for a straight lineo zig-zag

Parent and X links for a non-straight line Also notice that G (grandparent) now plays a part

9

Page 10: ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/Spla… · Web viewthis data structure is used for SEARCHING repeatedly used items gives quicker access to data you have

Performing the Zig-Zig and Zig-Zag again, these are AFTER the new node X bas been placed depending on where X is placed, we decide on zig or zag beyond level 1, where we have a G (grandfather is in play)

Zig-Zag and Zig-Zig Rotation

What links changed?What children were given new parents?

Is it a BST (at the end)?What does the triangle (A,B,C,D) mean?

How many levels did “x” go up maximum??

10

Page 11: ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/Spla… · Web viewthis data structure is used for SEARCHING repeatedly used items gives quicker access to data you have

Simple Zig-Zag and Zig-Zig Rotation TypesOriginal Splay Tree

Zig-Zig Rotation (add 10)Setup Step 1 Final Steps

1. rotate P around G to make P the highest position, dragging X with it

2. rotate X around P to make X the highest position, dragging whatever is below X with it

this is done if mirrored!!!

Zig-Zag Rotation (add 40)Setup Step 1 Final

(NOTICE THIS IS TEMPORARILY NOT A BST)

1. swap X and P2. rotate X around G to

make X the highest position, dragging whatever is below X with it

OR PUSH X up in between P and G!!this is done if mirrored!!!

ZIG != ZIG-ZIG

11

Page 12: ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/Spla… · Web viewthis data structure is used for SEARCHING repeatedly used items gives quicker access to data you have

The limited reach of rotations at most a rotation affect 2 levels either zig-zig or zig-zag will immediately affect only G, P and X nodes and

any children they might have

Showing the limits of rotationsOriginal Tree

Adding 99

notice we will perform a zig-zigAfter one rotation

BTW, we are done with the Splay for that sub-tree.

What rotation do we have left?

12

Page 13: ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/Spla… · Web viewthis data structure is used for SEARCHING repeatedly used items gives quicker access to data you have

Building Example with Rotations we won’t be able to build very far before we start having recursive rotations using all rotations as shown before

Starting from scratch1 add first node 25, nothing to do

2 add 50. Position node in BST tree

3 splay new node to top of tree. Zig Left.

4 add 10. Position node in BST tree

5 identify we are using a zig-zig to rotate. Rotate.

6 add 1. Position node in BST tree.

13

Page 14: ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/Spla… · Web viewthis data structure is used for SEARCHING repeatedly used items gives quicker access to data you have

7 identify we are using a zig-right to rotate. Rotate.

8 add 5. Position node in BST tree.

9 identify we are using a zig-zag to rotate. Rotate.

Try a few yourself:#1 5, 9, 3, 1, 2, 0 (complete together)

a. Draw each addition to the Splay Treea. Placement phaseb. Rotation phase, only done when the newest node is at the top!

b. Write down every type of rotation completed (zig-left, zig-right, zig-zag, zig-zig)

c. Stop after completedAnswerb:

#2 9, 7, 8, 5, 3, 1, 2 Answerb:

14

Page 15: ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/Spla… · Web viewthis data structure is used for SEARCHING repeatedly used items gives quicker access to data you have

Recursive Zig-Zags and Zig-Zigs starting from the bottom up of a larger tree, just to get the newest node to the

top will take several rotations if the newest node is placed at level 2 and down we use the SAME rotation as before, but the scope changes each time

o new G and P

Recursive Rotations

1 add 15. Position node in BST tree.

2 Label G and P, identify we are using a zig-zag to rotate. Rotate.

15

Page 16: ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/Spla… · Web viewthis data structure is used for SEARCHING repeatedly used items gives quicker access to data you have

3

4 Label G and P, identify we are using a simple zig-left to rotate. Rotate.

5Oh?? How did 10 happen?? (answered below)

16

Page 17: ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/Spla… · Web viewthis data structure is used for SEARCHING repeatedly used items gives quicker access to data you have

Handling Child Handoffs looks hard, but common sense if X needs to move up, but has to lose a child since it’s about to gain one

o previously it’s parent

Child Handoff ScenariosRotate X left && X has a left child

Rotate X right && X has a right child

just mirrored from above just mirrored from above

child needs to go to a spot where it is STILL in BST ordero attach to who was JUST rotated to a lower position

they HAVE to have a link free since it’s child is now the root!! same link is reused!!

17

Page 18: ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/Spla… · Web viewthis data structure is used for SEARCHING repeatedly used items gives quicker access to data you have

Chocked Full Rotation Example 2Step 1(no child handoff)

Step 2

Step 3

18

Page 19: ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/Spla… · Web viewthis data structure is used for SEARCHING repeatedly used items gives quicker access to data you have

Complicated Zig-Zig rotations w/ Kids kids ALWAYS make things complicated may not be able to hand off of the demoted item still will be a logical spot to place any “issue” nodes since it still needs to

result in a BST

Zig-Zig with kids scenariosBefore Rotation After Rotation

(normal zig-zig)

kidspivot Ntarget(X)

N

kidspivot Ytarget(X)

N

19

Page 20: ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/Spla… · Web viewthis data structure is used for SEARCHING repeatedly used items gives quicker access to data you have

kidson pivot? Ntarget(X) Y

YOU SOLVE!!!1. Which node is moving

up?2. ID the “kid” that will be

the issue3. Complete the rotation

without the kid4. Add the kid back

Answerb:

KidsOn pivot? Ytarget(X) Y

YOU SOLVE!!!Answerb:

(give at least 30 minutes to complete, last 15 for rest of notes)

Try as a group:#1 48, 41, 3, 6, 82, 70 (X w/ 2 kids promotion here!), 49, 52, 100, 43Try a few on your own:#2 10, 91, 11, 65, 80, 92, 45, 37, 7, 12#3 95, 18, 94, 1, 75, 51, 21, 3, 29, 8Answerb:

20

Page 21: ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/Spla… · Web viewthis data structure is used for SEARCHING repeatedly used items gives quicker access to data you have

Deleting requires finding the node!! Still Splay the found node!! then delete it, and move left up into it’s spot

o but this has complications if LEFT already has two childreno swap with either

rightmost child of it’s left sub tree leftmost child of it’s right sub tree this should sound familiar!!

this should not happen often with this data structureo not made for this!!

Look Up or “Find” this is why this data structure is used the overall goal when searching is to

o move the accessed node to the topo but try to keep nodes already close to the top still closeo not all nodes can stay at the top

nodes not accessed slowly move down the treeo which is fine since we don’t access these much!

given an element to search for “k”o begins like any BST

going left or right until we find a key we are looking for or dead end if NOT in tree

o “X” is where the search ended, weather found or not “splay” X to root of tree using the rotations we already covered

o again, we are assuming X will be searched for over and over again now it is higher up the tree, so a quicker search next time

21

Page 22: ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/Spla… · Web viewthis data structure is used for SEARCHING repeatedly used items gives quicker access to data you have

Seeing a Splay Tree in ActionStarting fully built tree. 15, 23, and 35 will be the values searched for the most

Finding 15 for the first time

Finding 35 for the first time

22

Page 23: ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/Spla… · Web viewthis data structure is used for SEARCHING repeatedly used items gives quicker access to data you have

Finding 23 for the first time

Once in a while an outlier (17)

After hundreds of searches with 15, 23 & 35 predominate

Performance vs. Other Trees (Data Structures)23

Page 24: ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/Spla… · Web viewthis data structure is used for SEARCHING repeatedly used items gives quicker access to data you have

Remember, Splay Trees aren’t used for everything! in order for it to really be worthwhile it needs

o large data seto a few items out of that data set that are used/searched over and overo hundreds of searches done

Other tree organization plans take longer to find a valueo same take a lot to construct as well!!

Comparing the various BST typesSplay Tree

Red-Black-Tree

AVL Tree

24

Page 25: ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/Spla… · Web viewthis data structure is used for SEARCHING repeatedly used items gives quicker access to data you have

#1 Complete a searching splay on the value 2 in the tree above.#2 Complete a searching splay on the value 89 in the tree above. (tree was reset)#3 Complete a searching splay on the value 67 in the tree above. (tree was reset)#4 Complete a searching splay on the value 56 in the tree above. (tree was reset)Answersb:

So what is splay? The function used after insertion or searching in order to move the target node

to it’s new root position Why/When am I using this?

If a very select few pieces of data in a massive set are accessed over and over againo cacheso memory allocatorso routerso garbage collectorso data compression

remember, this is not BUILDING or ADDING to the tree, this is only LOOKING UP DATA

25

Page 26: ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/Spla… · Web viewthis data structure is used for SEARCHING repeatedly used items gives quicker access to data you have

Splay Tree vs. AVL/RB Tree Debate pros of Splay Trees

o only requires two pointers with no additional informationo self-adjusting

cons of Splay Treeso search on a splay tree may be O(N) if elements are inserted in ordero with random input, splay tree is also slower than AVL and red-black

trees in building

Big Oh operations and stats all operation take O(log n) time on average

o some operations can be very slowo O(n) in the WORST case

n is the number of items in the tree most of the operations are so fast O(log n), and only a few times EVER will

the operations take O(n), that the faster operations make up for the slower instances

Easier to program since in reality, links are moved around, not data

26

Page 27: ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/Spla… · Web viewthis data structure is used for SEARCHING repeatedly used items gives quicker access to data you have

Answer SectionBuilding Splay Trees without Recursion

#1 5, 9, 3, 1, 2, 0 (complete together)http://youtu.be/zPrs01PXtq0 (silent, no audio, run at x2 speed)

#2 9, 7, 8, 5, 3, 1, 2http://youtu.be/J38BtDZM1r8 (silent, no audio, run at x2 speed)

Zig-Zigs with KidsBefore Rotation After

27

Page 28: ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/Spla… · Web viewthis data structure is used for SEARCHING repeatedly used items gives quicker access to data you have

28

Page 29: ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/Spla… · Web viewthis data structure is used for SEARCHING repeatedly used items gives quicker access to data you have

Building Full Splay Trees

#1 48, 41, 3, 6, 82, 70, 49, 52, 100, 43http://youtu.be/BWQoL6qoHwU

#2 10, 91, 11, 65, 80, 92, 45, 37, 7, 12http://youtu.be/QNs_Kzkrqfw

#3 95, 18, 94, 1, 75, 51, 21, 3, 29, 8http://youtu.be/I7LPx-iANCc

Searching Splay Trees#1 https://www.youtube.com/watch?v=omaUL0gX4v8#2 https://www.youtube.com/watch?v=eP29hqFPP2c#3 https://www.youtube.com/watch?v=GSKQu1qGd1E#4 https://www.youtube.com/watch?v=wuVXw6Su8rw

29

Page 30: ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/Spla… · Web viewthis data structure is used for SEARCHING repeatedly used items gives quicker access to data you have

Sourceshttp://www.cs.usfca.edu/~galles/visualization/SplayTree.htmlhttp://attractivechaos.wordpress.com/2008/10/02/comparison-of-binary-search-trees/http://www.csee.umbc.edu/courses/undergraduate/341/spring07/Lectures/Splay/http://www.drdobbs.com/cpp/implementing-splay-trees-in-c/184402007

30