Day20 二叉树的深度遍历的递归实现
很久没写代码了,今天开始康复学习,今天就主要是递归 心得懒得写了
1 public class BinaryCharTree { 2 /** 3 * The value in char. 4 */ 5 char value; 6 7 /** 8 * The left child 9 */ 10 BinaryCharTree leftChild; 11 12 /** 13 * The right child 14 */ 15 BinaryCharTree rigthChild; 16 17 /** 18 * The first construcotr 19 * 20 * @param paraName The value. 21 */ 22 public BinaryCharTree(char paraName) { 23 value = paraName; 24 leftChild = null; 25 rigthChild = null; 26 }// Of the constructor 27 28 /** 29 * Manually construct a tree. Only for testing. 30 * 31 * @return 32 */ 33 public static BinaryCharTree manualConstructTree() { 34 // step 1. Construct a tree with only one node. 35 BinaryCharTree resultTree = new BinaryCharTree('a'); 36 37 // Step 2. Construct all nodes. The first node is the root. 38 BinaryCharTree tempTreeB = new BinaryCharTree('b'); 39 BinaryCharTree tempTreeC = new BinaryCharTree('c'); 40 BinaryCharTree tempTreeD = new BinaryCharTree('d'); 41 BinaryCharTree tempTreeE = new BinaryCharTree('e'); 42 BinaryCharTree tempTreeF = new BinaryCharTree('f'); 43 BinaryCharTree tempTreeG = new BinaryCharTree('g'); 44 45 // Step 3. Link all nodes. 46 resultTree.leftChild = tempTreeB; 47 resultTree.rigthChild = tempTreeC; 48 tempTreeB.rigthChild = tempTreeD; 49 tempTreeC.leftChild = tempTreeE; 50 tempTreeD.leftChild = tempTreeF; 51 tempTreeD.rigthChild = tempTreeG; 52 53 return resultTree; 54 }// Of manualConstructTree 55 56 /** 57 * Pre-order visit. 58 */ 59 public void preOrderVisit() { 60 System.out.println("" + value + ""); 61 62 if (leftChild != null) { 63 leftChild.preOrderVisit(); 64 } // Of if 65 66 if (rigthChild != null) { 67 rigthChild.preOrderVisit(); 68 } // Of if 69 }// Of preOrderVisit 70 71 /** 72 * In-order visit. 73 */ 74 public void inOrderVisit() { 75 if (leftChild != null) { 76 leftChild.inOrderVisit(); 77 } // Of if 78 79 System.out.println("" + value + ""); 80 81 if (rigthChild != null) { 82 rigthChild.inOrderVisit(); 83 } // Of if 84 }// Of inOrderVisit 85 86 /** 87 * Post-order Visit. 88 */ 89 public void postOrderVisit() { 90 if (leftChild != null) { 91 leftChild.postOrderVisit(); 92 } // Of if 93 94 if (rigthChild != null) { 95 rigthChild.postOrderVisit(); 96 } // Of if 97 98 System.out.println("" + value + ""); 99 }// Of postVisit 100 101 /** 102 * Get the depth of the binary tree 103 * 104 * @return The depth. It is 1if there is only one node. 105 */ 106 public int getDepth() { 107 if ((leftChild == null) && (rigthChild == null)) { 108 return 1; 109 } // Of if 110 111 // The depth of the left child 112 int tempLeftDepth = 0; 113 if (leftChild != null) { 114 tempLeftDepth = leftChild.getDepth(); 115 } // Of if 116 117 // The depth of the right child 118 int tempRightDepth = 0; 119 if (rigthChild != null) { 120 tempRightDepth = rigthChild.getDepth(); 121 } // Of if 122 123 // The depth should increase by 1 124 if (tempLeftDepth >= tempRightDepth) { 125 return tempLeftDepth + 1; 126 } else { 127 return tempRightDepth + 1; 128 } // Of if 129 } // Of getDepth 130 131 /** 132 * Get the number of nodes. 133 * 134 * @return The number of nodes. 135 */ 136 public int getNumNodes() { 137 // It is a leaf. 138 if ((leftChild == null) && (rigthChild == null)) { 139 return 1; 140 } // Of if 141 142 // The number of nodes of the left child. 143 int tempLeftNodes = 0; 144 if (leftChild != null) { 145 tempLeftNodes = leftChild.getNumNodes(); 146 } // Of if 147 148 // The number of nodes of the rigth child. 149 int tempRightNodes = 0; 150 if (rigthChild != null) { 151 tempRightNodes = rigthChild.getNumNodes(); 152 } // of if 153 154 // The total number of nodes. 155 return tempLeftNodes + tempRightNodes + 1; 156 }// Of getNumNodes 157 158 public static void main(String[] args) { 159 BinaryCharTree tempTree = manualConstructTree(); 160 System.out.println("\r\nPreorder visit:"); 161 tempTree.preOrderVisit(); 162 System.out.println("\r\nIn-Order visit:"); 163 tempTree.inOrderVisit(); 164 System.out.println("\r\nPost-Order visit:"); 165 tempTree.postOrderVisit(); 166 167 System.out.println("\r\n\r\nThe depth is: " + tempTree.getDepth()); 168 tempTree.getDepth(); 169 System.out.println("The number of nodes is: " + tempTree.getNumNodes()); 170 }// Of main 171 }// Of BinaryCharTree


浙公网安备 33010602011771号