Skip to content

Commit c426880

Browse files
Update readme.md
1 parent 5d15f55 commit c426880

File tree

1 file changed

+63
-1
lines changed

1 file changed

+63
-1
lines changed

Splay Tree/readme.md

+63-1
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,36 @@ Splay tree is a data structure, structurally identitical to a Balanced Binary Se
44

55
## Rotations
66

7+
There are 3 types of rotations that can form an **Splaying**:
8+
9+
- ZigZig
10+
- ZigZag
11+
- Zig
12+
713
### Zig-Zig
814

915
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.
1016

17+
### Case both nodes right children
1118
![ZigZigCase1](Images/zigzig1.png)
1219

20+
### Case both nodes left children
1321
![ZigZigCase2](Images/zigzig2.png)
1422

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+
1525
### Zig-Zag
1626

1727
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.
1828

29+
### Case right - left
1930
![ZigZagCase1](Images/zigzag1.png)
2031

32+
### Case left - right
2133
![ZigZagCase2](Images/zigzag2.png)
2234

35+
**IMPORTANT** A *ZigZag* performs first the rotation of the grandchild node and later the same node with its new parent again.
36+
2337
### Zig
2438

2539
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.
2943

3044
## Splaying
3145

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")
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+
}
88+
}
89+
```
90+
91+
92+
## Operations on an Splay Tree
3393

3494
### Insertion
3595

@@ -73,6 +133,8 @@ Splay tree are not perfectly balanced always, so in case of accessing all the el
73133

74134
With *n* being the number of items in the tree.
75135

136+
# An example of the Worst Case Performance
137+
76138

77139
## See also
78140

0 commit comments

Comments
 (0)