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

1,前序与中序的区别, 仅仅在于输出语句的位置不同.
2,二叉树的遍历, 总共有 6 种排列:

  1. 左中右 (中序);
  2. 左右中 (后序);
  3. 中左右 (前序);
  4. 中右左;
  5. 右左中;
  6. 右中左.
    我们平常关心的是前三种, 是因为我们习惯于先左后右. 如果要先右后左, 就相当于左右子树互换, 这个是很容易做到的.

这里java代码分成两部分
第一部分是队列和算法部分借用以前写过的代码。

package java21to30;

import java.util.Arrays;

public class D24_BinaryCharTree {
	char value;
	D24_BinaryCharTree leftChild;
	D24_BinaryCharTree rightChild;

	public D24_BinaryCharTree(char paraName) {
		value = paraName;
		leftChild = null;
		rightChild = null;
	}

	public void preOrderVisitWithStack() {
		D25_ObjectStack tempStack = new D25_ObjectStack();
		D24_BinaryCharTree tempNode = this;
		while (!tempStack.isEmpty() || tempNode != null) {
			if (tempNode != null) {
				System.out.print("" + tempNode.value + " ");
				tempStack.push(tempNode);
				tempNode = tempNode.leftChild;
			} else {
				tempNode = (D24_BinaryCharTree) tempStack.pop();
				tempNode = tempNode.rightChild;
			} // Of if
		} // Of while
	}

	public void inOrderVisitWithStack() {
		D25_ObjectStack tempStack = new D25_ObjectStack();
		D24_BinaryCharTree tempNode = this;
		while (!tempStack.isEmpty() || tempNode != null) {
			if (tempNode != null) {
				tempStack.push(tempNode);
				tempNode = tempNode.leftChild;
			} else {
				tempNode = (D24_BinaryCharTree) tempStack.pop();
				System.out.print("" + tempNode.value + " ");
				tempNode = tempNode.rightChild;
			} // Of if
		} // Of while
	}

	public void postOrderVisitWithStack() {
		D25_ObjectStack tempStack = new D25_ObjectStack();
		D24_BinaryCharTree tempNode = this;
		D25_ObjectStack tempOutputStack = new D25_ObjectStack();

		while (!tempStack.isEmpty() || tempNode != null) {
			if (tempNode != null) {
				tempOutputStack.push(new Character(tempNode.value));
				tempStack.push(tempNode);
				tempNode = tempNode.rightChild;
			} else {
				tempNode = (D24_BinaryCharTree) tempStack.pop();
				tempNode = tempNode.leftChild;
			}
		}
		while (!tempOutputStack.isEmpty()) {
			System.out.print("" + tempOutputStack.pop() + " ");
		}
	}

	public static D24_BinaryCharTree manualConstructTree() {
		D24_BinaryCharTree resultTree = new D24_BinaryCharTree('a');
		D24_BinaryCharTree tempTreeB = new D24_BinaryCharTree('b');
		D24_BinaryCharTree tempTreeC = new D24_BinaryCharTree('c');
		D24_BinaryCharTree tempTreeD = new D24_BinaryCharTree('d');
		D24_BinaryCharTree tempTreeE = new D24_BinaryCharTree('e');
		D24_BinaryCharTree tempTreeF = new D24_BinaryCharTree('f');
		D24_BinaryCharTree tempTreeG = new D24_BinaryCharTree('g');

		resultTree.leftChild = tempTreeB;
		resultTree.rightChild = tempTreeC;
		tempTreeB.rightChild = tempTreeD;
		tempTreeC.leftChild = tempTreeE;
		tempTreeD.leftChild = tempTreeF;
		tempTreeD.rightChild = tempTreeG;

		return resultTree;
	}

	public D24_BinaryCharTree(char[] paraDataArray, int[] paraIndicesArray) {
		int tempNumNodes = paraDataArray.length;
		D24_BinaryCharTree[] tempAllNodes = new D24_BinaryCharTree[tempNumNodes];
		for (int i = 0; i < tempNumNodes; i++) {
			tempAllNodes[i] = new D24_BinaryCharTree(paraDataArray[i]);
		}
		for (int i = 1; i < tempNumNodes; i++) {
			for (int j = 0; j < i; j++) {
				System.out.println("indices " + paraIndicesArray[j] + " vs. " + paraIndicesArray[i]);
				if (paraIndicesArray[i] == paraIndicesArray[j] * 2 + 1) {
					tempAllNodes[j].leftChild = tempAllNodes[i];
					System.out.println("Linking " + j + " with " + i);
					break;
				} else if (paraIndicesArray[i] == paraIndicesArray[j] * 2 + 2) {
					tempAllNodes[j].rightChild = tempAllNodes[i];
					System.out.println("Linking " + j + " with " + i);
					break;
				}
			}
		}
		value = tempAllNodes[0].value;
		leftChild = tempAllNodes[0].leftChild;
		rightChild = tempAllNodes[0].rightChild;
	}

	public void preOrderVisit() {
		System.out.print("" + value + " ");

		if (leftChild != null) {
			leftChild.preOrderVisit();
		}
		if (rightChild != null) {
			rightChild.preOrderVisit();
		}
	}

	public void inOrderVisit() {
		if (leftChild != null) {
			leftChild.inOrderVisit();
		}
		System.out.print("" + value + " ");

		if (rightChild != null) {
			rightChild.inOrderVisit();
		}
	}

	public void postOrderVisit() {
		if (leftChild != null) {
			leftChild.postOrderVisit();
		}
		if (rightChild != null) {
			rightChild.postOrderVisit();
		}
		System.out.print("" + value + " ");
	}

	public int getDepth() {
		if ((leftChild == null) && (rightChild == null)) {
			return 1;
		}
		int tempLeftDepth = 0;
		if (leftChild != null) {
			tempLeftDepth = leftChild.getDepth();
		}
		int tempRightDepth = 0;
		if (rightChild != null) {
			tempRightDepth = rightChild.getDepth();
		}
		if (tempLeftDepth >= tempRightDepth) {
			return tempLeftDepth + 1;
		} else {
			return tempRightDepth + 1;
		}
	}

	public int getNumNodes() {
		if ((leftChild == null) && (rightChild == null)) {
			return 1;
		}
		int tempLeftNodes = 0;
		if (leftChild != null) {
			tempLeftNodes = leftChild.getNumNodes();
		}
		int tempRightNodes = 0;
		if (rightChild != null) {
			tempRightNodes = rightChild.getNumNodes();
		}
		return tempLeftNodes + tempRightNodes + 1;
	}

	char[] valuesArray;
	int[] indicesArray;

	public void toDataArrays() {
		int tempLength = getNumNodes();

		valuesArray = new char[tempLength];
		indicesArray = new int[tempLength];
		int i = 0;
		D22_CircleObjectQueue tempQueue = new D22_CircleObjectQueue();
		tempQueue.enqueue(this);
		D22_CircleObjectQueue tempIntQueue = new D22_CircleObjectQueue();
		tempIntQueue.enqueue(0);

		D24_BinaryCharTree tempTree = (D24_BinaryCharTree) tempQueue.dequeue();
		int tempIndex = (int) tempIntQueue.dequeue();
		while (tempTree != null) {
			valuesArray[i] = tempTree.value;
			indicesArray[i] = tempIndex;
			i++;

			if (tempTree.leftChild != null) {
				tempQueue.enqueue(tempTree.leftChild);
				tempIntQueue.enqueue(tempIndex * 2 + 1);
			}
			if (tempTree.rightChild != null) {
				tempQueue.enqueue(tempTree.rightChild);
				tempIntQueue.enqueue(tempIndex * 2 + 2);
			}
			tempTree = (D24_BinaryCharTree) tempQueue.dequeue();
			if (tempTree != null)
				tempIndex = (int) tempIntQueue.dequeue();
		}
	}

	public void toDataArraysObjectQueue() {
		int tempLength = getNumNodes();

		valuesArray = new char[tempLength];
		indicesArray = new int[tempLength];
		int i = 0;
		D22_CircleObjectQueue tempQueue = new D22_CircleObjectQueue();
		tempQueue.enqueue(this);
		D22_CircleObjectQueue tempIntQueue = new D22_CircleObjectQueue();
		Integer tempIndexInteger = new Integer(0);
		tempIntQueue.enqueue(tempIndexInteger);

		D24_BinaryCharTree tempTree = (D24_BinaryCharTree) tempQueue.dequeue();
		int tempIndex = ((Integer) tempIntQueue.dequeue()).intValue();
		System.out.println("tempIndex = " + tempIndex);
		while (tempTree != null) {
			valuesArray[i] = tempTree.value;
			indicesArray[i] = tempIndex;
			i++;

			if (tempTree.leftChild != null) {
				tempQueue.enqueue(tempTree.leftChild);
				tempIntQueue.enqueue(new Integer(tempIndex * 2 + 1));
			}
			if (tempTree.rightChild != null) {
				tempQueue.enqueue(tempTree.rightChild);
				tempIntQueue.enqueue(new Integer(tempIndex * 2 + 2));
			}
			tempTree = (D24_BinaryCharTree) tempQueue.dequeue();
			if (tempTree == null) {
				break;
			}
			tempIndex = ((Integer) tempIntQueue.dequeue()).intValue();
		}
	}

	public static void main(String args[]) {
		D24_BinaryCharTree tempTree = manualConstructTree();
		System.out.println("\r\n先序遍历:");
		tempTree.preOrderVisit();
		System.out.println("\r\n中序遍历:");
		tempTree.inOrderVisit();
		System.out.println("\r\n后序遍历:");
		tempTree.postOrderVisit();

		System.out.println("\r\n\r\n深度: " + tempTree.getDepth());
		System.out.println("节点个数: " + tempTree.getNumNodes());
		tempTree.toDataArrays();
		System.out.println("值是: " + Arrays.toString(tempTree.valuesArray));
		System.out.println("指数是:" + Arrays.toString(tempTree.indicesArray));
		tempTree.toDataArraysObjectQueue();
		System.out.println("对象序列.");
		System.out.println("值: " + Arrays.toString(tempTree.valuesArray));
		System.out.println("指数是: " + Arrays.toString(tempTree.indicesArray));

		char[] tempCharArray = { 'A', 'B', 'C', 'D', 'E', 'F' };
		int[] tempIndicesArray = { 0, 1, 2, 4, 5, 12 };
		D24_BinaryCharTree tempTree2 = new D24_BinaryCharTree(tempCharArray, tempIndicesArray);

		System.out.println("\r\n先序遍历:");
		tempTree2.preOrderVisit();
		System.out.println("\r\n中序遍历:");
		tempTree2.inOrderVisit();
		System.out.println("\r\n后序遍历:");
		tempTree2.postOrderVisit();
	}
}

第二部分:通过调用以前的方法来实现我们想要的输出。

package java21to30;

import java.util.Arrays;

public class D26_preOrderVisitWithStack {
	public static void main(String args[]) {
		D24_BinaryCharTree tempTree = D24_BinaryCharTree.manualConstructTree();
		System.out.println("\r先序遍历:");
		tempTree.preOrderVisit();
		System.out.println("\r\n中序遍历:");
		tempTree.inOrderVisit();
		System.out.println("\r\n后序遍历:");
		tempTree.postOrderVisit();

		System.out.println("\r\n\r\n树的深度: " + tempTree.getDepth());
		System.out.println("节点总数: " + tempTree.getNumNodes());

		tempTree.toDataArrays();
		System.out.println("值为: " + Arrays.toString(tempTree.valuesArray));
		System.out.println("指数为: " + Arrays.toString(tempTree.indicesArray));

		tempTree.toDataArraysObjectQueue();
		System.out.println("对象队列。");
		System.out.println("值是: " + Arrays.toString(tempTree.valuesArray));
		System.out.println("指数是: " + Arrays.toString(tempTree.indicesArray));

		char[] tempCharArray = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K' };
		int[] tempIndicesArray = { 0, 1, 2, 4, 5, 12, 11, 14, 16, 13, 10 };
		D24_BinaryCharTree tempTree2 = new D24_BinaryCharTree(tempCharArray, tempIndicesArray);

		System.out.println("\r\n先序遍历:");
		tempTree2.preOrderVisit();
		System.out.println("\r\n中序遍历:");
		tempTree2.inOrderVisit();
		System.out.println("\r\n后序遍历:");
		tempTree2.postOrderVisit();
		System.out.println("\r\n栈的先序遍历:");
		tempTree2.inOrderVisitWithStack();
		System.out.println("\r\n栈的中序遍历:");
		tempTree2.preOrderVisitWithStack();
		System.out.println("\r\n栈的后序遍历:");
		tempTree2.postOrderVisitWithStack();
	}
}

结果输出:


先序遍历:
a b d f g c e 
中序遍历:
b f d g a e c 
后序遍历:
f g d b e c a 

树的深度: 4
节点总数: 7
值为: [a, b, c, d, e, f, g]
指数为: [0, 1, 2, 4, 5, 9, 10]
tempIndex = 0
对象队列。
值是: [a, b, c, d, e, f, g]
指数是: [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
indices 0 vs. 11
indices 1 vs. 11
indices 2 vs. 11
indices 4 vs. 11
indices 5 vs. 11
Linking 4 with 6
indices 0 vs. 14
indices 1 vs. 14
indices 2 vs. 14
indices 4 vs. 14
indices 5 vs. 14
indices 12 vs. 14
indices 11 vs. 14
indices 0 vs. 16
indices 1 vs. 16
indices 2 vs. 16
indices 4 vs. 16
indices 5 vs. 16
indices 12 vs. 16
indices 11 vs. 16
indices 14 vs. 16
indices 0 vs. 13
indices 1 vs. 13
indices 2 vs. 13
indices 4 vs. 13
indices 5 vs. 13
indices 12 vs. 13
indices 11 vs. 13
indices 14 vs. 13
indices 16 vs. 13
indices 0 vs. 10
indices 1 vs. 10
indices 2 vs. 10
indices 4 vs. 10
Linking 3 with 10

先序遍历:
A B D K C E G F 
中序遍历:
B D K A G E F C 
后序遍历:
K D B G F E C A 
栈的先序遍历:
B D K A G E F C 
栈的中序遍历:
A B D K C E G F 
栈的后序遍历:
K D B G F E C A 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值