@coderbaba/stl-js
Version:
STL data structures for JavaScript
71 lines (59 loc) • 1.47 kB
text/typescript
class TreeNode<T> {
value: T;
left: TreeNode<T> | null = null;
right: TreeNode<T> | null = null;
constructor(value: T) {
this.value = value;
}
}
class BinaryTree<T> {
private root: TreeNode<T> | null = null;
insert(value: T): void {
const newNode = new TreeNode < T > (value);
if (!this.root) {
this.root = newNode;
return;
}
const queue: TreeNode<T>[] = [this.root];
while (queue.length) {
const node = queue.shift()!;
if (!node.left) {
node.left = newNode;
return;
} else {
queue.push(node.left);
}
if (!node.right) {
node.right = newNode;
return;
} else {
queue.push(node.right);
}
}
}
inOrder(node: TreeNode<T> | null = this.root, result: T[] = []): T[] {
if (node) {
this.inOrder(node.left, result);
result.push(node.value);
this.inOrder(node.right, result);
}
return result;
}
preOrder(node: TreeNode<T> | null = this.root, result: T[] = []): T[] {
if (node) {
result.push(node.value);
this.preOrder(node.left, result);
this.preOrder(node.right, result);
}
return result;
}
postOrder(node: TreeNode<T> | null = this.root, result: T[] = []): T[] {
if (node) {
this.postOrder(node.left, result);
this.postOrder(node.right, result);
result.push(node.value);
}
return result;
}
}
export default BinaryTree;