You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Splay Tree/readme.md
+137-7
Original file line number
Diff line number
Diff line change
@@ -20,7 +20,7 @@ Given a node *a* if *a* is not the root, and *a* has a child *b*, and both *a* a
20
20
### Case both nodes left children
21
21

22
22
23
-
**IMPORTANT** is to note that a *ZigZig* performs first the rotation of the middle node with its parent (call it the grandparent) and later the rotation of the remaining node (grandchild). Doing that helps to keep the trees balanced even if it was first created by inserted a sequence of increasing values (see below worst case scenario).
23
+
**IMPORTANT** is to note that a *ZigZig* performs first the rotation of the middle node with its parent (call it the grandparent) and later the rotation of the remaining node (grandchild). Doing that helps to keep the trees balanced even if it was first created by inserted a sequence of increasing values (see below worst case scenario followed by an explanation about why ZigZig rotates first to the grandparent).
24
24
25
25
### Zig-Zag
26
26
@@ -93,28 +93,69 @@ During the applying phase, the algorithms determines which nodes are involved de
93
93
94
94
### Insertion
95
95
96
+
To insert a value:
97
+
98
+
- Insert it as in a binary search tree
99
+
- Splay the value to the root
100
+
96
101
### Deletion
97
102
103
+
To delete a value:
104
+
105
+
- Delete it as in a binary search tree
106
+
- Splay the parent of the removed node to the root
107
+
98
108
### Search
99
109
110
+
To search a value:
111
+
112
+
- Search for it as in a binary search tree
113
+
- Splay the node containing the value to the root
114
+
- If not found splay the node that would had been the parent of the searched value
115
+
116
+
### Minimum and maximum
117
+
118
+
- Search the tree for the required value
119
+
- Splay the node to the root
120
+
100
121
## Examples
101
122
102
123
### Example 1
103
124
104
-

125
+
Lets suppose a *find(20)* operation was performed and now the values **20** needs to be splayed to the root.
126
+
The sequence of steps will be the following:
127
+
105
128
106
-

129
+
1. Since we are in a *ZigZig* case, we need to rotate **9** to **4**
107
130
108
-

131
+

132
+
133
+
2. We got the following tree after the first rotation:
134
+
135
+

136
+
137
+
3. And finally the **20** is rotated to the **9**
138
+
139
+

109
140
110
141
111
142
### Example 2
112
143
113
-

144
+
Now suppose a *insert(7)* operation was performed and we're in a *ZigZag* case.
145
+
146
+
147
+
1. First **7** is rotated to **9**
148
+
149
+

150
+
151
+
2. And the result tree is:
152
+
153
+

114
154
115
-

155
+
3. Finally **7** is rotated to **4**
156
+
157
+

116
158
117
-

118
159
119
160
## Advantages
120
161
@@ -135,10 +176,99 @@ With *n* being the number of items in the tree.
135
176
136
177
# An example of the Worst Case Performance
137
178
179
+
Suppose the a sequence of consecutive values are inserted in an Splay Tree.
180
+
Let's take for example [1,2,3,4,5,6,7,8].
181
+
182
+
The tree construction will be like following:
183
+
184
+
185
+
1. Insert the number **1**
186
+
187
+
2. Insert **2**
188
+
189
+
190
+

191
+
192
+
193
+
3. Splay **2** to the root
194
+
195
+
196
+

197
+
198
+
199
+
4. Insert **3**
200
+
201
+
202
+

203
+
204
+
5. Splay **3** to the root
205
+
206
+
207
+

208
+
209
+
210
+
6. Insert **4**
211
+
212
+
213
+

214
+
215
+
216
+
7. After inserting the rest of the values the tree will look like this:
217
+
218
+
219
+

220
+
221
+
222
+
223
+
If we keep insert number following the same sequence, that tree becomes inbalanced and have a height of **n** with **n** being the numbers of values inserted.
224
+
After getting this tree, a *find(1)* operation for example will take **O(n)**
225
+
226
+
## ZigZig rotation order: first grandparent
227
+
228
+
But thanks to the properties of the **Splay Tree** and the *ZigZig* rotations after the *find(1)* operation the tree becomes balanced again. This only happens if we respect the order of the *ZigZig* rotation, and the rotation to the grandparent happens first.
229
+
230
+
The sequence of *ZigZigs* rotations will look like follows:
231
+
232
+
1. Rotate **2** to **3**
233
+
234
+

235
+
236
+
2. Rotate **1** to **2**
237
+
238
+

239
+
240
+
3. Rotate **4** to **5**
241
+
242
+

243
+
244
+
4. Rotate **1** to **4**
245
+
246
+

247
+
248
+
5. Finally after splaying **1** to the root the tree will look like this:
249
+
250
+

251
+
252
+
253
+
Based on the example above, we can see why it's important to rotate first to the grandparent. We got a tree of height = 6, from an initial tree of height = 8. If the tree would had been taller, we would have achieved almost half of the initial height after the splaying operation.
254
+
255
+
## ZigZig wrong rotation order
256
+
257
+
If the rotations would had been taking first the parent and not the grandparent we would have finished with the following, yet unbalanced tree, just inverting the side of the elements.
258
+
259
+

260
+
138
261
139
262
## See also
140
263
141
264
[Splay Tree on Wikipedia](https://en.wikipedia.org/wiki/Splay_tree)
265
+
142
266
[Splay Tree by University of California in Berkeley - CS 61B Lecture 34](https://www.youtube.com/watch?v=G5QIXywcJlY)
143
267
268
+
269
+
----------------
270
+
144
271
*Written for Swift Algorithm Club by Barbara Martina Rodeker*
0 commit comments