Day 21 —— Binary Tree
1. Background
今天是学习java的第21天,今天学习的是二叉树。
主要内容有,二叉树的建立,前、中、后序遍历,算二叉树的深度和节点数。
2. Description
2.1 Tree
**树(Tree)**是n(n>=0)个结点的有限集。n=0时称为空树。在任意一颗非空树中:
1)有且仅有一个特定的称为根(Root)的结点;
2)当n>1时,其余结点可分为m(m>0)个互不相交的有限集T1、T2、…、Tn,其中每一个集合本身又是一棵树,并且称为根的子树。
结点子树的根结点为该结点的孩子结点。相应该结点称为孩子结点的双亲结点。
下图是今天所创建的二叉树:
2.2 遍历
树的遍历分为前序遍历,中序遍历,后序遍历。其实这些本质上是相同的,不同的是父亲节点的位置。前序遍历是先显示父亲节点,再读取孩子节点。中序则是父亲节点在中间,后序是父亲节点在孩子节点之后。
3. Code
package datastructure;
public class BinaryCharTree {
/**
* The value of node.
*/
char value;
/**
* 二叉树的左孩子。
*/
BinaryCharTree leftChild;
/**
* 二叉树的右孩子。
*/
BinaryCharTree rightChild;
/**
**************
* The first constructor.
*
* @param paraValue The given value.
**************
*/
public BinaryCharTree(char paraValue) {
value = paraValue;
leftChild = null;
rightChild = null;
} // Of the first construe
public static BinaryCharTree manualConstructTree() {
// step 1. Creat root.
BinaryCharTree resultTree = new BinaryCharTree('a');
// step 2. creat children and linked them.
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');
// Link strat
resultTree.leftChild = tempTreeB;
resultTree.rightChild = tempTreeC;
tempTreeB.rightChild = tempTreeD;
tempTreeC.leftChild = tempTreeE;
tempTreeD.leftChild = tempTreeF;
tempTreeD.rightChild = tempTreeG;
return resultTree;
} // Of manualConstructTree
/**
**************
* 先根、中根、后根遍历树。
**************
*/
public void preOrderVisit() {
System.out.print(value + " ");
if (leftChild != null) {
leftChild.preOrderVisit();
} // Of if
if (rightChild != null) {
rightChild.preOrderVisit();
} // Of if
} // Of preOrderVisit
public void inOrderVisit() {
if (leftChild != null) {
leftChild.inOrderVisit();
} // Of if
System.out.print(value + " ");
if (rightChild != null) {
rightChild.inOrderVisit();
} // Of if
}
public void postOrderVisit() {
if (leftChild != null) {
leftChild.postOrderVisit();
} // Of if
if (rightChild != null) {
rightChild.postOrderVisit();
} // Of if
System.out.print(value + " ");
}
public int getDepth() {
if ((leftChild == null) && (rightChild == null)) {
return 1;
} // Of if
// check left
int tempLeftDepth = 0;
if (leftChild != null) {
tempLeftDepth = leftChild.getDepth();
} // Of if
// Check right
int tempRightDepth = 0;
if (rightChild != null) {
tempRightDepth = rightChild.getDepth();
} // Of if
if (tempLeftDepth >= tempRightDepth) {
return tempLeftDepth + 1;
} else {
return tempRightDepth + 1;
} // Of if
} // Of getDepth
public int getNumNodes() {
if ((leftChild == null) && (rightChild == null)) {
return 1;
} // Of if
int tempLeftNodes = 0;
if (leftChild != null) {
tempLeftNodes = leftChild.getNumNodes();
} // Of if
int tempRightNodes = 0;
if (rightChild != null) {
tempRightNodes = rightChild.getNumNodes();
} // Of if
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("\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());
}// Of main
}
运行结果: