blakeembrey / code-problems

Common code and interview problems solved in multiple languages

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

is the refactor of the binary-search-tree.js ok?

caoglish opened this issue · comments

I learned binary-search-tree.js today. I found a duplicate code which is very obvious.

https://github.com/blakeembrey/code-problems/blob/master/solutions/javascript/binary-search-tree.js

if we refactored in this way:

doSubNode = function(direct, num) {
    if (this[direct] === undefined) {
        this[direct] = createNode(num);
    } else {
        this[direct].add(num);
    }
};

and used it in:

...
        if (num < this.value) {
            doSubNode.apply(this,["left", num]);
        } else if (num > this.value) {
            doSubNode.apply(this,["right", num]);
        } 
...

So is any reason to use the duplicated code? or it's OK to refactor the code?

I add this code in #103

@caoglish I would prefer the doSubNode to not use apply, but let's continue the discussion in the PR.