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
+87-3
Original file line number
Diff line number
Diff line change
@@ -4,21 +4,92 @@ Splay tree is a data structure, structurally identitical to a Balanced Binary Se
4
4
5
5
## Rotations
6
6
7
+
There are 3 types of rotations that can form an **Splaying**:
8
+
9
+
- ZigZig
10
+
- ZigZag
11
+
- Zig
12
+
7
13
### Zig-Zig
8
14
9
15
Given a node *a* if *a* is not the root, and *a* has a child *b*, and both *a* and *b* are left children or right children, a **Zig-Zig** is performed.
10
16
17
+
### Case both nodes right children
18
+

19
+
20
+
### Case both nodes left children
21
+

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).
24
+
11
25
### Zig-Zag
12
26
13
27
Given a node *a* if *a* is not the root, and *a* has a child *b*, and *b* is the left child of *a* being the right child (or the opporsite), a **Zig-Zag** is performed.
14
28
29
+
### Case right - left
30
+

31
+
32
+
### Case left - right
33
+

34
+
35
+
**IMPORTANT** A *ZigZag* performs first the rotation of the grandchild node and later the same node with its new parent again.
36
+
15
37
### Zig
16
38
17
39
A **Zig** is performed when the node *a* to be rotated has the root as parent.
18
40
41
+

42
+
43
+
19
44
## Splaying
20
45
21
-
## Operations
46
+
A splaying consists in making so many rotations as needed until the node affected by the operation is at the top and becomes the root of the tree.
47
+
48
+
```
49
+
while (node.parent != nil) {
50
+
operation(forNode: node).apply(onNode: node)
51
+
}
52
+
```
53
+
54
+
Where operation returns the required rotation to be applied.
55
+
56
+
```
57
+
public static func operation<T: Comparable>(forNode node: Node<T>) -> SplayOperation {
58
+
59
+
if let parent = node.parent, let _ = parent.parent {
60
+
if (node.isLeftChild && parent.isRightChild) || (node.isRightChild && parent.isLeftChild) {
61
+
return .zigZag
62
+
}
63
+
return .zigZig
64
+
}
65
+
return .zig
66
+
}
67
+
```
68
+
69
+
During the applying phase, the algorithms determines which nodes are involved depending on the rotation to be applied and proceeding to re-arrange the node with its parent.
70
+
71
+
```
72
+
public func apply<T: Comparable>(onNode node: Node<T>) {
73
+
switch self {
74
+
case .zigZag:
75
+
assert(node.parent != nil && node.parent!.parent != nil, "Should be at least 2 nodes up in the tree")
76
+
rotate(child: node, parent: node.parent!)
77
+
rotate(child: node, parent: node.parent!)
78
+
79
+
case .zigZig:
80
+
assert(node.parent != nil && node.parent!.parent != nil, "Should be at least 2 nodes up in the tree")
0 commit comments