学习来源:日撸 Java 三百行(21-30天,树与二叉树)
第 26 天: 二叉树深度遍历的栈实现 (前序和后序)
思路:前序与中序的区别,仅仅在于输出语句的位置不同。如果将前序的左右子树互换,就可得到中右左;再进行逆序,可以得到左右中。因此, 要把前序的代码改为后序,需要首先将 leftChild 和 rightChild 互换,然后用一个栈来存储需要输出的字符,最终反向输出即可。
输入:输入字符数组tempCharArray和整型数组tempIndicesArray构建二叉树
输出:输出该二叉树前序、中序、后序遍历序列(利用栈)
优化目标:无
/**
* @Description: pre-order visit with stack.
* @param none
*/
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
/**
* @Description: Post-order visit with stack.
* @param none
*/
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\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
运行截图:
第 27 天: Hanoi 塔问题
输入:输入三个字符表示三根柱子以及盘子总数paraNumber
输出:输出盘子的移动顺序
优化目标:无
package my_java;
/**
* @Description: Hanoi
* @author: Xin-Yu Li
* @date: 2022年4月17日
*/
public class Hanoi {
/**
* @Description: Move a number of plates.
* @param paraSource The source pole.
* @param paraIntermedium The intermediary pole.
* @param paraDestination The destination pole.
* @param paraNumber The number of plates.
*/
public static void hanoi(char paraSource, char paraIntermediary, char paraDestination,int paraNumber) {
if (paraNumber == 1) {
System.out.println(paraSource + "->" + paraDestination + " ");
return;
} // Of if
hanoi(paraSource, paraDestination, paraIntermediary, paraNumber - 1);//将a上paraNumber-1个盘子借助c移动到b
System.out.println(paraSource + "->" + paraDestination + " ");//将a上最后一个盘子移动到c
hanoi(paraIntermediary, paraSource, paraDestination, paraNumber - 1);//将b上paraNumber-1个盘子借助a移动到c
}// Of hanoi
public static void main(String args[]) {
hanoi('a', 'b', 'c', 3);
}// Of main
}// Of class Hanoi
运行截图: