一、二叉樹(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》