class Node {
constructor(val) {
if (!isNaN(val) && val != null) {
this.value = val;
} else {
throw "Error constructing Node instance: must pass a number to a Node";
}
this.left = null;
this.right = null;
}
add(val) {
if (this.value > val) {
if (this.left === null) {
this.left = new Node(val);
} else {
this.left.add(val);
}
}
if (this.value < val) {
if (this.right === null) {
this.right = new Node(val);
} else {
this.right.add(val);
}
}
}
preOrder() {
console.log(this.value);
if (this.left !== null) {
this.left.preOrder();
}
if (this.right !== null) {
this.right.preOrder();
}
}
inOrder() {
if (this.left !== null) {
this.left.inOrder();
}
console.log(this.value);
if (this.right !== null) {
this.right.inOrder();
}
}
postOrder() {
if (this.left !== null) {
this.left.postOrder();
}
if (this.right !== null) {
this.right.postOrder();
}
console.log(this.value);
}
}