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
+63-1
Original file line number
Diff line number
Diff line change
@@ -4,22 +4,36 @@ 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
11
18

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

14
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
+
15
25
### Zig-Zag
16
26
17
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.
18
28
29
+
### Case right - left
19
30

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

22
34
35
+
**IMPORTANT** A *ZigZag* performs first the rotation of the grandchild node and later the same node with its new parent again.
36
+
23
37
### Zig
24
38
25
39
A **Zig** is performed when the node *a* to be rotated has the root as parent.
@@ -29,7 +43,53 @@ A **Zig** is performed when the node *a* to be rotated has the root as parent.
29
43
30
44
## Splaying
31
45
32
-
## 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