二叉树分类
參考 完美二叉樹, 完全二叉樹和完滿二叉樹
1.樹 (Tree)的基本概念
1.1 樹的定義
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) 樹。一棵非空的樹包括一個根結點,還 (很可能) 有多個附加結點,所有結點構成一個多級分層結構。
[注:本文將 node 一律譯為 "結點"(而不是 "節點"),因為joint 或 connection 是節點,而 node 是結點。關于 "結點" 與 "節點" 請自行搜索浙江大學陳水福教授的文章 --"360 度" 解讀如何正確應用 "結點" 與 "節點"]
例如: 【圖片來源: https://upload.wikimedia.org/wikipedia/commons/f/f7/Binary_tree.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 樹的基本術語
| 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)
2.1 什么是二叉樹(Binary Tree)
每個結點至多擁有兩棵子樹 (即二叉樹中不存在度大于 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) 二叉樹。
總結
- 上一篇: 如何判断一个对象是否为jquery对象
- 下一篇: 招商信用卡怎么提前还款 招商银行的信用卡