二叉树

一、二叉树(Binary Tree)是n(n>=0)个结点的有限集合,该集合或者为空集(称为空二叉树),或者由一个根结点和两棵互不相交的、分别称为根结点的左子树和右子树的二叉树组成。如图1就是一棵二叉树

     图1

二叉树的特点:

(1)每个结点最多有两棵子树,所以二叉树中不存在度大于2的结点。

(2)左子树和右子树是由顺序的,次序不能颠倒。

(3)即使树中某结点只有一棵子树,也要区分它是左子树还是右子树。

二叉树具有五种基本形态:

(1)空二叉树;(2)只有一个根结点;(3)根结点只有左子树;(4)根结点只有右子树;(5)根结点既有左子树又有右子树。


二、特殊二叉树

1、斜树:所有的结点都只有左子树的二叉树叫左斜树。所有结点都是只有右子树的二叉树叫右斜树。

2、满二叉树:在一棵二叉树中,如果所有分支结点都存在左子树和右子树,并且所有叶子都在同一层上,这样的二叉树就称为满二叉树,如图2。

图2

满二叉树的特点有:

(1)叶子只能出现在最下一层。出现在其他层就不可能达到平衡。

(2)非叶子结点的度一定是2。

(3)在同样深度的二叉树中,满二叉树的结点个数最多,叶子树最多。

3、完全二叉树:

对一棵具有n个结点的二叉树按层序编号,如果编号为i(1<=i<=n)的结点与同样深度的满二叉树中编号为i的结点在二叉树中位置完全相同,则这棵二叉树称为完全二叉树,如图3。

  图3

完全二叉树的特点:

(1)叶子结点只能出现在最下两层。

(2)最下层的叶子一定集中在左部连续位置

(3)倒数二层,若有叶子结点,一定都在右部连续位置。

(4)如果结点度为1,则该结点只有左孩子,即不存在只有右子树的情况。

(5)同样结点数的二叉树,完全二叉树的深度最小。


注意:满二叉树一定是棵完全二叉树,但完全二叉树不一定是满的。


三、二叉树的性质

1、在二叉树的第i层上至多有2^(i-1)个结点(i>=1)。

2、深度为K的二叉树至多有2^k - 1个结点(k>=1)。

3、对任何一棵二叉树T,如果其终端结点数为n0, 度为2的结点数为n2,则n0 = n2 + 1。

Proof:
Let n = the total number of nodes

B = number of branches
n0, n1, n2 represent the number of nodes with no children, a single child, and two children respectively.
B = n - 1 (since all nodes except the root node come from a single branch)

B = n1 + 2*n2
n = n1+ 2*n2 + 1
n = n0 + n1 + n2
n1+ 2*n2 + 1 = n0 + n1 + n2 ==> n0 = n2 + 1





四、Encoding general trees as binary trees

Each node N in the ordered tree corresponds to a node N' in the binary tree;the left child of N' is the node corresponding to the first child of N, and 

the right child of N' is the node corresponding to N 's next sibling --- that is, the next node in order among the children of the parent of N.




参考:《大话数据结构》、《Data Structures》

原文地址:https://www.cnblogs.com/alantu2018/p/8471612.html