Skip to content

Commit bedaaac

Browse files
committed
Updated readme for splay trees for Swift 4.
1 parent 643828a commit bedaaac

File tree

1 file changed

+28
-28
lines changed

1 file changed

+28
-28
lines changed

Splay Tree/readme.md

+28-28
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# 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**.
33

44

55
## 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
2424

2525
### Zig-Zag
2626

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.
2828

2929
### Case right - left
3030
![ZigZagCase1](Images/zigzag1.png)
@@ -43,49 +43,49 @@ A **Zig** is performed when the node *a* to be rotated has the root as parent.
4343

4444
## Splaying
4545

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.
4747

4848
```
4949
while (node.parent != nil) {
50-
operation(forNode: node).apply(onNode: node)
50+
operation(forNode: node).apply(onNode: node)
5151
}
5252
```
5353

5454
Where operation returns the required rotation to be applied.
5555

5656
```
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
6462
}
65-
return .zig
63+
return .zigZig
6664
}
65+
return .zig
66+
}
6767
```
6868

6969
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.
7070

7171
```
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")
81-
rotate(child: node.parent!, parent: node.parent!.parent!)
82-
rotate(child: node, parent: node.parent!)
83-
84-
case .zig:
85-
assert(node.parent != nil && node.parent!.parent == nil, "There should be a parent which is the root")
86-
rotate(child: node, parent: node.parent!)
87-
}
72+
public func apply<T>(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")
81+
rotate(child: node.parent!, parent: node.parent!.parent!)
82+
rotate(child: node, parent: node.parent!)
83+
84+
case .zig:
85+
assert(node.parent != nil && node.parent!.parent == nil, "There should be a parent which is the root")
86+
rotate(child: node, parent: node.parent!)
8887
}
88+
}
8989
```
9090

9191

0 commit comments

Comments
 (0)