Skip to content

Commit 72b6ef6

Browse files
Finishing documentation
1 parent b04ecf9 commit 72b6ef6

File tree

1 file changed

+137
-7
lines changed

1 file changed

+137
-7
lines changed

Splay Tree/readme.md

+137-7
Original file line numberDiff line numberDiff 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
2020
### Case both nodes left children
2121
![ZigZigCase2](Images/zigzig2.png)
2222

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).
2424

2525
### Zig-Zag
2626

@@ -93,28 +93,69 @@ During the applying phase, the algorithms determines which nodes are involved de
9393

9494
### Insertion
9595

96+
To insert a value:
97+
98+
- Insert it as in a binary search tree
99+
- Splay the value to the root
100+
96101
### Deletion
97102

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+
98108
### Search
99109

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+
100121
## Examples
101122

102123
### Example 1
103124

104-
![ZigEx1](Images/examplezigzig1.png)
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+
105128

106-
![ZigEx2](Images/examplezigzig2.png)
129+
1. Since we are in a *ZigZig* case, we need to rotate **9** to **4**
107130

108-
![ZigEx3](Images/examplezigzig3.png)
131+
![ZiggEx1](Images/examplezigzig1.png)
132+
133+
2. We got the following tree after the first rotation:
134+
135+
![ZiggEx2](Images/examplezigzig2.png)
136+
137+
3. And finally the **20** is rotated to the **9**
138+
139+
![ZiggEx3](Images/examplezigzig3.png)
109140

110141

111142
### Example 2
112143

113-
![ZigEx21](Images/example1-1.png)
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+
![ZigggEx21](Images/example1-1.png)
150+
151+
2. And the result tree is:
152+
153+
![ZigggEx22](Images/example1-2.png)
114154

115-
![ZigEx22](Images/example1-2.png)
155+
3. Finally **7** is rotated to **4**
156+
157+
![ZigggEx23](Images/example1-3.png)
116158

117-
![ZigEx23](Images/example1-3.png)
118159

119160
## Advantages
120161

@@ -135,10 +176,99 @@ With *n* being the number of items in the tree.
135176

136177
# An example of the Worst Case Performance
137178

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+
![WorstCase1](Images/worst-case-1.png)
191+
192+
193+
3. Splay **2** to the root
194+
195+
196+
![WorstCase2](Images/worst-case-2.png)
197+
198+
199+
4. Insert **3**
200+
201+
202+
![WorstCase3](Images/worst-case-3.png)
203+
204+
5. Splay **3** to the root
205+
206+
207+
![WorstCase4](Images/worst-case-4.png)
208+
209+
210+
6. Insert **4**
211+
212+
213+
![WorstCase5](Images/worst-case-5.png)
214+
215+
216+
7. After inserting the rest of the values the tree will look like this:
217+
218+
219+
![WorstCase6](Images/worst-case-6.png)
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+
![ZigZig1](Images/example-zigzig-1.png)
235+
236+
2. Rotate **1** to **2**
237+
238+
![ZigZig2](Images/example-zigzig-2.png)
239+
240+
3. Rotate **4** to **5**
241+
242+
![ZigZig3](Images/example-zigzig-3.png)
243+
244+
4. Rotate **1** to **4**
245+
246+
![ZigZig4](Images/example-zigzig-4.png)
247+
248+
5. Finally after splaying **1** to the root the tree will look like this:
249+
250+
![ZigZig5](Images/example-zigzig-5.png)
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+
![ZigZigWrong](Images/zigzig-wrongrotated.png)
260+
138261

139262
## See also
140263

141264
[Splay Tree on Wikipedia](https://en.wikipedia.org/wiki/Splay_tree)
265+
142266
[Splay Tree by University of California in Berkeley - CS 61B Lecture 34](https://www.youtube.com/watch?v=G5QIXywcJlY)
143267

268+
269+
----------------
270+
144271
*Written for Swift Algorithm Club by Barbara Martina Rodeker*
272+
273+
----------------
274+

0 commit comments

Comments
 (0)