js---树的创建,查找,添加,查看树的最大最小高度以及判断是否为平衡二叉树

//数据结构------二叉树
var displayTree = (tree) => console.log(JSON.stringify(tree, null, 2));
function Node(value) {
    this.value = value;
    this.left = null;
    this.right = null;
}
function BinarySearchTree() {
    this.root = null;
    // change code below this line
        this.findMin=function(){
          if(this.root==null){
            return null;
        }
    	var current=this.root
    	while(current.left!==null){
    		current=current.left;
    	}
    	return current.value;
    };
    this.findMax=function(){
        if(this.root==null){
            return null;
        }
    	var current=this.root
    	while(current.right!==null){
    		current=current.right;
    	}
    	return current.value;
    };
//  添加节点
    this.add=function(data){
    	if(this.find(data)){
    		return null;
    	}
    	var n = new Node(data);
    	if(this.root==null){
    		this.root=n;
    	}else{
    		var current=this.root;
    		var parent;
    		while(current){
    			parent=current;
    			//左子树
	    		if(data<current.value){
	    			current=current.left;
	    			if(parent.left==null){
	    				parent.left=n;
	    				break;
	    			}
	    		}else{
	    			//右子树
	    			current=current.right;
	    			if(parent.right==null){
	    				parent.right=n;
	    				break;
	    			}
	    			
	    		}
    		}
    	}
    }
    
    this.find=(data)=>{
    	var n=new Node(data);
    	var current=this.root;
    	while(current){
    		if(data==current.value){
    			return true;
    			break;
    		}else if(data<current.value){
    			current=current.left;
    			if(current==null){
    				return false;
    				break;
    			}
    		}else{
    			current=current.right;
    			if(current==null){
    				return false;
    				break;
    			}
    		}
    	}
    }
    

this.findMaxHeight=function (root=this.root){
    if(root == null){
        return -1;
    }
    else{
        var left = this.findMaxHeight(root.left);
        var right = this.findMaxHeight(root.right);
//      console.log(left,right);
        return  1+Math.max(left,right);
    }
}


this.findMinHeight=function (root=this.root){
    if(root == null){
        return -1;
    }
    else{
        var left = this.findMinHeight(root.left);
        var right = this.findMinHeight(root.right);
        return 1+Math.min(left,right);
    }
}
// 二叉树的高度
this.treeHeight=function(root=this.root){
	if(root==null){
		return 0;
	}else{
		var left = this.treeHeight(root.left);
        var right = this.treeHeight(root.right);
//      console.log(left,right);
        return  1+Math.max(left,right);
	}
}
   this.isBalanced=function(root=this.root){
   if(root==null){
   	//空树是平衡二叉树
   	return true;
   }
   var right=this.treeHeight(root.right);
   var left=this.treeHeight(root.left);
   var gap=right-left;
if(gap<-1||gap>1){
   	return false;
   }
   //递归判断子树
   return this.isBalanced(root.right)&&this.isBalanced(root.left);
   }
 //使用深度优先搜索
   //中序遍历---遍历后的顺序即为由小到大排列的顺序
   var list=[];
   this.inorder=function(root=this.root){
		 if(root){
                this.inorder(root.left);
                //使用递归,当左子树为空时输出当前子树再逐级返回
//              console.log(root.value);
                list.push(root.value);
                this.inorder(root.right);
            }else{
                return null;
            }
		 return list;	
   }
   //前序遍历---在叶子之前探索所有的根
	this.preorder=function(root=this.root){
		if(root){
			list.push(root.value);
			this.preorder(root.left);
			this.preorder(root.right);
		}else{
                return null;
       	}
		 return list;	
	}
   
   //后序遍历---在根之前探索所有的叶子
   this.postorder=function(root=this.root){
   	if(root){
			this.postorder(root.left);
			this.postorder(root.right);
			list.push(root.value);
			console.log(root.value);
		}else{
                return null;
        }
   	return list;
   }


}



var tree= new BinarySearchTree;
tree.add(5);
tree.add(4);
tree.add(7);

console.log(tree);
console.log(tree.isBalanced());


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值