Java学习第26天:二叉树深度遍历的栈实现 (前序和后序)

主要任务:以为前序和后序要比中序难很多, 其实并没有.前序与中序的区别, 仅仅在于输出语句的位置不同.

二叉树的遍历, 总共有 6 种排列: 1) 左中右 (中序); 2) 左右中 (后序); 3) 中左右 (前序); 4) 中右左; 5) 右左中; 6) 右中左. 我们平常关心的是前三种, 是因为我们习惯于先左后右. 如果要先右后左, 就相当于左右子树互换, 这个是很容易做到的.如果将前序的左右子树互换, 就可得到 4) 中右左; 再进行逆序, 可以得到 2) 左右中.

因此, 要把前序的代码改为后序, 需要首先将 leftChild 和rightChild 互换, 然后用一个栈来存储需要输出的字符, 最终反向输出即可. 这种将一个问题转换成另一个等价问题的方式, 无论在数学还是计算机领域, 都极度重要. 


在二叉树的代码中继续添加以下代码:

 * 按前序访问堆栈.
	 *********************
	 */
	public void preOrderVisitWithStack() {
		ObjectStack tempStack = new ObjectStack();
		BinaryCharTree tempNode = this;
		while (!tempStack.isEmpty() || tempNode != null) {
			if (tempNode != null) {
				System.out.print("" + tempNode.value + " ");
				tempStack.push(tempNode);
				tempNode = tempNode.leftChild;
			} else {
				tempNode = (BinaryCharTree) tempStack.pop();
				tempNode = tempNode.rightChild;
			} // Of if
		} // Of while
	}// Of preOrderVisitWithStack

	/**
	 *********************
	 * 按后续访问堆栈.
	 *********************
	 */
	public void postOrderVisitWithStack() {
		ObjectStack tempStack = new ObjectStack();
		BinaryCharTree tempNode = this;
		ObjectStack tempOutputStack = new ObjectStack();
		
		while (!tempStack.isEmpty() || tempNode != null) {
			if (tempNode != null) {
				//Store for output.
				tempOutputStack.push(new Character(tempNode.value));
				tempStack.push(tempNode);
				tempNode = tempNode.rightChild;
			} else {
				tempNode = (BinaryCharTree) tempStack.pop();
				tempNode = tempNode.leftChild;
			} // Of if
		} // Of while
		
		//Now reverse output.
		while (!tempOutputStack.isEmpty()) {
			System.out.print("" + tempOutputStack.pop() + " ");
		}//Of while
	}// Of postOrderVisitWithStack


public static void main(String args[]) {
		BinaryCharTree tempTree = manualConstructTree();
		System.out.println("\r\nPreorder visit:");
		tempTree.preOrderVisit();
		System.out.println("\r\nIn-order visit:");
		tempTree.inOrderVisit();
		System.out.println("\r\nPost-order visit:");
		tempTree.postOrderVisit();

		System.out.println("\r\n\r\nThe depth is: " + tempTree.getDepth());
		System.out.println("The number of nodes is: " + tempTree.getNumNodes());
		
		tempTree.toDataArrays();
		System.out.println("The values are: " + Arrays.toString(tempTree.valuesArray));
		System.out.println("The indices are: " + Arrays.toString(tempTree.indicesArray));
		tempTree.toDataArraysObjectQueue();
		System.out.println("Only object queue.");
		System.out.println("The values are: " + Arrays.toString(tempTree.valuesArray));
		System.out.println("The indices are: " + Arrays.toString(tempTree.indicesArray));
		char[] tempCharArray = {'A', 'B', 'C', 'D', 'E', 'F'};
		int[] tempIndicesArray = {0, 1, 2, 4, 5, 12};
		BinaryCharTree tempTree2 = new BinaryCharTree(tempCharArray, tempIndicesArray);

		System.out.println("\r\nPreorder visit:");
		tempTree2.preOrderVisit();
		System.out.println("\r\nIn-order visit:");
		tempTree2.inOrderVisit();
		System.out.println("\r\nPost-order visit:");
		tempTree2.postOrderVisit();
		
		System.out.println("\r\nIn-order visit with stack:");
		tempTree2.inOrderVisitWithStack();
		System.out.println("\r\nPre-order visit with stack:");
		tempTree2.preOrderVisitWithStack();
		System.out.println("\r\nPost-order visit with stack:");
		tempTree2.postOrderVisitWithStack();

	}// Of main

输出结果:

Preorder visit:
a b d f g c e 
In-order visit:
b f d g a e c 
Post-order visit:
f g d b e c a 

The depth is: 4
The number of nodes is: 7
The values are: [a, b, c, d, e, f, g]
The indices are: [0, 1, 2, 4, 5, 9, 10]
tempIndex = 0
Only object queue.
The values are: [a, b, c, d, e, f, g]
The indices are: [0, 1, 2, 4, 5, 9, 10]
indices 0 vs. 1
Linking 0 with 1
indices 0 vs. 2
Linking 0 with 2
indices 0 vs. 4
indices 1 vs. 4
Linking 1 with 3
indices 0 vs. 5
indices 1 vs. 5
indices 2 vs. 5
Linking 2 with 4
indices 0 vs. 12
indices 1 vs. 12
indices 2 vs. 12
indices 4 vs. 12
indices 5 vs. 12
Linking 4 with 5

Preorder visit:
A B D C E F 
In-order visit:
B D A E F C 
Post-order visit:
D B F E C A 
In-order visit with stack:
B D A E F C 
Pre-order visit with stack:
A B D C E F 
Post-order visit with stack:
D B F E C A 

注:前序遍历(根左右),后序遍历为(左右根),因此可以先将前序遍历中的左右孩子顺序调换一下,最后再将输出结果逆向输出就得到了后序遍历结果。而前序与中序的区别也仅仅就是输出语句的位置不同而已。需要下来多理解理解,多看例子。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值