日撸 Java 三百行day21

第 21 天: 二叉树的深度遍历的递归实现

今天的任务主要是递归,感觉递归之前接触相对较少,稍微陌生一点,多看多练吧。

一、概念
二叉树:树的度为2(树中每个节点最多有两个度),树的度就是树中最大节点度。
遍历:把每个数据访问(visit)一遍,访问:打印、比较、修改,可以是各种复杂计算或者创建/删除节点。

深度:从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度
二、遍历
1、先序遍历:先访问根节点、再访问左子树、再访问右子树
2、中序遍历:先访问左子树、再访问根节点、再访问右子树
3、后序遍历:先访问左子树、再访问右子树、再访问左子树

package day17;

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

	public BinaryCharTree(char paraName) {
		value = paraName;
		rightChild = null;
		leftChild = null;
	}// Of the constructor

	/**
	 *********************
	 * Manually construct a tree. Only for testing.
	 *********************
	 */
	public static BinaryCharTree manualConstructTree() {
		// Step 1. Construct a tree with only one node.
		BinaryCharTree resultTree = new BinaryCharTree('a');

		// Step 2. Construct all nodes. The first node is the root.
		// BinaryCharTreeNode tempTreeA = resultTree.root;
		BinaryCharTree tempTreeB = new BinaryCharTree('b');
		BinaryCharTree tempTreeC = new BinaryCharTree('c');
		BinaryCharTree tempTreeD = new BinaryCharTree('d');
		BinaryCharTree tempTreeE = new BinaryCharTree('e');
		BinaryCharTree tempTreeF = new BinaryCharTree('f');
		BinaryCharTree tempTreeG = new BinaryCharTree('g');

		// Step 3. Link all nodes.
		resultTree.leftChild = tempTreeB;
		resultTree.rightChild = tempTreeC;
		tempTreeB.rightChild = tempTreeD;
		tempTreeC.leftChild = tempTreeE;
		tempTreeD.leftChild = tempTreeF;
		tempTreeD.rightChild = tempTreeG;

		return resultTree;
	}// Of manualConstructTree

	/**
	 *********************
	 * Pre-order visit.
	 *********************
	 */
	public void preOrderVisit() {
		System.out.print("" + value + " ");

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

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

	/**
	 *********************
	 * In-order visit.
	 *********************
	 */
	public void inOrderVisit() {
		if (leftChild != null) {
			leftChild.inOrderVisit();
		} // Of if

		System.out.print("" + value + " ");

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

	/**
	 *********************
	 * Post-order visit.
	 *********************
	 */
	public void postOrderVisit() {
		if (leftChild != null) {
			leftChild.postOrderVisit();
		} // Of if

		if (rightChild != null) {
			rightChild.postOrderVisit();
		} // Of if

		System.out.print("" + value + " ");
	}// Of postOrderVisit

	/**
	 *********************
	 * Get the depth of the binary tree.
	 * 
	 * @return The depth. It is 1 if there is only one node, i.e., the root.
	 *********************
	 */
	public int getDepth() {
		// It is a leaf.
		if ((leftChild == null) && (rightChild == null)) {
			return 1;
		} // Of if

		// The depth of the left child.
		int tempLeftDepth = 0;
		if (leftChild != null) {
			tempLeftDepth = leftChild.getDepth();
		} // Of if

		// The depth of the right child.
		int tempRightDepth = 0;
		if (rightChild != null) {
			tempRightDepth = rightChild.getDepth();
		} // Of if

		// The depth should increment by 1.
		if (tempLeftDepth >= tempRightDepth) {
			return tempLeftDepth + 1;
		} else {
			return tempRightDepth + 1;
		} // Of if
	}// Of getDepth

	/**
	 *********************
	 * Get the number of nodes.
	 * 
	 * @return The number of nodes.
	 *********************
	 */
	public int getNumNodes() {
		// It is a leaf.
		if ((leftChild == null) && (rightChild == null)) {
			return 1;
		} // Of if

		// The number of nodes of the left child.
		int tempLeftNodes = 0;
		if (leftChild != null) {
			tempLeftNodes = leftChild.getNumNodes();
		} // Of if

		// The number of nodes of the right child.
		int tempRightNodes = 0;
		if (rightChild != null) {
			tempRightNodes = rightChild.getNumNodes();
		} // Of if

		// The total number of nodes.
		return tempLeftNodes + tempRightNodes + 1;
	}// Of getNumNodes

	/**
	 *********************
	 * The entrance of the program.
	 * 
	 * @param args Not used now.
	 *********************
	 */
	public static void main(String args[]) {
		BinaryCharTree tempTree = manualConstructTree();
		System.out.println("Preorder visit:");
		tempTree.preOrderVisit();
		System.out.println("\nIn-order visit:");
		tempTree.inOrderVisit();
		System.out.println("\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());
	}// Of main

}// Of BinaryCharTree

代码中用的每一个方法几乎都是递归,有点绕,好在自己建立的那棵树规模比较小,慢慢推导理解出来了,但是感觉这个有一定的方法,需要站在相对宏观的角度进行思考,这样才能相对明白的写出来。三种遍历方式,差别就在于输出语句的位置,都是递归调用。之后对于求深度我还有所困惑,推了几遍,理解了,但是感觉自己有点写不出来。

你可能感兴趣的:(java)