简体   繁体   中英

Optimization for Binary Search Tree 'remove' function

I just finished my first binary search tree remove function and it is in major need of optimization. I have spent a lot of time on this and this was the best I could manage. Is there a easier way to do this? Does anyone have any suggestions for optimization? To me, it just seems like it will necessarily be massive code.

For starters...

My Binary Search Tree...

function BST() {
    this.root = null;
}

My 'remove' function...

BST.prototype.remove = function(data) {
    if(this.root.data === data){
        var curr = this.root.left;
        while(true){
            if(curr.right.left === null && curr.right.right === null){
                this.root.data = curr.right.data;
                curr.right = null;
                break;
            }
            curr = curr.right;
        }
    }

    var curr = this.root;
    var found_data = this.find(data);
    if(found_data.left !== null && found_data.right !== null){
        var runner = found_data.right;
        var runner_prev = found_data;
        while(true){
            if(runner.left === null && runner.right === null){
                found_data.data = runner.data;
                if(runner_prev.left === runner){
                    runner_prev.left = null;
                }else{
                    runner_prev.right = null;
                }
                break;
            }
            runner_prev = runner;
            runner = runner.left;
        }
    }else if(found_data.left === null || found_data.right === null){
        var prev = this.prev(found_data.data);
        if(prev.right === found_data){
            if(found_data.left){
                prev.right = found_data.left;
            }else{
                prev.right = found_data.right;
            }
        }else{
            if(found_data.left){
                prev.left = found_data.left;
            }else{
                prev.left = found_data.right;
            }
        }
    }else{
        var prev = this.prev(found_data.data);
        if(prev.left === found_data){
            prev.left = null;
        }else{
            prev.right = null;
        }
    }

};

You will notice that I use supporting functions within my remove() function, such as prev() and find() They are apart of my overarching BST() function and can be used anywhere within is by prefacing it using this. .

Supporting functions I use within remove() ( prev() and find() )

BST.prototype.find = function(data) {
    if(this.root === null){
        return 'wrong';
    }

    var curr = this.root;
    while(true){
        if(data > curr.data){
            curr = curr.right;
        }else if(data < curr.data){
            curr = curr.left;
        }else{
            if(curr.data enter code here=== data){
                return curr;
            }else{
                return 'not here player'
            }
        }
    }
}

BST.prototype.prev = function(data){
    if(this.root === null){
        return false;
    }
    var prev = this.root;
    var curr = this.root;
    while(true){
        if(curr.left === null && curr.right === null){
            return prev;
        }
        if(data < curr.data){
            prev = curr;
            curr = curr.left;
        }else if(data > curr.data){
            prev = curr;
            curr = curr.right;
        }else{
            return prev;
        }
    }
}

This algorithm absolutely works, but as you can imagine, this is not the type of monster you would want to be answering a whiteboard interview question with.

It would be more efficient if you either:

  1. Combine prev() and find() by returning both the previous node and the found node from find()
  2. Give each node a parent pointer, and just follow it to find prev

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM