JAVA学习—二叉树深度遍历的栈实现 (前序和后序)—2021-06-14
前序与中序的区别, 仅仅在于输出语句的位置不同. 二叉树的遍历, 总共有 6 种排列: 1) 左中右 (中序); 2) 左右中 (后序);
3) 中左右 (前序); 4) 中右左; 5) 右左中; 6) 右中左. 我们平常关心的是前三种, 是因为我们习惯于先左后右.
如果要先右后左, 就相当于左右子树互换, 这个是很容易做到的. 如果将前序的左右子树互换, 就可得到 4) 中右左; 再进行逆序, 可以得到
2) 左右中. 因此, 要把前序的代码改为后序, 需要首先将 leftChild 和 rightChild 互换,
然后用一个栈来存储需要输出的字符, 最终反向输出即可. 这种将一个问题转换成另一个等价问题的方式, 无论在数学还是计算机领域, 都极度重要.
参见 https://blog.csdn.net/minfanphd/article/details/117318844 中莫峦奇的版本.
如果不按上述方式, 直接写后序遍历, 就会复杂得多, 有双重的 while 循环. 参见
https://blog.csdn.net/minfanphd/article/details/117318844 中潘佳豪的版本.
———————————————— @minfanphd
/**
*********************
* 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
/**
*********************
* 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
/**
*********************
* The entrance of the program.
*
* @param args
* Not used now.
*********************
*/
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();
}// Of main
基础知识
JAVA中的数据类型
基础类型8种:byte、short、int、long、float、double、char、boolean
引用类型:除了基本类型都算,包括数组、类、接口、字符串等
注意:定义一个long数据时,要在后加L;定义float数据时,后加F。
变量的作用域:变量定义在哪个大括号内就只能在哪引用,超出就不能。
可以同时定义多个类型相同的变量。如:int a,b,c;
或者 int a=1, b=2, c=3;
运算符
四则运算符
+号对于字符串来说不是相加的意思而是连接的功能;任何数据类型与字符串类型相连,结果都是字符串。
自增运算符:++
自减运算符: --
,可以使用在变量前面或者后面,当单独使用时,没有什么区别;混合使用的时候,有很大区别,前加加则是先加后用,后加加则是先用后加。只能用于变量不能用于常量。
赋值预算符
复合赋值运算符+=、-=、*=、/=、%=
这些,赋值运算符左边只能是一个变量,右边无所谓。
比较运算符<、>、<=、>=、==、!=
这些,==两个等号连写才是相等的意思。比较预算符的结果一定是一个boolean指,成立true,错误false。
逻辑运算符
与&
或|
亦或^
非!
与&
全是true才是true;否则就是false
或|
至少一个true就是true;全是false才是false
亦或^
相同就是false;不同才是true
非!
本来是true变成false;本来是false变成true