二进制搜索树是平衡的Javascript

二进制搜索树是平衡的Javascript,javascript,binary-search-tree,Javascript,Binary Search Tree,我想添加一个方法,如果二叉搜索树是平衡的,我想我会使用一个方法,给出两边的最大值和最小值,然后看看差值是否为0,但我无法从分支中获得值。 如果有人知道我能做些什么,或者用更有效的方法看看BST是否平衡 函数二进制搜索树(值){ 这个值=值; this.left=null; this.right=null; } BinarySearchTree.prototype.insert=函数(值){ var nuevo=新的二进制搜索树(值); 如果(值>此.value){ 如果(!this.right

我想添加一个方法,如果二叉搜索树是平衡的,我想我会使用一个方法,给出两边的最大值和最小值,然后看看差值是否为0,但我无法从分支中获得值。 如果有人知道我能做些什么,或者用更有效的方法看看BST是否平衡

函数二进制搜索树(值){
这个值=值;
this.left=null;
this.right=null;
}
BinarySearchTree.prototype.insert=函数(值){
var nuevo=新的二进制搜索树(值);
如果(值>此.value){
如果(!this.right){
this.right=nuevo;
}否则{
此。右。插入(值);
}
}否则{
如果(!this.left){
this.left=nuevo;
}否则{
此。左。插入(值);
}
}
} 
BinarySearchTree.prototype.contains=函数(值){
如果(value==this.value)返回true;
如果(值>此.value){
return!!this.right&&this.right.contains(值);
}
if(值<此值){
return!!this.left&&this.left.contains(值);
}
}
BinarySearchTree.prototype.BroadthFirstForeach=函数(fn,数组){
if(!数组){
var数组=[];
}
!!this.left&&array.push(this.left);
!!this.right&&array.push(this.right);
fn(该值);
!!array.length&&array.shift().breadthFirstForEach(fn,数组);
}
BinarySearchTree.prototype.size=函数(){
如果(!this.left&&!this.right)返回1;
如果(!this.left&&this.right)返回1+this.right.size();
if(this.left&!this.right)返回1+this.left.size();
返回1+this.left.size()+this.right.size();
}
BinarySearchTree.prototype.print=函数(){
}
BinarySearchTree.prototype.remove=函数(值){
if(值<此值){
this.left=this.left&&this.left.remove(值);
}else if(值>此.value){
this.right=this.right&&this.right.remove(值);
}else if(this.left&&this.right){
this.value=this.right.findMinValue();
this.right=this.right.remove(this.value);
}否则{
把这个.左| |这个.右;
}
还这个
}
BinarySearchTree.prototype.findMinValue=函数(){
返回this.left?this.left.findMinValue():this.value;
}
BinarySearchTree.prototype.minDepth=函数(){
返回this.left&&this.right?this.size+Math.min(this.left.minDepth())+Math.min(this.right.minDepth()):null

}
每次创建新树时,您都将创建一个新树node@EugenSunic不,BinarySearchTree类只表示树中的一个节点。每次插入只会创建一个新节点并将其附加到树的正确位置。“我无法从分支中获取值”-看起来您已经很清楚该怎么做了。因此,现在只需继续添加一个新方法即可返回所需的值(例如,具有
min
max
的对象)!每次创建新树时,都将创建新树node@EugenSunic不,BinarySearchTree类只表示树中的一个节点。每次插入只会创建一个新节点并将其附加到树的正确位置。“我无法从分支中获取值”-看起来您已经很清楚该怎么做了。因此,现在只需继续添加一个新方法即可返回所需的值(例如,具有
min
max
的对象)!