deno.land / std@0.166.0 / collections / binary_search_tree.ts

binary_search_tree.ts
نووسراو ببینە
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
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license./** This module is browser compatible. */
import { ascend } from "./_comparators.ts";import { BinarySearchNode, Direction } from "./binary_search_node.ts";export * from "./_comparators.ts";
/** * An unbalanced binary search tree. The values are in ascending order by default, * using JavaScript's built in comparison operators to sort the values. */export class BinarySearchTree<T> implements Iterable<T> { protected root: BinarySearchNode<T> | null = null; protected _size = 0; constructor( protected compare: (a: T, b: T) => number = ascend, ) {}
/** Creates a new binary search tree from an array like or iterable object. */ static from<T>( collection: ArrayLike<T> | Iterable<T> | BinarySearchTree<T>, ): BinarySearchTree<T>; static from<T>( collection: ArrayLike<T> | Iterable<T> | BinarySearchTree<T>, options: { compare?: (a: T, b: T) => number; }, ): BinarySearchTree<T>; static from<T, U, V>( collection: ArrayLike<T> | Iterable<T> | BinarySearchTree<T>, options: { compare?: (a: U, b: U) => number; map: (value: T, index: number) => U; thisArg?: V; }, ): BinarySearchTree<U>; static from<T, U, V>( collection: ArrayLike<T> | Iterable<T> | BinarySearchTree<T>, options?: { compare?: (a: U, b: U) => number; map?: (value: T, index: number) => U; thisArg?: V; }, ): BinarySearchTree<U> { let result: BinarySearchTree<U>; let unmappedValues: ArrayLike<T> | Iterable<T> = []; if (collection instanceof BinarySearchTree) { result = new BinarySearchTree( options?.compare ?? (collection as unknown as BinarySearchTree<U>).compare, ); if (options?.compare || options?.map) { unmappedValues = collection; } else { const nodes: BinarySearchNode<U>[] = []; if (collection.root) { result.root = BinarySearchNode.from( collection.root as unknown as BinarySearchNode<U>, ); nodes.push(result.root); } while (nodes.length) { const node: BinarySearchNode<U> = nodes.pop()!; const left: BinarySearchNode<U> | null = node.left ? BinarySearchNode.from(node.left) : null; const right: BinarySearchNode<U> | null = node.right ? BinarySearchNode.from(node.right) : null;
if (left) { left.parent = node; nodes.push(left); } if (right) { right.parent = node; nodes.push(right); } } } } else { result = (options?.compare ? new BinarySearchTree(options.compare) : new BinarySearchTree()) as BinarySearchTree<U>; unmappedValues = collection; } const values: Iterable<U> = options?.map ? Array.from(unmappedValues, options.map, options.thisArg) : unmappedValues as U[]; for (const value of values) result.insert(value); return result; }
/** The amount of values stored in the binary search tree. */ get size(): number { return this._size; }
protected findNode(value: T): BinarySearchNode<T> | null { let node: BinarySearchNode<T> | null = this.root; while (node) { const order: number = this.compare(value as T, node.value); if (order === 0) break; const direction: "left" | "right" = order < 0 ? "left" : "right"; node = node[direction]; } return node; }
protected rotateNode(node: BinarySearchNode<T>, direction: Direction) { const replacementDirection: Direction = direction === "left" ? "right" : "left"; if (!node[replacementDirection]) { throw new TypeError( `cannot rotate ${direction} without ${replacementDirection} child`, ); } const replacement: BinarySearchNode<T> = node[replacementDirection]!; node[replacementDirection] = replacement[direction] ?? null; if (replacement[direction]) replacement[direction]!.parent = node; replacement.parent = node.parent; if (node.parent) { const parentDirection: Direction = node === node.parent[direction] ? direction : replacementDirection; node.parent[parentDirection] = replacement; } else { this.root = replacement; } replacement[direction] = node; node.parent = replacement; }
protected insertNode( Node: typeof BinarySearchNode, value: T, ): BinarySearchNode<T> | null { if (!this.root) { this.root = new Node(null, value); this._size++; return this.root; } else { let node: BinarySearchNode<T> = this.root; while (true) { const order: number = this.compare(value, node.value); if (order === 0) break; const direction: Direction = order < 0 ? "left" : "right"; if (node[direction]) { node = node[direction]!; } else { node[direction] = new Node(node, value); this._size++; return node[direction]; } } } return null; }
protected removeNode( value: T, ): BinarySearchNode<T> | null { let removeNode: BinarySearchNode<T> | null = this.findNode(value); if (removeNode) { const successorNode: BinarySearchNode<T> | null = !removeNode.left || !removeNode.right ? removeNode : removeNode.findSuccessorNode()!; const replacementNode: BinarySearchNode<T> | null = successorNode.left ?? successorNode.right; if (replacementNode) replacementNode.parent = successorNode.parent;
if (!successorNode.parent) { this.root = replacementNode; } else { successorNode.parent[successorNode.directionFromParent()!] = replacementNode; }
if (successorNode !== removeNode) { removeNode.value = successorNode.value; removeNode = successorNode; } this._size--; } return removeNode; }
/** * Adds the value to the binary search tree if it does not already exist in it. * Returns true if successful. */ insert(value: T): boolean { return !!this.insertNode(BinarySearchNode, value); }
/** * Removes node value from the binary search tree if found. * Returns true if found and removed. */ remove(value: T): boolean { return !!this.removeNode(value); }
/** Returns node value if found in the binary search tree. */ find(value: T): T | null { return this.findNode(value)?.value ?? null; }
/** Returns the minimum value in the binary search tree or null if empty. */ min(): T | null { return this.root ? this.root.findMinNode().value : null; }
/** Returns the maximum value in the binary search tree or null if empty. */ max(): T | null { return this.root ? this.root.findMaxNode().value : null; }
/** Removes all values from the binary search tree. */ clear() { this.root = null; this._size = 0; }
/** Checks if the binary search tree is empty. */ isEmpty(): boolean { return this.size === 0; }
/** * Returns an iterator that uses in-order (LNR) tree traversal for * retrieving values from the binary search tree. */ *lnrValues(): IterableIterator<T> { const nodes: BinarySearchNode<T>[] = []; let node: BinarySearchNode<T> | null = this.root; while (nodes.length || node) { if (node) { nodes.push(node); node = node.left; } else { node = nodes.pop()!; yield node.value; node = node.right; } } }
/** * Returns an iterator that uses reverse in-order (RNL) tree traversal for * retrieving values from the binary search tree. */ *rnlValues(): IterableIterator<T> { const nodes: BinarySearchNode<T>[] = []; let node: BinarySearchNode<T> | null = this.root; while (nodes.length || node) { if (node) { nodes.push(node); node = node.right; } else { node = nodes.pop()!; yield node.value; node = node.left; } } }
/** * Returns an iterator that uses pre-order (NLR) tree traversal for * retrieving values from the binary search tree. */ *nlrValues(): IterableIterator<T> { const nodes: BinarySearchNode<T>[] = []; if (this.root) nodes.push(this.root); while (nodes.length) { const node: BinarySearchNode<T> = nodes.pop()!; yield node.value; if (node.right) nodes.push(node.right); if (node.left) nodes.push(node.left); } }
/** * Returns an iterator that uses post-order (LRN) tree traversal for * retrieving values from the binary search tree. */ *lrnValues(): IterableIterator<T> { const nodes: BinarySearchNode<T>[] = []; let node: BinarySearchNode<T> | null = this.root; let lastNodeVisited: BinarySearchNode<T> | null = null; while (nodes.length || node) { if (node) { nodes.push(node); node = node.left; } else { const lastNode: BinarySearchNode<T> = nodes[nodes.length - 1]; if (lastNode.right && lastNode.right !== lastNodeVisited) { node = lastNode.right; } else { yield lastNode.value; lastNodeVisited = nodes.pop()!; } } } }
/** * Returns an iterator that uses level order tree traversal for * retrieving values from the binary search tree. */ *lvlValues(): IterableIterator<T> { const children: BinarySearchNode<T>[] = []; let cursor: BinarySearchNode<T> | null = this.root; while (cursor) { yield cursor.value; if (cursor.left) children.push(cursor.left); if (cursor.right) children.push(cursor.right); cursor = children.shift() ?? null; } }
/** * Returns an iterator that uses in-order (LNR) tree traversal for * retrieving values from the binary search tree. */ *[Symbol.iterator](): IterableIterator<T> { yield* this.lnrValues(); }}
std
Deno standard library
GitHub Repositorydenoland/deno_std
GitHub Stars
2458

Version Info

Tagged at
2 months ago