aboutsummaryrefslogtreecommitdiff
path: root/src/rbtree.ts
blob: 53667244c181f192c42f80d46502680c1530b342 (plain)
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/*
 * Copied from node_modules/bintrees/lib/, and adapted to use ES6, classes, and typescript.
 */

export class Node<T> {
	data: T;
	left: Node<T> | null;
	right: Node<T> | null;
	red: boolean;

	constructor(data: T){
		this.data = data;
		this.left = null;
		this.right = null;
		this.red = true;
	}

	get_child(dir: boolean){
		return dir ? this.right : this.left;
	}

	set_child(dir: boolean, val: Node<T> | null){
		if (dir) {
			this.right = val;
		} else {
			this.left = val;
		}
	}
}

export class Iterator<T> {
	_tree: RBTree<T>;
	_ancestors: Node<T>[];
	_cursor: Node<T> | null;

	constructor(tree: RBTree<T>){
		this._tree = tree;
		this._ancestors = [];
		this._cursor = null;
	}

	data(): T | null {
		return this._cursor !== null ? this._cursor.data : null;
	}

	// if null-iterator, returns first node
	// otherwise, returns next node
	next(): T | null{
		if (this._cursor === null) {
			const root = this._tree._root;
			if (root !== null) {
				this._minNode(root);
			}
		}
		else {
			if(this._cursor.right === null) {
				// no greater node in subtree, go up to parent
				// if coming from a right child, continue up the stack
				let save;
				do {
					save = this._cursor;
					if (this._ancestors.length) {
						this._cursor = this._ancestors.pop()!;
					}
					else {
						this._cursor = null;
						break;
					}
				} while (this._cursor.right === save);
			}
			else {
				// get the next node from the subtree
				this._ancestors.push(this._cursor);
				this._minNode(this._cursor.right);
			}
		}
		return this._cursor !== null ? this._cursor.data : null;
	}

	// if null-iterator, returns last node
	// otherwise, returns previous node
	prev(): T | null {
		if(this._cursor === null) {
			const root = this._tree._root;
			if(root !== null) {
				this._maxNode(root);
			}
		}
		else {
			if(this._cursor.left === null) {
				let save;
				do {
					save = this._cursor;
					if(this._ancestors.length) {
						this._cursor = this._ancestors.pop()!;
					}
					else {
						this._cursor = null;
						break;
					}
				} while(this._cursor.left === save);
			}
			else {
				this._ancestors.push(this._cursor);
				this._maxNode(this._cursor.left);
			}
		}
		return this._cursor !== null ? this._cursor.data : null;
	}

	_minNode(start: Node<T>) {
		while(start.left !== null) {
			this._ancestors.push(start);
			start = start.left;
		}
		this._cursor = start;
	}

	_maxNode(start: Node<T>) {
		while(start.right !== null) {
			this._ancestors.push(start);
			start = start.right;
		}
		this._cursor = start;
	}
}

export class RBTree<T> {
	_root: Node<T> | null;
	size: number;
	_comparator: (a: T, b: T) => number;

	constructor(comparator: (a: T, b: T) => number){
		this._root = null;
		this._comparator = comparator;
		this.size = 0;
	}

	// removes all nodes from the tree
	clear(){
		this._root = null;
		this.size = 0;
	}

	// returns node data if found, null otherwise
	find(data: T): T | null{
		let res = this._root;
		while (res !== null) {
			const c = this._comparator(data, res.data);
			if(c === 0) {
				return res.data;
			}
			else {
				res = res.get_child(c > 0);
			}
		}
		return null;
	}

	// returns iterator to node if found, null otherwise
	findIter(data: T): Iterator<T> | null {
		let res = this._root;
		const iter = this.iterator();
		while(res !== null) {
			const c = this._comparator(data, res.data);
			if(c === 0) {
				iter._cursor = res;
				return iter;
			}
			else {
				iter._ancestors.push(res);
				res = res.get_child(c > 0);
			}
		}
		return null;
	}

	// Returns an iterator to the tree node at or immediately after the item
	lowerBound(item: T): Iterator<T> {
		let cur = this._root;
		const iter = this.iterator();
		const cmp = this._comparator;
		while(cur !== null) {
			const c = cmp(item, cur.data);
			if(c === 0) {
				iter._cursor = cur;
				return iter;
			}
			iter._ancestors.push(cur);
			cur = cur.get_child(c > 0);
		}
		for(let i=iter._ancestors.length - 1; i >= 0; --i) {
			cur = iter._ancestors[i];
			if(cmp(item, cur.data) < 0) {
				iter._cursor = cur;
				iter._ancestors.length = i;
				return iter;
			}
		}
		iter._ancestors.length = 0;
		return iter;
	}

	// Returns an iterator to the tree node immediately after the item
	upperBound(item: T): Iterator<T> {
		const iter = this.lowerBound(item);
		const cmp = this._comparator;
		while(iter.data() !== null && cmp(iter.data()!, item) === 0) {
			iter.next();
		}
		return iter;
	}

	// returns null if tree is empty
	min(): T | null {
		let res = this._root;
		if(res === null) {
			return null;
		}
		while(res.left !== null) {
			res = res.left;
		}
		return res.data;
	}

	// returns null if tree is empty
	max(): T | null {
		let res = this._root;
		if(res === null) {
			return null;
		}
		while(res.right !== null) {
			res = res.right;
		}
		return res.data;
	}

	// returns a null iterator
	// call next() or prev() to point to an element
	iterator() {
		return new Iterator(this);
	}

	// calls cb on each node's data, in order
	each(cb: (x: T) => boolean) {
		const it = this.iterator();
		let data: T | null;
		while((data = it.next()) !== null) {
			if(cb(data) === false) {
				return;
			}
		}
	}

	// calls cb on each node's data, in reverse order
	reach(cb: (x: T) => boolean) {
		const it=this.iterator();
		let data: T | null;
		while((data = it.prev()) !== null) {
			if(cb(data) === false) {
				return;
			}
		}
	}

	// returns true if inserted, false if duplicate
	insert(data: T): boolean {
		let ret = false;
		if(this._root === null) {
			// empty tree
			this._root = new Node(data);
			ret = true;
			this.size++;
		}
		else {
			const head = new Node(undefined) as Node<T>; // fake tree root
			let dir = false;
			let last = false;
			// setup
			let gp: Node<T> | null = null; // grandparent
			let ggp = head; // grand-grand-parent
			let p: Node<T> | null = null; // parent
			let node: Node<T> | null = this._root;
			ggp.right = this._root;
			// search down
			while(true) {
				if(node === null) {
					// insert new node at the bottom
					node = new Node(data);
					p!.set_child(dir, node);
					ret = true;
					this.size++;
				}
				else if(is_red(node.left) && is_red(node.right)) {
					// color flip
					node.red = true;
					node.left!.red = false;
					node.right!.red = false;
				}
				// fix red violation
				if(is_red(node) && is_red(p)) {
					const dir2 = ggp.right === gp;
					if(node === p!.get_child(last)) {
						ggp.set_child(dir2, single_rotate(gp!, !last));
					}
					else {
						ggp.set_child(dir2, double_rotate(gp!, !last));
					}
				}
				const cmp = this._comparator(node.data, data);
				// stop if found
				if(cmp === 0) {
					break;
				}
				last = dir;
				dir = cmp < 0;
				// update helpers
				if(gp !== null) {
					ggp = gp;
				}
				gp = p;
				p = node;
				node = node.get_child(dir);
			}
			// update root
			this._root = head.right;
		}
		// make root black
		this._root!.red = false;
		return ret;
	}

	// returns true if removed, false if not found
	remove(data: T): boolean {
		if(this._root === null) {
			return false;
		}
		const head = new Node(undefined) as Node<T>; // fake tree root
		let node = head;
		node.right = this._root;
		let p: Node<T> | null = null; // parent
		let gp: Node<T> | null = null; // grand parent
		let found: Node<T> | null = null; // found item
		let dir = true;
		while(node.get_child(dir) !== null) {
			const last = dir;
			// update helpers
			gp = p;
			p = node;
			node = node.get_child(dir)!;
			const cmp = this._comparator(data, node.data);
			dir = cmp > 0;
			// save found node
			if (cmp === 0) {
				found = node;
			}
			// push the red node down
			if (!is_red(node) && !is_red(node.get_child(dir))) {
				if(is_red(node.get_child(!dir))) {
					const sr = single_rotate(node, dir);
					p.set_child(last, sr);
					p = sr;
				}
				else if(!is_red(node.get_child(!dir))) {
					const sibling = p.get_child(!last);
					if(sibling !== null) {
						if(!is_red(sibling.get_child(!last)) && !is_red(sibling.get_child(last))) {
							// color flip
							p.red = false;
							sibling.red = true;
							node.red = true;
						}
						else {
							const dir2 = gp!.right === p;
							if(is_red(sibling.get_child(last))) {
								gp!.set_child(dir2, double_rotate(p, last));
							}
							else if(is_red(sibling.get_child(!last))) {
								gp!.set_child(dir2, single_rotate(p, last));
							}
							// ensure correct coloring
							const gpc = gp!.get_child(dir2)!;
							gpc.red = true;
							node.red = true;
							gpc.left!.red = false;
							gpc.right!.red = false;
						}
					}
				}
			}
		}
		// replace and remove if found
		if (found !== null) {
			found.data = node.data;
			p!.set_child(p!.right === node, node.get_child(node.left === null));
			this.size--;
		}
		// update root and make it black
		this._root = head.right;
		if(this._root !== null) {
			this._root.red = false;
		}
		return found !== null;
	}
}

function is_red<T>(node: Node<T> | null): boolean {
	return node !== null && node.red;
}

function single_rotate<T>(root: Node<T>, dir: boolean): Node<T> {
	const save = root.get_child(!dir)!;
	root.set_child(!dir, save.get_child(dir));
	save.set_child(dir, root);
	root.red = true;
	save.red = false;
	return save;
}

function double_rotate<T>(root: Node<T>, dir: boolean): Node<T> {
	root.set_child(!dir, single_rotate(root.get_child(!dir)!, !dir));
	return single_rotate(root, dir);
}

export function rbtree_shallow_copy<T>(tree: RBTree<T>): RBTree<T> {
	const newTree = new RBTree(tree._comparator);
	newTree._root = tree._root;
	newTree.size = tree.size;
	return newTree;
}