今天是学习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. Codepackage datastructure;
public class BinaryCharTree {
char value;
BinaryCharTree leftChild;
BinaryCharTree rightChild;
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
public static void main(String args[]) {
BinaryCharTree tempTree = manualConstructTree();
System.out.println("rnPreorder visit:");
tempTree.preOrderVisit();
System.out.println("rnIn-order visit:");
tempTree.inOrderVisit();
System.out.println("rnPost-order visit:");
tempTree.postOrderVisit();
System.out.println("rnrnThe depth is: " + tempTree.getDepth());
System.out.println("The number of nodes is: " + tempTree.getNumNodes());
}// Of main
}
运行结果:



