//数据结构------二叉树
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());
js---树的创建,查找,添加,查看树的最大最小高度以及判断是否为平衡二叉树
最新推荐文章于 2022-03-23 13:12:00 发布