2023-09-01 05:17:06 +02:00
|
|
|
// https://www.freecodecamp.org/learn/coding-interview-prep/data-structures/use-depth-first-search-in-a-binary-search-tree
|
|
|
|
|
|
|
|
var displayTree = (tree) => console.log(JSON.stringify(tree, null, 2));
|
|
|
|
function Node(value) {
|
|
|
|
this.value = value;
|
|
|
|
this.left = null;
|
|
|
|
this.right = null;
|
|
|
|
}
|
|
|
|
function BinarySearchTree() {
|
|
|
|
this.root = null;
|
|
|
|
// Only change code below this line
|
|
|
|
this.inorder = () => {
|
|
|
|
if (this.root === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const values = [];
|
2023-09-01 10:17:58 +02:00
|
|
|
const stack = [{ node: this.root, stage: "left" }];
|
|
|
|
while (stack.length) {
|
|
|
|
const { node, stage } = stack[stack.length - 1];
|
|
|
|
if (stage === "left") {
|
|
|
|
stack[stack.length - 1].stage = "node";
|
|
|
|
if (node.left !== null) {
|
|
|
|
stack.push({ node: node.left, stage: "left" });
|
|
|
|
}
|
|
|
|
} else if (stage === "node") {
|
|
|
|
stack[stack.length - 1].stage = "right";
|
|
|
|
values.push(node.value);
|
|
|
|
} else if (stage === "right") {
|
|
|
|
stack.pop();
|
|
|
|
if (node.right !== null) {
|
|
|
|
stack.push({ node: node.right, stage: "left" });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-09-01 05:17:06 +02:00
|
|
|
return values;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.preorder = () => {
|
|
|
|
if (this.root === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const values = [];
|
2023-09-01 10:17:58 +02:00
|
|
|
const stack = [{ node: this.root, stage: "node" }];
|
|
|
|
while (stack.length) {
|
|
|
|
const { node, stage } = stack[stack.length - 1];
|
|
|
|
if (stage === "node") {
|
|
|
|
stack[stack.length - 1].stage = "left";
|
|
|
|
values.push(node.value);
|
|
|
|
} else if (stage === "left") {
|
|
|
|
stack[stack.length - 1].stage = "right";
|
|
|
|
if (node.left !== null) {
|
|
|
|
stack.push({ node: node.left, stage: "node" });
|
|
|
|
}
|
|
|
|
} else if (stage === "right") {
|
|
|
|
stack.pop();
|
|
|
|
if (node.right !== null) {
|
|
|
|
stack.push({ node: node.right, stage: "node" });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-09-01 05:17:06 +02:00
|
|
|
return values;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.postorder = () => {
|
|
|
|
if (this.root === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const values = [];
|
2023-09-01 10:17:58 +02:00
|
|
|
const stack = [{ node: this.root, stage: "left" }];
|
|
|
|
while (stack.length) {
|
|
|
|
const { node, stage } = stack[stack.length - 1];
|
|
|
|
if (stage === "left") {
|
|
|
|
stack[stack.length - 1].stage = "right";
|
|
|
|
if (node.left !== null) {
|
|
|
|
stack.push({ node: node.left, stage: "left" });
|
|
|
|
}
|
|
|
|
} else if (stage === "right") {
|
|
|
|
stack[stack.length - 1].stage = "node";
|
|
|
|
if (node.right !== null) {
|
|
|
|
stack.push({ node: node.right, stage: "left" });
|
|
|
|
}
|
|
|
|
} else if (stage === "node") {
|
|
|
|
stack.pop();
|
|
|
|
values.push(node.value);
|
|
|
|
}
|
|
|
|
}
|
2023-09-01 05:17:06 +02:00
|
|
|
return values;
|
|
|
|
};
|
|
|
|
// Only change code above this line
|
|
|
|
}
|