lazzy.ts
Version:
Fast and lightweight library for lazy operations with iterable objects.
44 lines • 1.44 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BinaryTree = void 0;
class BinaryTree {
constructor(root) {
this.root = root;
}
add(node, ...comparer) {
if (comparer[0] != null) {
this.recursiveAdd(node, this.root, comparer[0]);
}
else if (typeof node.value === "number" || typeof node.value === "boolean") {
const numericComparer = (left, right) => left - right;
this.recursiveAdd(node, this.root, numericComparer);
}
else if (typeof node.value === "string") {
const stringComparer = (left, right) => left.localeCompare(right);
this.recursiveAdd(node, this.root, stringComparer);
}
else {
throw new TypeError("You must pass a comparer function!");
}
}
recursiveAdd(node, parent, comparer) {
if (comparer(node.value, parent.value) < 0) {
if (parent.left != null) {
this.recursiveAdd(node, parent.left, comparer);
}
else {
parent.left = node;
}
}
else {
if (parent.right != null) {
this.recursiveAdd(node, parent.right, comparer);
}
else {
parent.right = node;
}
}
}
}
exports.BinaryTree = BinaryTree;
//# sourceMappingURL=binaryTree.js.map