抄代码DAY19 二叉树深度遍历的栈实现 (先序和后序)

昨天写了二叉树非递归(利用栈)的中序遍历。今天学习利用栈实现前序和后序。在我数据结构学习的过程中,没有接触过除了前中后三种遍历以外其他的遍历,因为习惯默认都是先左后右。浅谈一下前序和后序:

(代码时在昨天的代码上继续的)

前序:中左右的顺序。当栈不空或者树中节点尚未访问完时,进入循环。若树中节点尚未访问,访问数中节点并将其打印输出,再将该节点入栈,访问其左孩子。执行此循环,直至其无左孩子(左孩子为空),将栈顶元素出栈,访问出栈节点的右孩子。执行以上循环。当树中节点全部访问结束且栈空,结束while循环。

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;//访问出栈元素的右孩子
			} 
		} 
	}
————————————————
版权声明:本文为CSDN博主「闵帆」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/minfanphd/article/details/116975721

后序:思想为将先序(中左右)的左右顺序互换得到:中右左   再进行逆置为:左右中,得到后序遍历。

逆置时,利用栈进行逆置。将一列元素全部依次入栈,再输出,就得到了逆置后的元素序列。

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;//访问节点左孩子
			} 
		} 
		
		//逆置
		while (!tempOutputStack.isEmpty()) {
			System.out.print("" + tempOutputStack.pop() + " ");
		}
	}
————————————————
版权声明:本文为CSDN博主「闵帆」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/minfanphd/article/details/116975721

mian方法:

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\nPre-order 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();
	}

结果:

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

实现其他的输出如法炮制。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值