25-2 中序遍历

【25-2 中序遍历】学习来源:日撸 Java 三百行(21-30天,树与二叉树)_闵帆的博客-CSDN博客 25-2 中序遍历
25-2.1中序是几种遍历中最简单的 。
代码(其余代码位于类中):
/****************** * In-order visit with stack.*****************/public void inOrderVisitWithStack() {ObjectStack tempStack = new ObjectStack();BinaryChartTree tempNode = this;while (!tempStack.isEmpty() || tempNode !=null) {if (tempNode != null) {tempStack.push(tempNode);tempNode = tempNode.leftChild;} else {tempNode = (BinaryChartTree) tempStack.pop();System.out.println("" + tempNode.value + " ");tempNode = tempNode.rightChild;} // Of if} // Of while} // Of inOderVisit/****************** The entrance of the program.* * @param args Not used now.****************/public static void main(String[] args) {BinaryChartTree tempTree = manualConstructTree();System.out.println("\r\nPreorder visit:");tempTree.preOderVisit();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.ToDataArraysObjetcQueue();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};BinaryChartTree tempTree2 = new BinaryChartTree(tempCharArray, tempIndicesArray);System.out.println("\r\nPreorder visit:");tempTree2.preOderVisit();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();} // Of main
截图:
26 二叉树深度遍历的栈实现
26.1 前序与中序的区别, 仅仅在于输出语句的位置不同 。
26.2 二叉树的遍历,总共有 6 种排列:1)左中右(中序);2)左右中(后序);3)中左右(前序);4)中右左;5)右左中;6)右中左 。我们平常关心的是前三种,是因为我们习惯于先左后右 。如果要先右后左,就相当于左右子树互换,这个是很容易做到的 。
代码(其余代码位于类中):
/******************* Pre-order visit with stack.*****************/public void preOrderVisitWithStack() {ObjectStack tempStack = new ObjectStack();BinaryChartTree tempNode = this;while (!tempStack.isEmpty() || tempNode !=null) {if (tempNode != null) {System.out.println("" + tempNode.value + " ");tempStack.push(tempNode);tempNode = tempNode.leftChild;} else {tempNode = (BinaryChartTree) tempStack.pop();tempNode = tempNode.rightChild;} // Of if} // Of while} // Of preOrderVisitWithStack/*********************Post-order visit with stack.*******************/public void postOderVisitWithStack() {ObjectStack tempStack = new ObjectStack();BinaryChartTree 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 = (BinaryChartTree) tempStack.pop();tempNode = tempNode.leftChild;} // Of if} // Of while// Now reverse outputwhile (!tempOutputStack.isEmpty()) {System.out.println("" + tempOutputStack.pop() + " ");} // Of while} // Of postOderVisitWithStack/****************** The entrance of the program.* * @param args Not used now.****************/public static void main(String[] args) {BinaryChartTree tempTree = manualConstructTree();System.out.println("\r\nPreorder visit:");tempTree.preOderVisit();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.ToDataArraysObjetcQueue();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};BinaryChartTree tempTree2 = new BinaryChartTree(tempCharArray, tempIndicesArray);System.out.println("\r\nPreorder visit:");tempTree2.preOderVisit();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.postOderVisitWithStack();} // Of main