-
Notifications
You must be signed in to change notification settings - Fork 8
/
llrb.js
168 lines (130 loc) · 3.63 KB
/
llrb.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
'use strict';
module.exports = llrb;
function llrb(compare) {
return new LLRBTree(compare);
}
function Node(key, value, red, left, right) {
this.key = key;
this.value = value;
this.red = red;
this.left = left;
this.right = right;
}
var bottom = new Node(null, null, false);
bottom.left = bottom;
bottom.right = bottom;
function LLRBTree(compare) {
this.compare = compare || defaultCompare;
this.root = bottom;
}
LLRBTree.prototype = {
find: function (key) {
var n = this.root,
cmp = this.compare;
while (n !== bottom) {
var c = cmp(key, n.key);
if (c === 0) return n;
n = c < 0 ? n.left : n.right;
}
return null;
},
insert: function (key, value) {
this.root = insert(this.root, key, value, this.compare);
this.root.red = false;
},
removeMin: function () {
var root = this.root;
if (root === bottom) return;
if (!root.left.red && !root.right.red) root.red = true;
root = this.root = removeMin(root);
root.red = false;
},
remove: function (key) {
if (!this.find(key)) return;
var root = this.root;
if (!root.left.red && !root.right.red) root.red = true;
root = this.root = remove(root, key, this.compare);
root.red = false;
}
};
function defaultCompare(a, b) {
return a < b ? -1 : a > b ? 1 : 0;
}
function insert(h, key, value, compare) {
if (h === bottom) return new Node(key, value, true, bottom, bottom);
var c = compare(key, h.key);
if (c < 0) h.left = insert(h.left, key, value, compare);
else if (c > 0) h.right = insert(h.right, key, value, compare);
else h.value = value;
if (h.right.red && !h.left.red) h = rotateLeft(h);
if (h.left.red && h.left.left.red) h = rotateRight(h);
if (h.left.red && h.right.red) flipColors(h);
return h;
}
function removeMin(h) {
if (h.left === bottom) return bottom;
if (!h.left.red && !h.left.left.red) h = moveRedLeft(h);
h.left = removeMin(h.left);
return balance(h);
}
function remove(h, key, compare) {
if (compare(key, h.key) < 0) {
if (!h.left.red && !h.left.left.red) h = moveRedLeft(h);
h.left = remove(h.left, key, compare);
} else {
if (h.left.red) h = rotateRight(h);
if (compare(key, h.key) === 0 && (h.right === bottom)) return bottom;
if (!h.right.red && !h.right.left.red) h = moveRedRight(h);
if (compare(key, h.key) === 0) {
var x = min(h.right);
h.key = x.key;
h.val = x.val;
h.right = removeMin(h.right);
} else h.right = remove(h.right, key, compare);
}
return balance(h);
}
function min(x) {
if (x.left === bottom) return x;
else return min(x.left);
}
function rotateRight(h) {
var x = h.left;
h.left = x.right;
x.right = h;
x.red = h.red;
h.red = true;
return x;
}
function rotateLeft(h) {
var x = h.right;
h.right = x.left;
x.left = h;
x.red = h.red;
h.red = true;
return x;
}
function flipColors(h) {
h.red = !h.red;
h.left.red = !h.left.red;
h.right.red = !h.right.red;
}
function moveRedLeft(h) {
flipColors(h);
if (h.right.left.red) {
h.right = rotateRight(h.right);
h = rotateLeft(h);
}
return h;
}
function moveRedRight(h) {
flipColors(h);
if (h.left.left.red) h = rotateRight(h);
return h;
}
function balance(h) {
if (h.right.red) h = rotateLeft(h);
if (h.left.red && h.left.left.red) h = rotateRight(h);
if (h.left.red && h.right.red) flipColors(h);
return h;
}