树型结构是一类重要的非线性数据结构。
一、树的定义与基本术语
1.1 定义
树是n(n大于等于0)个结点的有限集。树是非线性数据结构,以分支关系定义的层次结构。
在树形结构中,树根结点没有前驱结点,其余每个结点有且只有一个前驱结点。叶子结点没有后继结点,其余结点后继结点可以是一个也可以是多个。
1.2 基本术语:
- 结点:含数据项及若干指向子树的分支。
- 结点的度:结点拥有的子树数。
- 树的度:树内各结点的度的最大值。
- 树的深度或高度:树中的结点的最大层次。
二、二叉树
2.1 定义
二叉树是n大于等于0个结点的有限集,它可以是空树,也可以由一个根结点和两颗分别成为左、右子树的互不相同的二叉树构成。
2.2 代码展示
package datastructure.tree;
/**
* @author 前夜
*
*/
public class BinaryCharTree {
/**
* The value in char, the left child and the right child.
*/
char value;
BinaryCharTree leftChild, rightChild;
/**
********************
* The first constructor.
* @param paraName
********************
*/
public BinaryCharTree(char paraName) {
value = paraName;
leftChild = null;
rightChild = null;
}// Of the constructor
/**
********************
* Manually construct a tree which is only for testing.
* @return
********************
*/
public static BinaryCharTree manualConstructTree() {
// Step 1. Construct a tree with only one node.And this node is the root.
BinaryCharTree resultTree = new BinaryCharTree('a');
// Step 2.Construct all nodes.
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 manualConstuctTree
/**
********************
* 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. 获取二叉树的深度
* 1 stand for a node and only have a leaf.
* @return
********************
*/
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. 深度增加1
if (tempLeftDepth >= tempRightDepth) {
return tempLeftDepth + 1;
} else {
return tempRightDepth + 1;
} // Of if
}// Of getDepth
/**
********************
* Get the number of nodes.
* @return
********************
*/
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
return tempLeftNodes + tempRightNodes + 1;
}// Of getNumNodes
/**
********************
* The entrance of the program.
*
* @param args
********************
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
BinaryCharTree tempTree = manualConstructTree();
System.out.println("PreOrder 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 the nodes is: " + tempTree.getNumNodes());
}// Of main
}// Of BinaryCharTree```