Skip to content

Commit b04ecf9

Browse files
author
barbara
committed
Merge branch 'splay-tree' of github.com:barbaramartina/swift-algorithm-club into splay-tree
2 parents fbd5d6e + c426880 commit b04ecf9

File tree

1 file changed

+87
-3
lines changed

1 file changed

+87
-3
lines changed

Splay Tree/readme.md

+87-3
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,92 @@ 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
18+
![ZigZigCase1](Images/zigzig1.png)
19+
20+
### Case both nodes left children
21+
![ZigZigCase2](Images/zigzig2.png)
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+
1125
### Zig-Zag
1226

1327
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.
1428

29+
### Case right - left
30+
![ZigZagCase1](Images/zigzag1.png)
31+
32+
### Case left - right
33+
![ZigZagCase2](Images/zigzag2.png)
34+
35+
**IMPORTANT** A *ZigZag* performs first the rotation of the grandchild node and later the same node with its new parent again.
36+
1537
### Zig
1638

1739
A **Zig** is performed when the node *a* to be rotated has the root as parent.
1840

41+
![ZigCase](Images/zig.png)
42+
43+
1944
## Splaying
2045

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")
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
2293

2394
### Insertion
2495

@@ -30,9 +101,20 @@ A **Zig** is performed when the node *a* to be rotated has the root as parent.
30101

31102
### Example 1
32103

104+
![ZigEx1](Images/examplezigzig1.png)
105+
106+
![ZigEx2](Images/examplezigzig2.png)
107+
108+
![ZigEx3](Images/examplezigzig3.png)
109+
110+
33111
### Example 2
34112

35-
### Example 3
113+
![ZigEx21](Images/example1-1.png)
114+
115+
![ZigEx22](Images/example1-2.png)
116+
117+
![ZigEx23](Images/example1-3.png)
36118

37119
## Advantages
38120

@@ -51,10 +133,12 @@ Splay tree are not perfectly balanced always, so in case of accessing all the el
51133

52134
With *n* being the number of items in the tree.
53135

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

55139
## See also
56140

57141
[Splay Tree on Wikipedia](https://en.wikipedia.org/wiki/Splay_tree)
58142
[Splay Tree by University of California in Berkeley - CS 61B Lecture 34](https://www.youtube.com/watch?v=G5QIXywcJlY)
59143

60-
*Written for Swift Algorithm Club by Mike Taghavi and Matthijs Hollemans*
144+
*Written for Swift Algorithm Club by Barbara Martina Rodeker*

0 commit comments

Comments
 (0)