splay trees
DESCRIPTION
Splay trees. Outline. This topic covers splay trees A binary search tree An alternate idea to optimizing run times A possible height of O( n ) but amortized run times of Q (ln( n )) Each access or insertion moves that node to the root Operations are zig-zag and zig-zig - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/1.jpg)
ECE 250 Algorithms and Data Structures
Douglas Wilhelm Harder, M.Math. LELDepartment of Electrical and Computer EngineeringUniversity of WaterlooWaterloo, Ontario, Canada
© 2006-2013 by Douglas Wilhelm Harder. Some rights reserved.
Splay trees
![Page 2: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/2.jpg)
2Splay trees
Outline
This topic covers splay trees– A binary search tree– An alternate idea to optimizing run times– A possible height of O(n) but amortized run times of Q(ln(n))– Each access or insertion moves that node to the root– Operations are zig-zag and zig-zig– Similar to, but different from, AVL trees
![Page 3: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/3.jpg)
3Splay trees
Background
AVL trees and red-black trees are binary search trees with logarithmic height– This ensures all operations are O(ln(n))
An alternative to maintaining a height logarithmic with respect to the number of nodes, an alternative idea is to make use of an old maxim:
Data that has been recently accessed is more likely tobe accessed again in the near future.
![Page 4: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/4.jpg)
4Splay trees
Background
Accessed nodes could be rotated or splayed to the root of the tree:– Accessed nodes are splayed to the root during the count/find operation– Inserted nodes are inserted normally and then splayed– The parent of a removed node is splayed to the root
Invented in 1985 by Daniel Dominic Sleator and Robert Endre Tarjan
![Page 5: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/5.jpg)
5Splay trees
Insertion at the Root
Immediately, inserting at the root makes it clear that we will still have access times that are O(n):– Insert the values 1, 2, 3, 4, …, n, in that order
– Now, an access to 1 requires that a linked list be traversed
![Page 6: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/6.jpg)
6Splay trees
Inserting at the Root
However, we are interested in amortized run times:– We only require that n accesses have Q(n ln(n)) time– Thus O(ln(n)) of those accesses could still be O(n)
![Page 7: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/7.jpg)
7Splay trees
Inserting at the Root
Before we consider insertions, how can we simply move an access node to the root?– We could consider AVL rotations, the simplest of which is:
![Page 8: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/8.jpg)
8Splay trees
Single Rotations
Unfortunately, as we will see, using just single rotations does not work
![Page 9: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/9.jpg)
9Splay trees
Single Rotations
Consider this splay tree with five entries– They were inserted in the order 1, 2, 3, 4 and 5– Let us access 1 by find it and then rotating it back to the root
![Page 10: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/10.jpg)
10Splay trees
Single Rotations
Rotating 1 and 2
![Page 11: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/11.jpg)
11Splay trees
Single Rotations
Rotating 1 and 3
![Page 12: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/12.jpg)
12Splay trees
Single Rotations
Rotating 1 and 4
![Page 13: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/13.jpg)
13Splay trees
Single Rotations
Rotating 1 and 5– The result still looks like a linked list
![Page 14: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/14.jpg)
14Splay trees
Single Rotations
Accessing 2 next doesn’t do much
![Page 15: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/15.jpg)
15Splay trees
Single Rotations
Accessing 2 next doesn’t do much
![Page 16: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/16.jpg)
16Splay trees
Single Rotations
Accessing 2 next doesn’t do much
![Page 17: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/17.jpg)
17Splay trees
Single Rotations
Accessing 2 next doesn’t do much
![Page 18: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/18.jpg)
18Splay trees
Single Rotations
Accessing 2 next doesn’t do much– The resulting tree is shallower by only 1
![Page 19: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/19.jpg)
19Splay trees
Single Rotations
Accessing 3 isn’t significant, either
![Page 20: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/20.jpg)
20Splay trees
Single Rotations
Accessing 3 isn’t significant, either
![Page 21: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/21.jpg)
21Splay trees
Single Rotations
Accessing 3 isn’t significant, either
![Page 22: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/22.jpg)
22Splay trees
Single Rotations
Accessing 3 isn’t significant, either– Essentially, it is two linked lists and the left sub-tree is turning into the
original linked list
![Page 23: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/23.jpg)
23Splay trees
Single Rotations
In a general splay tree created in the order1, 2, 3, 4, …, n
and then accessed repeated in the order1, 2, 3, 4, …, n
will require
comparisons—an amortized run time of O(n)
2 2 2
1 1
1 1O
2 2
n n
k k
n n n nn k n k n n
![Page 24: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/24.jpg)
24Splay trees
Single Rotations
Thus, a single rotation will not do– It can convert a linked list into a linked list
![Page 25: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/25.jpg)
25Splay trees
Depth-2 Rotations
Let’s try rotations with entries at depth 2– Suppose we are accessing A on the left and B on the right
![Page 26: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/26.jpg)
26Splay trees
In the first case, two rotations at the root bring A to the root– We will call this a zig-zig rotation
Depth-2 Rotations
![Page 27: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/27.jpg)
27Splay trees
In the second, two rotations bring B to the root– It doesn’t seem we’ve done a lot…– We will call this a zig-zag rotation
Depth-2 Rotations
![Page 28: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/28.jpg)
28Splay trees
Depth-2 Rotations
If the accessed node is a child of the root, we must revert to a single rotation:– A zig rotation
![Page 29: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/29.jpg)
29Splay trees
Operations
Accessing any node splays the node to the root
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
Removing a node also follows the pattern of a binary search tree– Copy the minimum of the right sub-tree– Splay the parent of the removed node to the root
![Page 30: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/30.jpg)
30Splay trees
Examples
With a little consideration, it becomes obvious that inserting 1 through 10, in that order, will produce the splay tree
![Page 31: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/31.jpg)
31Splay trees
Examples
We will repeatedly access the deepest node in the tree– With each operation, this node will be splayed to the root– We begin with a zig-zig rotation
![Page 32: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/32.jpg)
32Splay trees
Examples
This is followed by another zig-zig operation...
![Page 33: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/33.jpg)
33Splay trees
Examples
...and another
![Page 34: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/34.jpg)
34Splay trees
Examples
...and another
![Page 35: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/35.jpg)
35Splay trees
Examples
At this point, this requires a single zig operation to bring 1 to the root
![Page 36: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/36.jpg)
36Splay trees
Examples
The height of this tree is now 6 and no longer 9
![Page 37: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/37.jpg)
37Splay trees
Examples
The deepest node is now 3:– This node must be splayed to the root beginning with a zig-zag
operation
![Page 38: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/38.jpg)
38Splay trees
Examples
The node 3 is rotated up– Next we require a zig-zig operation
![Page 39: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/39.jpg)
39Splay trees
Examples
Finally, to bring 3 to the root, we need a zig-zag operation
![Page 40: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/40.jpg)
40Splay trees
Examples
The height of this tree is only 4
![Page 41: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/41.jpg)
41Splay trees
Examples
Of 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 down
![Page 42: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/42.jpg)
42Splay trees
Examples
This is closer to a linked list; however, we’re not finished– A zig-zag operation will move 9 to the root
![Page 43: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/43.jpg)
43Splay trees
Examples
In this case, the height of the tree is now greater: 5
![Page 44: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/44.jpg)
44Splay trees
Examples
Accessing the deepest node, 5, we must begin with a zig-zag operation
![Page 45: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/45.jpg)
45Splay trees
Examples
Next, we require a zig-zag operation to move 5 to the location of 3
![Page 46: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/46.jpg)
46Splay trees
Examples
Finally, we require a single zig operation to move 5 to the root
![Page 47: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/47.jpg)
47Splay trees
Examples
The height of the tree is 4; however, 7 of the nodes form a perfect tree at the root
![Page 48: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/48.jpg)
48Splay trees
Examples
Accessing 7 will require two zig-zag operations
![Page 49: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/49.jpg)
49Splay trees
Examples
The first zig-zag moves it to depth 2
![Page 50: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/50.jpg)
50Splay trees
Examples
7 is promoted to the root through a zig-zag operation
![Page 51: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/51.jpg)
51Splay trees
Examples
Finally, accessing 2, we first require a zig-zag operation
![Page 52: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/52.jpg)
52Splay trees
Examples
This now requires a zig-zig operation to promote 2 to the root
![Page 53: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/53.jpg)
53Splay trees
Examples
In 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 54: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/54.jpg)
54Splay trees
Examples
To remove a node, for example, 6, splay it to the root– First we require a zig-zag operation
![Page 55: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/55.jpg)
55Splay trees
Examples
At this point, we need a zig operation to move 6 to the root
![Page 56: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/56.jpg)
56Splay trees
Examples
We will now copy the minimum element from the right sub-tree– In this case, the node with 7 has a single sub-tree, we will simply move
it up
![Page 57: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/57.jpg)
57Splay trees
Examples
Thus, we have removed 6 and the resulting tree is, again, reasonably balanced
![Page 58: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/58.jpg)
58Splay trees
Performance
It is very difficult with small trees to demonstrate the amortized logarithmic behaviour of splay trees
The original ACM article proves the balance theorem:The run time of performing a sequence of m operations on a splay tree with n nodes is O( m(1 + ln(n)) + n ln(n) ).
Therefore the run time for a splay tree is comparable to any balanced tree assuming at least n operations
![Page 59: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/59.jpg)
59Splay trees
Performance
The ECE 250 web site has an implementation of splay trees athttp://ece.uwaterloo.ca/~dwharder/aads/Algorithms/Splay_trees/
It allows the user to export trees as SVG files
![Page 60: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/60.jpg)
60Splay trees
Comparisons
Advantages:– The amortized run times are similar to that of AVL trees and red-black
trees– The implementation is easier– No additional information (height/colour) is required
Disadvantages:– The tree will change with read-only operations
![Page 61: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/61.jpg)
61Splay trees
Summary
This topic covers splay trees– A binary search tree– Splay accessed or inserted nodes to the root– The height is O(n) but amortized run times of Q(ln(n)) for W(n) operations– Operations are termed zig, zig-zag and zig-zig– Requires no additional memory
![Page 62: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/62.jpg)
62Splay trees
References
[1] Weiss, Data Structures and Algorithm Analysis in C++, 3rd Ed., Addison Wesley, §4.5, pp.149-58.
[2] Daniel D. Sleator and Robert E. Tarjan, "Self-Adjusting Binary Search Trees", Journal of the ACM 32 (3), 1985, pp.652-86.
![Page 63: Splay trees](https://reader035.vdocument.in/reader035/viewer/2022062813/56816624550346895dd97d0f/html5/thumbnails/63.jpg)
63Splay trees
Usage Notes
• These slides are made publicly available on the web for anyone to use
• If you choose to use them, or a part thereof, for a course at another institution, I ask only three things:– that you inform me that you are using the slides,– that you acknowledge my work, and– that you alert me of any mistakes which I made or changes which you
make, and allow me the option of incorporating such changes (with an acknowledgment) in my set of slides
Sincerely,Douglas Wilhelm Harder, [email protected]