This repository was archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathsplay-tree.js
99 lines (86 loc) · 2.91 KB
/
splay-tree.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
module.exports =
class SplayTree {
splayNode (node) {
if (!node) return
while (true) {
if (this.isNodeLeftChild(this.getParent(node)) && this.isNodeRightChild(node)) { // zig-zag
this.rotateNodeLeft(node)
this.rotateNodeRight(node)
} else if (this.isNodeRightChild(this.getParent(node)) && this.isNodeLeftChild(node)) { // zig-zag
this.rotateNodeRight(node)
this.rotateNodeLeft(node)
} else if (this.isNodeLeftChild(this.getParent(node)) && this.isNodeLeftChild(node)) { // zig-zig
this.rotateNodeRight(this.getParent(node))
this.rotateNodeRight(node)
} else if (this.isNodeRightChild(this.getParent(node)) && this.isNodeRightChild(node)) { // zig-zig
this.rotateNodeLeft(this.getParent(node))
this.rotateNodeLeft(node)
} else { // zig
if (this.isNodeLeftChild(node)) {
this.rotateNodeRight(node)
} else if (this.isNodeRightChild(node)) {
this.rotateNodeLeft(node)
}
return
}
}
}
rotateNodeLeft (pivot) {
const root = this.getParent(pivot)
if (this.getParent(root)) {
if (root === this.getLeft(this.getParent(root))) {
this.setLeft(this.getParent(root), pivot)
} else {
this.setRight(this.getParent(root), pivot)
}
} else {
this.root = pivot
}
this.setParent(pivot, this.getParent(root))
this.setRight(root, this.getLeft(pivot))
if (this.getRight(root)) this.setParent(this.getRight(root), root)
this.setLeft(pivot, root)
this.setParent(this.getLeft(pivot), pivot)
this.updateSubtreeExtent(root)
this.updateSubtreeExtent(pivot)
}
rotateNodeRight (pivot) {
const root = this.getParent(pivot)
if (this.getParent(root)) {
if (root === this.getLeft(this.getParent(root))) {
this.setLeft(this.getParent(root), pivot)
} else {
this.setRight(this.getParent(root), pivot)
}
} else {
this.root = pivot
}
this.setParent(pivot, this.getParent(root))
this.setLeft(root, this.getRight(pivot))
if (this.getLeft(root)) this.setParent(this.getLeft(root), root)
this.setRight(pivot, root)
this.setParent(this.getRight(pivot), pivot)
this.updateSubtreeExtent(root)
this.updateSubtreeExtent(pivot)
}
isNodeLeftChild (node) {
return node != null && this.getParent(node) != null && this.getLeft(this.getParent(node)) === node
}
isNodeRightChild (node) {
return node != null && this.getParent(node) != null && this.getRight(this.getParent(node)) === node
}
getSuccessor (node) {
if (this.getRight(node)) {
node = this.getRight(node)
while (this.getLeft(node)) {
node = this.getLeft(node)
}
} else {
while (this.getParent(node) && this.getRight(this.getParent(node)) === node) {
node = this.getParent(node)
}
node = this.getParent(node)
}
return node
}
}