手写二叉树

public class BinaryTree {
    private TreeNode  root = null;

    public BinaryTree(){
        root = new TreeNode(1, "A");
    }

    /**
     * 构建二叉树
     *         A
     *     B       C
     * D      E        F
     */
    public void createBinaryTree(){
        TreeNode nodeB = new TreeNode(2, "B");
        TreeNode nodeC = new TreeNode(3, "C");
        TreeNode nodeD = new TreeNode(4, "D");
        TreeNode nodeE = new TreeNode(5, "E");
        TreeNode nodeF = new TreeNode(6, "F");
        root.leftChild = nodeB;
        root.rightChild = nodeC;
        nodeB.leftChild = nodeD;
        nodeB.rightChild = nodeE;
        nodeC.rightChild = nodeF;
    }

    /**
     * 求二叉树的高度
     * @author Administrator
     *
     */
    public int getHeight(){
        return getHeight(root);
    }

    private int getHeight(TreeNode node) {
        if(node == null){
            return 0;
        }else{
            int i = getHeight(node.leftChild);
            int j = getHeight(node.rightChild);
            return (i<j)?j+1:i+1;
        }
    }


    /**
     * 获取二叉树的结点数
     * @author Administrator
     *
     */
    public int getSize(){
        return getSize(root);
    }


    private int getSize(TreeNode node) {
        if(node == null){
            return 0;
        }else{
            return 1+getSize(node.leftChild)+getSize(node.rightChild);
        }
    }


    /**
     * 前序遍历——迭代
     * @author Administrator
     *
     */
    public void preOrder(TreeNode node){
        if(node == null){
            return;
        }else{
            System.out.println("preOrder data:"+node.getData());
            preOrder(node.leftChild);
            preOrder(node.rightChild);
        }
    }

    /**
     * 前序遍历——非迭代
     */

    public void nonRecOrder(TreeNode node){
        if(node == null){
            return;
        }
        Stack<TreeNode> stack = new Stack<TreeNode>();
        stack.push(node);
        while(!stack.isEmpty()){
//出栈和进栈
            TreeNode n = stack.pop();//弹出根结点
//压入子结点
            System.out.println("nonRecOrder data"+n.getData());
            if(n.rightChild!=null){
                stack.push(n.rightChild);

            }
            if(n.leftChild!=null){
                stack.push(n.leftChild);
            }
        }
    }

    /**
     * 中序遍历——迭代
     * @author Administrator
     *
     */
    public void midOrder(TreeNode node){
        if(node == null){
            return;
        }else{
            midOrder(node.leftChild);
            System.out.println("midOrder data:"+node.getData());
            midOrder(node.rightChild);
        }
    }

    /**
     * 后序遍历——迭代
     * @author Administrator
     *
     */
    public void postOrder(TreeNode node){
        if(node == null){
            return;
        }else{
            postOrder(node.leftChild);
            postOrder(node.rightChild);
            System.out.println("postOrder data:"+node.getData());
        }
    }
    public class TreeNode{
        private int index;
        private String data;
        private TreeNode leftChild;
        private TreeNode rightChild;


        public int getIndex() {
            return index;
        }




        public void setIndex(int index) {
            this.index = index;
        }




        public String getData() {
            return data;
        }




        public void setData(String data) {
            this.data = data;
        }




        public TreeNode(int index,String data){
            this.index = index;
            this.data = data;
            this.leftChild = null;
            this.rightChild = null;
        }
    }


    public static void main(String[] args){
        BinaryTree binaryTree = new BinaryTree();
        binaryTree.createBinaryTree();
        int height = binaryTree.getHeight();
        System.out.println("treeHeihgt:"+height);
        int size = binaryTree.getSize();
        System.out.println("treeSize:"+size);
// binaryTree.preOrder(binaryTree.root);
// binaryTree.midOrder(binaryTree.root);
// binaryTree.postOrder(binaryTree.root);
        binaryTree.nonRecOrder(binaryTree.root);
    }

//不明白的大家可以一起讨论!欢迎留言!
 
	/**
	 * public class Node {
		public int data; //树结点标号
		public Node lchild; //左子树
		public Node rchild;  //右子树
	}
	后序遍历递归定义:先左子树,后右子树,再根节点。
	后序遍历的难点在于:需要判断上次访问的节点是位于左子树,还是右子树。
		若是位于左子树,则需跳过根节点,先进入右子树,再回头访问根节点;
		若是位于右子树,则直接访问根节点。
	 */
	
	public void postOrder(Node node){
		if(node==null)
			return;
		Stack<Node> s = new Stack<Node>();
		
		Node curNode; //当前访问的结点
		Node lastVisitNode; //上次访问的结点
		curNode = node;
		lastVisitNode = null;
		
		//把currentNode移到左子树的最下边
		while(curNode!=null){
			s.push(curNode);
			curNode = curNode.getLchild();
		}
		while(!s.empty()){
			curNode = s.pop();  //弹出栈顶元素
			//一个根节点被访问的前提是:无右子树或右子树已被访问过
			if(curNode.getRchild()!=null&&curNode.getRchild()!=lastVisitNode){
				//根节点再次入栈
				s.push(curNode);
				//进入右子树,且可肯定右子树一定不为空
				curNode = curNode.getRchild();
				while(curNode!=null){
					//再走到右子树的最左边
					s.push(curNode);
					curNode = curNode.getLchild();
				}
			}else{
				//访问
				System.out.println(curNode.getData());
				//修改最近被访问的节点
				lastVisitNode = curNode;
			}
		} //while
	}
 


}
         
        //中序遍历的非递归实现    
        public void nonRecInOrder(TreeNode p){    
            Stack<TreeNode> stack =new Stack<TreeNode>();    
            TreeNode node =p;    
            while(node!=null||stack.size()>0){    
                //存在左子树    
                while(node!=null){    
                    stack.push(node);    
                    node=node.leftChild;    
                }    
                //栈非空    
                if(stack.size()>0){    
                    node=stack.pop();    
                    visted(node);    
                    node=node.rightChild;    
                }    
            }    
        }     

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值