java构建二叉树及先根,中根,后根遍历

  1. 把数组中存储的数据构建成二叉树。

  2. 根节点循环 for (int parentIndex = 0; parentIndex < array.length / 2 - 1; parentIndex++)

  3. 最有一个根节点即parentIndex =array.length / 2 - 1,最后单独处理,因为最后一个根节点可能没有有孩子,如果总数为奇数才有有孩子

java代码如下:

public void createBinTree() {
		nodeList = new LinkedList<Node>();
		// 将一个数组的值依次转换为Node节点
		for (int nodeIndex = 0; nodeIndex < array.length; nodeIndex++) {
			nodeList.add(new Node(array[nodeIndex]));
		}
		// 对前lastParentIndex-1个父节点按照父节点与孩子节点的数字关系建立二叉树,最后一个根节点可能没有有孩子,如果
		//parentIndex<= array.length / 2 - 1, 数组可能越界,只有奇数个节点才不会越界
		for (int parentIndex = 0; parentIndex < array.length / 2 - 1; parentIndex++) {
			// 左孩子
			nodeList.get(parentIndex).leftChild = nodeList
					.get(parentIndex * 2 + 1);
			// 右孩子
			nodeList.get(parentIndex).rightChild = nodeList
					.get(parentIndex * 2 + 2);
		}
		// 最后一个父节点:因为最后一个父节点可能没有右孩子,所以单独拿出来处理
		int lastParentIndex = array.length / 2 - 1;
		// 左孩子
		nodeList.get(lastParentIndex).leftChild = nodeList
				.get(lastParentIndex * 2 + 1);
		// 右孩子,如果数组的长度为奇数才建立右孩子
		if (array.length % 2 == 1) {
			nodeList.get(lastParentIndex).rightChild = nodeList
					.get(lastParentIndex * 2 + 2);
		}
	}
	/**
	 * 先序遍历
	 * 
	 * 这三种不同的遍历结构都是一样的,只是先后顺序不一样而已
	 * 
	 * @param node
	 *            遍历的节点
	 */
	public static void preOrderTraverse(Node node) {
		if (node == null)
			return;
		System.out.print(node.data + " ");
		preOrderTraverse(node.leftChild);
		preOrderTraverse(node.rightChild);
	}

	/**
	 * 中序遍历
	 * 
	 * 这三种不同的遍历结构都是一样的,只是先后顺序不一样而已
	 * 
	 * @param node
	 *            遍历的节点
	 */
	public static void inOrderTraverse(Node node) {
		if (node == null)
			return;
		inOrderTraverse(node.leftChild);
		System.out.print(node.data + " ");
		inOrderTraverse(node.rightChild);
	}

	/**
	 * 后序遍历
	 * 
	 * 这三种不同的遍历结构都是一样的,只是先后顺序不一样而已
	 * 
	 * @param node
	 *            遍历的节点
	 */
	public static void postOrderTraverse(Node node) {
		if (node == null)
			return;
		postOrderTraverse(node.leftChild);
		postOrderTraverse(node.rightChild);
		System.out.print(node.data + " ");
	}

	public static void main(String[] args) {
		BinTreeTraverse2 binTree = new BinTreeTraverse2();
		binTree.createBinTree();
		// nodeList中第0个索引处的值即为根节点
		Node root = nodeList.get(0);

		System.out.println("先序遍历:");
		preOrderTraverse(root);
		System.out.println();

		System.out.println("中序遍历:");
		inOrderTraverse(root);
		System.out.println();

		System.out.println("后序遍历:");
		postOrderTraverse(root);
	}


非递归遍历方法

  1.  先根遍历:压栈, 出栈,保证顺序,循环栈直到为空

    /**
     * 非递归先根遍历
     * @param p
     */
	 public static void iterativePreorder(Node p) {  
	        Stack<Node> stack = new Stack<Node>();  
	        if (p != null) {  
	            stack.push(p);  
	            while (!stack.empty()) {  
	                p = stack.pop();  
	                visit(p);  
	                if (p.getRightChild()!= null)  
	                    stack.push(p.getRightChild());  
	                if (p.getLeftChild() != null)  
	                    stack.push(p.getLeftChild());  
	            }  
	        }  
	    }

 2  中根遍历

  /** 非递归实现中序遍历 */  
	    protected static void iterativeInorder(Node p) {  
	        Stack<Node> stack = new Stack<Node>();  
	        Node node = p;  
	        while (node != null || stack.size() > 0) {  
	            while (node != null) {  
	                stack.push(node);  
	                node = node.getLeftChild();  
	            }  
	            if (stack.size() > 0) {  
	                node = stack.pop();  
	                visit(node);  
	                node = node.getRightChild();  
	            }  
	        }  
	    }

  3  后根遍历

   /** 非递归实现后序遍历 单栈法*/  
    protected static void iterativePostorder3(Node p) {  
        Stack<Node> stack = new Stack<Node>();  
        Node node = p, prev = p;  
        while (node != null || stack.size() > 0) {  
            while (node != null) {  
                stack.push(node);  
                node = node.getLeft();  
            }  
            if (stack.size() > 0) {  
                Node temp = stack.peek().getRight();  
                if (temp == null || temp == prev) {  
                    node = stack.pop();  
                    visit(node);  
                    prev = node;  
                    node = null;  
                } else {  
                    node = temp;  
                }  
            }  
  
        }  
    }


http://robinsoncrusoe.iteye.com/blog/808526















转载于:https://my.oschina.net/hanruikai/blog/326777

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值