4
\$\begingroup\$

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)
\$\endgroup\$
2
\$\begingroup\$

A few key points:

  • Make sure to put in semicolons!
  • You don't need to export properties (i.e do this.myVar) if you're not going to access them - this can lead to potentially slowing down recurrence, or in some cases, security vulnerabilities.
  • If you're going to use arrow functions, keep it consistent - do const Tree = () => { on the first line

Some more specific things about the actual functionality/logic:

  • By tracking this.level, you could simplify all required actions into an array by simply pushing their position in the tree. If this.level === 0, you know you are at the top of the tree, and you know to end recurrence.
  • Using a position-based tracking lets you make it asyncronous, and print the result at the end. Remember that in JS, console.logs take a lot of time, so you want to avoid it as much as possible.
  • This would let you essentially JSONify the tree into a bunch of function references, which, when called, would populate themselves. Giving the option to return this would make the class more integratable into other things, where a console output is not necessarily the most useful thing. Further, it would let you cut out the repetition of if (left_tree.length>1){, which is not DRY.
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.