mirror of
https://github.com/voltrevo/ValueScript.git
synced 2026-01-13 07:28:03 -05:00
37 lines
683 B
TypeScript
37 lines
683 B
TypeScript
import type NotNullish from "./NotNullish.ts";
|
|
|
|
export default class BinaryTree<T extends NotNullish> {
|
|
left?: BinaryTree<T>;
|
|
value?: T;
|
|
right?: BinaryTree<T>;
|
|
|
|
insert(newValue: T) {
|
|
if (this.value === undefined) {
|
|
this.value = newValue;
|
|
return;
|
|
}
|
|
|
|
if (newValue < this.value) {
|
|
this.left ??= new BinaryTree();
|
|
this.left.insert(newValue);
|
|
} else {
|
|
this.right ??= new BinaryTree();
|
|
this.right.insert(newValue);
|
|
}
|
|
}
|
|
|
|
*[Symbol.iterator](): Generator<T> {
|
|
if (this.left) {
|
|
yield* this.left;
|
|
}
|
|
|
|
if (this.value) {
|
|
yield this.value;
|
|
}
|
|
|
|
if (this.right) {
|
|
yield* this.right;
|
|
}
|
|
}
|
|
}
|