Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 二叉搜索树的优化&x27;拆下';功能_Javascript_Algorithm_Data Structures_Binary Search Tree - Fatal编程技术网

Javascript 二叉搜索树的优化&x27;拆下';功能

Javascript 二叉搜索树的优化&x27;拆下';功能,javascript,algorithm,data-structures,binary-search-tree,Javascript,Algorithm,Data Structures,Binary Search Tree,我刚刚完成了我的第一个二叉搜索树remove函数,它主要需要优化。我在这上面花了很多时间,这是我能做到的最好的了。有没有更简单的方法?有人对优化有什么建议吗?对我来说,这似乎必然是大量的代码 首先 我的二进制搜索树… 我的“删除”功能… 您会注意到,我在我的remove()函数中使用了支持函数,例如prev()和find()它们是我的首要BST()函数的一部分,可以通过使用this.在is中的任何位置使用 支持我在remove()中使用的函数(prev()和find()) BST.prototy

我刚刚完成了我的第一个二叉搜索树
remove
函数,它主要需要优化。我在这上面花了很多时间,这是我能做到的最好的了。有没有更简单的方法?有人对优化有什么建议吗?对我来说,这似乎必然是大量的代码

首先

我的二进制搜索树…

我的“删除”功能…

您会注意到,我在我的
remove()
函数中使用了支持函数,例如
prev()
find()
它们是我的首要
BST()
函数的一部分,可以通过使用
this.
在is中的任何位置使用

支持我在
remove()
中使用的函数(
prev()
find()

BST.prototype.find=函数(数据){
if(this.root==null){
返回“错误”;
}
var curr=this.root;
while(true){
如果(数据>当前数据){
curr=curr.right;
}否则如果(数据<当前数据){
当前=当前左侧;
}否则{
如果(当前数据在此处输入代码==数据){
返回货币;
}否则{
返回“非本地玩家”
}
}
}
}
BST.prototype.prev=函数(数据){
if(this.root==null){
返回false;
}
var prev=this.root;
var curr=this.root;
while(true){
if(curr.left==null&&curr.right==null){
返回上一个;
}
if(数据<当前数据){
上一次=当前;
当前=当前左侧;
}否则如果(数据>当前数据){
上一次=当前;
curr=curr.right;
}否则{
返回上一个;
}
}
}

此算法绝对有效,但正如您所想象的,这不是您想要回答白板面试问题的怪物类型。

如果您:

  • 通过从
    find()
    返回上一个节点和找到的节点,组合
    prev()
    find()
  • 为每个节点指定一个父指针,然后跟随它查找
    prev

  • 没有递归是故意的吗?在递归数据结构上不使用递归必然是笨拙的。您还应该研究BST旋转,它保留了顺序,但可以让您轻松地移动节点(您也可以使用它们实现平衡)。
    function BST() {
        this.root = null;
    }
    
    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;
            }
        }
    
    };
    
    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;
            }
        }
    }