package com.daojia.collect;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
public class Btree {
private Node root = null;
Btree(int value) {
root = new Node(value);
root.leftChild = null;
root.rightChild = null;
}
//查找
public Node findKey(int value) {
Node current = root ;
while(true){
if(current.value== value)
{
return current;
}
else if(current.value>value){
current = current.leftChild;
}else if(current.value < value){
current = current.rightChild;
}
if(current == null){
return null;
}
}
}
//插入
public Node insert(int value) {
String error = null;
Node node = new Node(value);
if (root == null) {
root = node;
root.leftChild = null;
root.rightChild = null;
} else {
Node current = root;
Node parent = null;
while (true) {
if (value < current.value) {
parent = current;
current = current.leftChild;
if (current == null) {
parent.leftChild = node;
break;
}
} else if (value > current.value) {
parent = current;
current = current.rightChild;
if (current == null) {
parent.rightChild = node;
break;
}
} else {
System.out.println("exists");
break;
}
} // end of while
}
return node;
}
/**
* 求二叉树的最大深度
* @param node
* @return
*/
public int getMaxdepth(Node node){
if(node == null)
{
return 0;
}
int left = getMaxdepth(node.leftChild);
int right = getMaxdepth(node.rightChild);
return Math.max(left,right)+1;
}
/**
* 求二叉树的最小深度
* @param node
* @return
*/
public int getMindepth(Node node){
if(node == null)
{
return 0;
}
if(node.leftChild== null && node.rightChild == null){
return 1;
}
return Math.min(getMindepth(node.leftChild),getMindepth(node.rightChild) )+1;
}
/**
* 求二叉树节点数量
* @param node
* @return
*/
public int getNumofNodes(Node node){
if(node == null){
return 0;
}
int left = getNumofNodes(node.leftChild);
int right = getNumofNodes(node.rightChild);
return left +right+1;
}
/**
* 求叶子节点数量
* @param node
* @return
*/
public int getNumoflastnode(Node node){
if(node == null){
return 0;
}
if(node.leftChild == null && node.rightChild == null)
{
return 1;
}
return getNumoflastnode(node.leftChild)+getNumoflastnode(node.rightChild);
}
/**
* 求二叉树第K层节点个数
* @param node
* @param k
* @return
*/
public int getKlevelNum(Node node,int k){
if(node == null || k<=0)
{
return 0;
}
if(k ==1){
return 1;
}
return getKlevelNum(node.leftChild,k-1)+getKlevelNum(node.rightChild,k-1);
}
/**
* 是否平衡二叉树:左右子树的深度差不能大于2
* @return
*/
public boolean isBlanceBtree(){
if(getDepth(root)!=-1){
return true;
}
return false;
}
private int getDepth(Node node){
if(node == null){
return 0;
}
int left = getDepth(node.leftChild);
int right = getDepth(node.rightChild);
//平衡树是要求左右子树深度相差<2
if(Math.abs(left-right)>1)
{
return -1;
}
return Math.max(left, right)+1;
}
/**
* 一次节点遍历法。
* 是否平衡二叉树:左右子树的深度差不能大于2
* @return
*/
private boolean isBalanced=true;
public boolean isBalanceTreeNew(){
getDepthNew(root);
return isBalanced;
}
private int getDepthNew(Node node){
if(node == null){
return 0;
}
int left = getDepthNew(node.leftChild);
int right = getDepthNew(node.rightChild);
if(Math.abs(left-right)>1){
isBalanced =false;
}
return left >right ? left+1:right+1;
}
public static void main(String[] args){
Btree bt = new Btree(52);
bt.insert(580);
bt.insert(12);
bt.insert(50);
bt.insert(58);
bt.insert(9);
bt.insert(888);
bt.insert(248);
bt.insert(32);
bt.insert(666);
bt.insert(455);
bt.insert(777);
bt.insert(999);
System.out.println("最大深度:"+bt.getMaxdepth(bt.root));
System.out.println("最小深度:"+bt.getMindepth(bt.root));
System.out.println("所有叶子节点数:"+bt.getNumofNodes(bt.root));
System.out.println("叶子节点数:"+bt.getNumoflastnode(bt.root));
System.out.println("第K层节点数目:"+bt.getKlevelNum(bt.root, 4));
System.out.println("是否平衡树:"+bt.isBlanceBtree());
System.out.println("是否平衡树一次遍历法:"+bt.isBalanceTreeNew());
}
}
二叉树系列(3 常见方法)
最新推荐文章于 2022-12-21 19:11:54 发布