二叉树系列(3 常见方法)

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());
	        }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值