I am working on a Daily Coding Challenge to print the BST if given an in-ordered and pre-ordered array.
Below is my code in JavaScript ES6.
Please can you review it. This is my first fully-featured JS program so I am very unaware of standard syntax or shortcuts in JS
function Tree(){
this.value = null
this.right = null
this.left = null
this.reconstruct = (pre_order, in_order)=>{
if (pre_order){
if (this.value == null){
this.value = pre_order[0]
}
let node = pre_order[0]
let split_index = in_order.indexOf(node)
let left_tree = in_order.slice(0, split_index)
let right_tree = in_order.slice(split_index+1)
this.left = new Tree()
this.right = new Tree()
if (left_tree.length>1){
this.left.reconstruct(pre_order.slice(1, split_index), left_tree)
}else{
if (left_tree){
this.left.value = left_tree[0]
}
}
if (right_tree.length>1){
this.right.reconstruct(pre_order.slice(split_index+1), right_tree)
}else{
if (right_tree){
this.right.value = right_tree[0]
}
}
}
}
this.print_tree = (level) =>{
if (this.value){
console.log(" ".repeat(level), this.value)
if(this.left){
this.left.print_tree(level+1)
}
if(this.right){
this.right.print_tree(level+1)
}
}
}
}
in_order = [4, 3, 5, 2, 6, 1, 12, 8, 13, 7, 10, 9, 11]
pre_order = [1, 2, 3, 4, 5, 6, 7, 8, 12, 13, 9, 10, 11]
t = new Tree()
t.reconstruct(pre_order, in_order)
t.print_tree(0)