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
+28-28
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,5 @@
1
1
# Splay Tree
2
-
Splay tree is a data structure, structurally identitical to a Balanced Binary Search Tree. Every operation performed on a Splay Tree causes a readjustment in order to provide fast access to recently operated values. On every access, the tree is rearranged and the node accessed is moved to the root of the tree using a set of specific rotations, which together are referred to as **Splaying**.
2
+
Splay tree is a data structure, structurally identitical to a balanced binary search tree. Every operation performed on a Splay Tree causes a readjustment in order to provide fast access to recently operated values. On every access, the tree is rearranged and the node accessed is moved to the root of the tree using a set of specific rotations, which together are referred to as **Splaying**.
3
3
4
4
5
5
## Rotations
@@ -24,7 +24,7 @@ Given a node *a* if *a* is not the root, and *a* has a child *b*, and both *a* a
24
24
25
25
### Zig-Zag
26
26
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.
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 opposite), a **Zig-Zag** is performed.
28
28
29
29
### Case right - left
30
30

@@ -43,49 +43,49 @@ A **Zig** is performed when the node *a* to be rotated has the root as parent.
43
43
44
44
## Splaying
45
45
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.
46
+
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
47
48
48
```
49
49
while (node.parent != nil) {
50
-
operation(forNode: node).apply(onNode: node)
50
+
operation(forNode: node).apply(onNode: node)
51
51
}
52
52
```
53
53
54
54
Where operation returns the required rotation to be applied.
55
55
56
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
57
+
public static func operation<T>(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
64
62
}
65
-
return .zig
63
+
return .zigZig
66
64
}
65
+
return .zig
66
+
}
67
67
```
68
68
69
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
70
71
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