二叉樹分類

參考    完美二叉樹, 徹底二叉樹和完滿二叉樹html

1. 樹 (Tree) 的基本概念node

1.1 樹的定義git

A tree is a (possibly non-linear) data structure made up of nodes or vertices 
and edges without having any cycle. The tree with no nodes is called the null 
or empty tree. A tree that is not empty consists of a root node and potentially 
many levels of additional nodes that form a hierarchy.

樹是由結點或頂點和邊組成的 (多是非線性的) 且不存在着任何環的一種數據結構。沒有結點的樹稱爲空 (null 或 empty) 樹。一棵非空的樹包括一個根結點,還 (極可能) 有多個附加結點,全部結點構成一個多級分層結構。github

[注:本文將 node 一概譯爲 "結點"(而不是 "節點"),由於 joint 或 connection 是節點,而 node 是結點。關於 "結點" 與 "節點" 請自行搜索浙江大學陳水福教授的文章 --"360 度" 解讀如何正確應用 "結點" 與 "節點"]數據結構

例如: 【圖片來源: https://upload.wikimedia.org/wikipedia/commons/f/f7/Binary_tree.svg】svg

A simple unordered tree; in this diagram, the node labeled 7 has two children, 
labeled 2 and 6, and one parent, labeled 2. The root node, at the top, 
has no parent. 上圖是一棵無序的樹示例。在上圖中,標號爲7的結點有兩個孩子,分別是標號爲2和6的結點。
根結點,在最頂端,它沒有雙親。

1.2 樹的基本術語post

Root The top node in a tree. 樹的頂端結點
Child A node directly connected to another node when moving away from the Root. 孩子 當遠離根 (Root) 的時候,直接鏈接到另一個結點的結點被稱之爲孩子 (Child); 
Parent The converse notion of a child. 雙親 相應地,另一個結點稱爲孩子 (child) 的雙親 (parent)。
Siblings A group of nodes with the same parent. 兄弟 具備同一個雙親 (Parent) 的孩子 (Child) 之間互稱爲兄弟 (Sibling)。
Ancestor A node reachable by repeated proceeding from child to parent. 祖先 結點的祖先 (Ancestor) 是從根(Root)到該結點所經分支 (Branch) 上的全部結點。
Descendant A node reachable by repeated proceeding from parent to child. 子孫 反之,以某結點爲根的子樹中的任一結點都稱爲該結點的子孫 (Ancestor)。
Leaf A node with no children. 葉子(終端結點) 沒有孩子的結點 (也就是度爲 0 的結點) 稱爲葉子 (Leaf) 或終端結點。
Branch A node with at least one child. 分支 (非終端結點) 至少有一個孩子的結點稱爲分支 (Branch) 或非終端結點。
Degree The number of sub trees of a node. 結點所擁有的子樹個數稱爲結點的度 (Degree)。
Edge The connection between one node and another. 一個結點和另外一個結點之間的鏈接被稱之爲邊 (Edge)。
Path A sequence of nodes and edges connecting a node with a descendant. 路徑 鏈接結點和其後代的結點之間的 (結點, 邊) 的序列。 
Level The level of a node is defined by 0 + (the number of connections between the node and the root). 層次 結點的層次 (Level) 從根 (Root) 開始定義起,根爲第 0 層,根的孩子爲第 1 層。以此類推,若某結點在第 i 層,那麼其子樹的根就在第 i+1 層。
Height of node The height of a node is the number of edges on the longest path between that node and a leaf. 結點的高度 結點的高度是該結點和某個葉子之間存在的最長路徑上的邊的個數。 
Height of tree The height of a tree is the height of its root node. 樹的高度 樹的高度是其根結點的高度。 
Depth of node
The depth of a node is the number of edges from the tree's root node to the node. 結點的深度 結點的深度是從樹的根結點到該結點的邊的個數。(注:樹的深度指的是樹中結點的最大層次。)
Forest A forest is a set of n ≥ 0 disjoint trees. 森林 森林是 n(>=0) 棵互不相交的樹的集合。

 

2 二叉樹(Binary Tree)this

2.1 什麼是二叉樹(Binary Tree)url

每一個結點至多擁有兩棵子樹 (即二叉樹中不存在度大於 2 的結點),而且,二叉樹的子樹有左右之分,其次序不能任意顛倒。翻譯

2.2 二叉樹的性質

(1)若二叉樹的層次從 0 開始,則在二叉樹的第 i 層至多有 2^i 個結點 (i>=0)。

(2)高度爲 k 的二叉樹最多有 2^(k+1) - 1 個結點 (k>=-1)。 (空樹的高度爲 - 1)

(3)對任何一棵二叉樹,若是其葉子結點 (度爲 0) 數爲 m, 度爲 2 的結點數爲 n, 則 m = n + 1。

2.3 完美二叉樹 (Perfect Binary Tree)

A Perfect Binary Tree(PBT) is a tree with all leaf nodes at the same depth. 
All internal nodes have degree 2.

一個深度爲 k(>=-1) 且有 2^(k+1) - 1 個結點的二叉樹稱爲完美二叉樹。 (注: 國內的數據結構教材大多翻譯爲 "滿二叉樹 ")

例如:

2.4 徹底二叉樹 (Complete Binary Tree)

A Complete Binary Tree (CBT) is a binary tree in which every level, 
except possibly the last, is completely filled, and all nodes 
are as far left as possible.

換句話說,徹底二叉樹從根結點到倒數第二層知足完美二叉樹,最後一層能夠不徹底填充,其葉子結點都靠左對齊。

例如:

2.5 完滿二叉樹 (Full Binary Tree)

A Full Binary Tree (FBT) is a tree in which every node other than the leaves has two children.

換句話說,全部非葉子結點的度都是 2。(只要你有孩子,你就必然是有兩個孩子。) 

注:Full Binary Tree 又叫作 Strictly Binary Tree。

例如:

2.6 完美 (Perfect) 二叉樹 v.s. 徹底 (Complete) 二叉樹

(1) 一棵完美 (Perfect) 二叉樹看起來是這個樣兒的, 【圖 2.6.1】

(2) 那麼,將編號爲 15, 14, ..., 9 的葉子結點從右到左依次拿掉或者拿掉部分,則是一棵徹底 (Complete) 二叉樹,

例如,將上圖中的編號爲 15, 14, 13, 12, 11 葉子結點都拿掉 (從右到左的順序), 【圖 2.6.2】

(3) 下圖就不是一棵徹底 (Complete) 二叉樹,【圖 2.6.3】,

若是將編號 11(K) 結點從編號 6(E) 的左兒子位置移動到編號 5(E) 的右兒子位置,則變成一棵徹底 (Complete) 二叉樹。

注: 圖 2.6.1, 2.6.2 和 2.6.3 均來自:http://alrightchiu.github.io/SecondRound/binary-tree-introjian-jie.html, 可是,其將 Full Binary Tree 當作就是 Perfect Binary Tree, 我認爲是不正確的,特此說明。

特別說明: 其實,理解徹底 (Complete) 二叉樹能夠藉助於棧 (stack) 的思想。 例如,把圖 2.6.1 中的完美 (Perfect) 二叉樹的全部結點按照編號 1, 2, 3, ..., 15 依次入棧 (push)。 那麼,對棧的每一次出棧 (pop) 操做後,棧裏保存的結點集對應到圖 2.6.1 上去都是一棵徹底 (Complete) 二叉樹。

2.7 徹底 (Complete) 二叉樹 v.s. 完滿 (Full) 二叉樹

【截圖來源:http://courses.cs.vt.edu/~cs3114/Fall09/wmcquain/Notes/T03a.BinaryTreeTheorems.pdf】

2.8 完滿 (Full) 二叉樹 v.s. 徹底 (Complete) 二叉樹 v.s. 完美 (Perfect) 二叉樹

【圖片來源: http://www.csie.ntnu.edu.tw/~u91029/BinaryTree2.png】

 

3. 總結 (下表參考來源)

完美二叉樹 Perfect Binary Tree

Every node except the leaf nodes have two children and every level (last level too) is completely filled. 除了葉子結點以外的每個結點都有兩個孩子,每一層 (固然包含最後一層) 都被徹底填充。

徹底二叉樹 Complete Binary Tree Every level except the last level is completely filled and all the nodes are left justified. 除了最後一層以外的其餘每一層都被徹底填充,而且全部結點都保持向左對齊。
完滿二叉樹 Full/Strictly Binary Tree Every node except the leaf nodes have two children. 除了葉子結點以外的每個結點都有兩個孩子結點。
  • 完美 (Perfect) 二叉樹必定是徹底 (Complete) 二叉樹,但徹底 (Complete) 二叉樹不必定是完美 (Perfect) 二叉樹。
  • 完美 (Perfect) 二叉樹必定是完滿 (Full) 二叉樹,但完滿 (Full) 二叉樹不必定是完美 (Perfect) 二叉樹。
  • 徹底 (Complete) 二叉樹多是完滿 (Full) 二叉樹,完滿 (Full) 二叉樹也多是徹底 (Complete) 二叉樹。
  • 既是徹底 (Complete) 二叉樹又是完滿 (Full) 二叉樹     不必定就是完美 (Perfect) 二叉樹。
相關文章
相關標籤/搜索