day15-Java(二叉树前序和后序遍历的栈实现)

这篇博客介绍了如何使用栈来实现Java中的二叉树前序和后序遍历。在前序遍历中,栈的进栈顺序决定了遍历顺序,而在后序遍历中,遍历结束后根据栈内的元素顺序得到遍历结果。文章通过实例展示了整个过程,并提供了代码和数据测试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

二叉树前序和后序遍历的栈实现

一、前序遍历的栈实现

在这里插入图片描述
Pre-order visit with stack: A

tempNode进出栈
Apush(A)
A.leftChild/

在这里插入图片描述
Pre-order visit with stack: A B

tempNode进出栈
Bpush(B)
A.leftChild/
nullpop(B)
B.rightChild/

在这里插入图片描述
Pre-order visit with stack: A B D

tempNode进出栈
Dpush(D)
A.leftChild/
nullpop(D)
D.rightChild/
nullpop(A)
A.rightChild/

这里注意与中序遍历的区别,虽然到目前为止,二叉树个节点的遍历顺序与站内元素都与中序遍历一样,但栈的作用完全不一样。在中序遍历中,栈中元素的出栈顺序即为遍历的顺序,而在前序遍历中,栈中元素的进栈顺序即为遍历顺序。

在这里插入图片描述
Pre-order visit with stack: A B D C

tempNode进出栈
Cpush(C )
C.leftChild/

在这里插入图片描述
Pre-order visit with stack: A B D C E

tempNode进出栈
Epush(E)
E.leftChild/
nullpop(E)
E.rightChild/

在这里插入图片描述
Pre-order visit with stack: A B D C E F

tempNode进出栈
Fpush(F)
F.leftChild/
nullpop(F)
F.rightChildpop(F)
nullpop(C )
C.rightChild/
null/

结束遍历。

二、后序遍历的栈实现

在这里插入图片描述

tempNodetempOutputStacktempStack
Apush(A)push(A)
A.rightChild//

在这里插入图片描述

tempNodetempOutputStacktempStack
Cpush©push©
C.rightChild//

在这里插入图片描述

tempNodetempOutputStacktempStack
null/push©
C.leftChild//
Epush(E)push(E)
E.rightChild//

在这里插入图片描述

tempNodetempOutputStacktempStack
Fpush(F)push(F)
F.rightChild//
null/pop(F)
F.leftChild//
null/pop(E)
E.leftChild//
null/pop(A)
A.leftChild//

在这里插入图片描述

tempNodetempOutputStacktempStack
Bpush(B)push(B)
B.rightChild//

在这里插入图片描述

tempNodetempOutputStacktempStack
Dpush(D)push(D)
D.rightChild//
null/pop(D)
D.leftChild/
null/pop(B)

遍历结束。
根据最后的tempOutputStack,Post-order visit with stack: D B F E C A。

三、代码与数据测试

/**
	 ********************
	 * Pre-order visit with stack.
	 ********************
	 */
	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
	
	/**![在这里插入图片描述](https://img-blog.csdnimg.cn/33be8a6b883f4f80adc09733d298ec4b.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAbm9vb29vb29vb29vYg==,size_13,color_FFFFFF,t_70,g_se,x_16#pic_center)

	 ********************
	 * Post-order visit with stack.
	 ********************
	 */
	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

运行结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值