JAVA数据结构二叉树高度计算

版权声明:版权声明:本文为博主原创文章,博客地址:http://blog.csdn.net/qq_37808895,未经博主允许不得转载。 https://blog.csdn.net/qq_37808895/article/details/88624250

二叉树结点类

public class BinaryNode<T> {
	public T data;
	public BinaryNode<T> left,right;
	public BinaryNode(T data,BinaryNode<T> left,BinaryNode<T> right){
		this.data=data;
		this.left=left;
		this.right=right;
	}
	public BinaryNode(T data){
		this(data,null,null);
	}
	public String toString(){
		return this.data.toString();
	}

}

二叉树类

public class BinaryTree<T> {
	public BinaryNode<T> root;
	public BinaryTree(){
		this.root=null;
	}
	public BinaryTree(BinaryTree<T> bitree){
		this.root=copy(bitree.root);
	}
	private BinaryNode<T> copy(BinaryNode<T> p){
		if(p==null)
			return null;
		BinaryNode<T> q=new BinaryNode<T>(p.data);
		q.left=copy(p.left);
		q.right=copy(p.right);
		return q;
			
	}
	public BinaryNode<T> insert(T x){
		return this.root=new BinaryNode<T> (x,null,null);
	}
	public BinaryNode<T> insert(BinaryNode<T> parent,T x,boolean leftChild){
		if(x==null)
			return null;
		if(leftChild)
			return parent.left=new BinaryNode<T>(x,parent.left,null);
		return parent.right=new BinaryNode<T>(x,null,parent.right);
	}
	public void preorder(){
		preorder(this.root);
		System.out.println();
	}
	public void preorder(BinaryNode<T> p){
		if(p!=null){
			System.out.print(p.data.toString());
			preorder(p.left);
			preorder(p.right);
		}
	}
/*	public void postorderTraverse(){
		BinaryNode<T> p=this.root;
		BinaryNode<T> q=p;
		LinkedStack<BinaryNode<T>> stack=new LinkedStack<BinaryNode<T>>(); 
		while (p != null) {  
        // 左子树入栈  
			for (; p.left != null; p = p.left){  
				stack.push(p); 
				} 
        // 当前节点无右子或右子已经输出  
			while (p != null && (p.right == null || p.right == q)) {  
            System.out.println(p.toString()); 
            System.out.println(stack.toString()+"");
            q = p;// 记录上一个已输出节点  
            if (stack.isEmpty())  
                return;  
            p = stack.pop(); 
            System.out.println(stack.toString()+"");
        }  
        // 处理右子  
        stack.push(p);  
        p = p.right;  
		}
    }*/  
	public int height(){
		if(this.root==null)
			return 0;
		LinkedStack<BinaryNode<T>> stack=new LinkedStack<BinaryNode<T>>();
		int maxheight=0;
		int i=0;
		BinaryNode<T> p=this.root;
		BinaryNode<T> q=p;
		OUT:while (p != null) {  
        // 左子树入栈  
			for (; p.left != null; p = p.left){  
				stack.push(p); 
				i++;
				maxheight=maxheight>i?maxheight:i;
				} 
        // 当前节点无右子或右子已经输出  
			while (p != null && (p.right == null || p.right == q)) {  
            System.out.println(p.toString()); 
            if(p.right!=q&&p.left!=p){
            	 i++;maxheight=maxheight>i?maxheight:i;
            }
            else
            	i--;
            q = p;// 记录上一个已输出节点            
            if (stack.isEmpty())  {
                break OUT;
               }
           
            else{
            	p = stack.pop(); 
            	i--;
            }
        }  
        // 处理右子  
        stack.push(p);
        i++;
        maxheight=maxheight>i?maxheight:i;
        p = p.right;  
		}
		
		System.out.print("Height:");
		return maxheight;
	}

	/*递归
	public int height(){
		return height(root);
	}
	public int height(BinaryNode<T> root){
		if(root==null)
			return 0;
		int L_height=height(root.left);
		int R_height=height(root.right);
		return (L_height>=R_height)?L_height+1:R_height+1;
	}*/

	public static void main(String[] args) {
		String[] list={"A","B","C","D","E","F","G"};
		BinaryTree<String> tree=new BinaryTree<String>();
		tree.insert(list[0]);
		tree.insert(tree.root, list[1],true);
		tree.insert(tree.root,list[2],false);
		
		tree.insert(tree.root.left, list[3], true);
		tree.insert(tree.root.left, list[4], false);
		tree.insert(tree.root.left.right, list[5], true);
		tree.insert(tree.root.left.left, list[6], false);
		tree.preorder();
		int i= tree.height();
		System.out.println(i);
		//tree.postorderTraverse();
		//深拷贝
		BinaryTree<String> ctree=new BinaryTree<String>();
		ctree.copy(tree.root);
	}

}

采用先序遍历求二叉树高的算法int height()方法

猜你喜欢

转载自blog.csdn.net/qq_37808895/article/details/88624250