树型结构
概念
树是一种
非线性
的数据结构,它是由
n
(
n>=0
)个有限结点组成一个具有层次关系的集合。
把它叫做树是因为它看
起来像一棵倒挂的树,也就是说它是根朝上,而叶朝下的
。它具有以下的特点:
1.有一个特殊的结点,称为根结点,根结点没有前驱结点
2.除根结点外,其余结点被分成
M(M > 0)
个互不相交的集合
T1
、
T2
、
......
、
Tm
,其中每一个集合
Ti (1 <= i <= m) 又是一棵与树类似的子树。每棵子树的根结点有且只有一个前驱,可以有
0
个或多个后继
3.树是递归定义的。
![](https://i-blog.csdnimg.cn/direct/f3491509a59741a69bbf86a8ef2612c6.png)
结点的度
:一个结点含有子树的个数称为该结点的度;
树的度
:一棵树中,所有结点度的最大值称为树的度;
叶子结点或终端结点
:度为
0
的结点称为叶结点;
双亲结点或父结点
:若一个结点含有子结点,则这个结点称为其子结点的父结点;
孩子结点或子结点
:一个结点含有的子树的根结点称为该结点的子结点;
根结点
:一棵树中,没有双亲结点的结点;
结点的层次
:从根开始定义起,根为第
1
层,根的子结点为第
2
层,以此类推
树的高度或深度
:树中结点的最大层次;
非终端结点或分支结点
:度不为
0
的结点;
兄弟结点
:具有相同父结点的结点互称为兄弟结点;
堂兄弟结点
:双亲在同一层的结点互为堂兄弟;
结点的祖先
:从根到该结点所经分支上的所有结点;
子孙
:以某结点为根的子树中任一结点都称为该结点的子孙。
森林
:由
m
(
m>=0
)棵互不相交的树组成的集合称为森林。
二叉树
概念
一棵二叉树是结点的一个有限集合,该集合:
1.
或者为空
2.
或者是由
一个根节
点加上两棵别称为
左子树
和
右子树
的二叉树组成。
![](https://i-blog.csdnimg.cn/direct/715d0c35ea684a9792dafc5dad73cf20.png)
两种特殊的二叉树
1.
满二叉树
:
一棵二叉树,如果
每层的结点数都达到最大值,则这棵二叉树就是满二叉树
。也就是说,
如果一棵
二叉树的层数为
K
,且结点总数是 2^k-1
,则它就是满二叉树
。
2.
完全二叉树
:
完全二叉树是效率很高的数据结构,完全二叉树是由满二叉树而引出来的。对于深度为
K
的,有
n个结点的二叉树,当且仅当其每一个结点都与深度为K
的满二叉树中编号从
0
至
n-1
的结点一一对应时称之为完全二叉树。 要注意的是满二叉树是一种特殊的完全二叉树。
![](https://i-blog.csdnimg.cn/direct/238a7cb8cc5f41c2b2c482411fe66e10.png)
二叉树的性质
1.
若规定
根结点的层数为
1
,则一棵
非空二叉树的第
i
层上最多有2^i-1
(i>0)
个结点
2.
若规定只有
根结点的二叉树的深度为
1
,则
深度为
K的二叉树的最大结点数是 2^k-1(k>=0)
3.
对任何一棵二叉树
,
如果其
叶结点个数为
n0,
度为
2
的非叶结点个数为
n2,
则有
n0
=
n2
+
1
4.
具有
n
个结点的完全二叉树的深度
k
为log2(n+1)
上取整
5.
对于具有
n
个结点的完全二叉树
,如果按照
从上至下从左至右的顺序对所有节点从
0
开始编号
,则对于
序号为
i
的结点有
:
若
i>0
,
双亲序号:
(i-1)/2
;
i=0
,
i
为根结点编号
,无双亲结点
若
2i+1<n
,左孩子序号:
2i+1
,否则无左孩子
若
2i+2<n
,右孩子序号:
2i+2
,否则无右孩子
二叉树的遍历
NLR
:前序遍历
(Preorder Traversal
亦称先序遍历
)——
访问根结点
--->
根的左子树
--->
根的右子树。
LNR
:中序遍历
(Inorder Traversal)——
根的左子树
--->
根节点
--->
根的右子树。
LRN
:后序遍历
(Postorder Traversal)——
根的左子树
--->
根的右子树
--->
根节点。
示例代码如下:
public class BinaryTree {
public static class TreeNode{
public char val;
public TreeNode right;
public TreeNode left;
public TreeNode(char val){
this.val = val;
}
}
public TreeNode CreatTree(){
TreeNode A = new TreeNode('A');
TreeNode B = new TreeNode('B');
TreeNode C = new TreeNode('C');
TreeNode D = new TreeNode('D');
A.right = B;
A.left = C;
B.left = D;
return A;
}
public void preOrder(TreeNode x){
if(x == null){
return;
}
System.out.print(x.val + " ");
preOrder(x.left);
preOrder(x.right);
}
public void inOrder(TreeNode x){
if(x == null){
return;
}
inOrder(x.left);
System.out.print(x.val + " ");
inOrder(x.right);
}
public void postOrder(TreeNode x){
if(x == null){
return;
}
postOrder(x.left);
postOrder(x.right);
System.out.print(x.val + " ");
}
}
二叉树的基本操作
// 获取树中节点的个数
int size(TreeNode root);
// 获取叶子节点的个数
int getLeafNodeCount(TreeNode root);
// 获取第K层节点的个数
int getKLevelNodeCount(TreeNode root);
// 获取二叉树的高度
int getHeight(TreeNode root);
// 检测值为value的元素是否
Node find(TreeNode root, int val);
//层序遍历
void levelOrder(TreeNode root);
// 判断一棵树是不是完全二叉树
boolean isCompleteTree(TreeNode root);
示例代码如下:
public class BinaryTree {
public static class TreeNode{
public char val;
public TreeNode right;
public TreeNode left;
public TreeNode(char val){
this.val = val;
}
}
public TreeNode CreatTree(){
TreeNode A = new TreeNode('A');
TreeNode B = new TreeNode('B');
TreeNode C = new TreeNode('C');
TreeNode D = new TreeNode('D');
A.right = B;
A.left = C;
B.left = D;
return A;
}
public void preOrder(TreeNode x){
if(x == null){
return;
}
System.out.print(x.val + " ");
preOrder(x.left);
preOrder(x.right);
}
public void inOrder(TreeNode x){
if(x == null){
return;
}
inOrder(x.left);
System.out.print(x.val + " ");
inOrder(x.right);
}
public void postOrder(TreeNode x){
if(x == null){
return;
}
postOrder(x.left);
postOrder(x.right);
System.out.print(x.val + " ");
}
public static int usedSize = 0;
public int size(TreeNode x){
if (x == null){
return 0;
}
usedSize++;
size(x.right);
size(x.left);
return usedSize;
}
public int getLeafNodeCount(TreeNode x){
if (x == null){
return 0;
}
if(x.left == null&&x.right == null) {
return 1;
}
return getLeafNodeCount(x.right)+getLeafNodeCount(x.left);
}
public int getKLevelNodeCount(TreeNode root,int k){
if (root == null){
return 0;
}
if (k == 1){
return 1;
}
return getKLevelNodeCount(root.left,k-1)+getKLevelNodeCount(root.right,k-1);
}
public int getHeight(TreeNode root){
if (root == null){
return 0;
}
return Math.max(getHeight(root.left),getHeight(root.right))+1;
}
public TreeNode find(TreeNode root, int val){
if(root == null){
return null;
}
if (root.val == val){
return root;
}
TreeNode FindL = find(root.left,val);
if (FindL != null){
return FindL;
}
TreeNode FindR = find(root.right,val);
if (FindR != null){
return FindR;
}
return null;
}
public List<List<Character>> levelOrder(TreeNode root){
List<List<Character>> cur = new ArrayList<>();
if(root == null){
return cur;
}
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.offer(root);
while(!queue.isEmpty()){
int a = queue.size();
List<Character> list = new ArrayList<>();
while(a != 0){
TreeNode nrc = queue.poll();
list.add(nrc.val);
if (nrc.right != null){
queue.offer(nrc.right);
}
if (nrc.left != null){
queue.offer(nrc.left);
}
a--;
}
cur.add(list);
}
return cur;
}
public boolean isCompleteTree(TreeNode root){
Queue<TreeNode> queue = new LinkedList<>();
if(root != null){
queue.offer(root);
}
while(!queue.isEmpty()) {
TreeNode nur = queue.poll();
if (nur != null) {
queue.offer(nur.right);
queue.offer(nur.left);
} else {
break;
}
}
while(!queue.isEmpty()){
TreeNode nur = queue.poll();
if(nur != null){
return false;
}
}
return true;
}
}