hello-algo/docs/chapter_tree/binary_tree.md

663 lines
20 KiB
Markdown
Raw Normal View History

2023-02-28 20:03:53 +08:00
# 二叉树
2022-11-22 17:47:26 +08:00
<u>二叉树binary tree</u>是一种非线性数据结构,代表“祖先”与“后代”之间的派生关系,体现了“一分为二”的分治逻辑。与链表类似,二叉树的基本单元是节点,每个节点包含值、左子节点引用和右子节点引用。
2022-11-22 17:47:26 +08:00
=== "Python"
2022-11-22 17:47:26 +08:00
```python title=""
class TreeNode:
"""二叉树节点类"""
def __init__(self, val: int):
2023-09-20 01:53:44 +08:00
self.val: int = val # 节点值
self.left: TreeNode | None = None # 左子节点引用
self.right: TreeNode | None = None # 右子节点引用
2022-11-22 17:47:26 +08:00
```
=== "C++"
2022-12-03 01:31:29 +08:00
```cpp title=""
/* 二叉树节点结构体 */
struct TreeNode {
int val; // 节点值
TreeNode *left; // 左子节点指针
TreeNode *right; // 右子节点指针
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
```
=== "Java"
```java title=""
/* 二叉树节点类 */
class TreeNode {
int val; // 节点值
TreeNode left; // 左子节点引用
TreeNode right; // 右子节点引用
TreeNode(int x) { val = x; }
}
```
=== "C#"
```csharp title=""
/* 二叉树节点类 */
class TreeNode(int? x) {
public int? val = x; // 节点值
public TreeNode? left; // 左子节点引用
public TreeNode? right; // 右子节点引用
}
```
=== "Go"
2022-12-03 01:31:29 +08:00
```go title=""
/* 二叉树节点结构体 */
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
/* 构造方法 */
func NewTreeNode(v int) *TreeNode {
return &TreeNode{
Left: nil, // 左子节点指针
Right: nil, // 右子节点指针
Val: v, // 节点值
}
}
2022-12-03 01:31:29 +08:00
```
=== "Swift"
```swift title=""
/* 二叉树节点类 */
class TreeNode {
var val: Int // 节点值
var left: TreeNode? // 左子节点引用
var right: TreeNode? // 右子节点引用
init(x: Int) {
val = x
}
}
```
=== "JS"
2022-12-03 01:31:29 +08:00
2023-02-08 04:27:55 +08:00
```javascript title=""
/* 二叉树节点类 */
2023-10-01 19:33:53 +08:00
class TreeNode {
val; // 节点值
left; // 左子节点指针
right; // 右子节点指针
constructor(val, left, right) {
this.val = val === undefined ? 0 : val;
this.left = left === undefined ? null : left;
this.right = right === undefined ? null : right;
}
2022-12-05 00:13:48 +08:00
}
2022-12-03 01:31:29 +08:00
```
=== "TS"
2022-12-03 01:31:29 +08:00
```typescript title=""
/* 二叉树节点类 */
class TreeNode {
val: number;
left: TreeNode | null;
right: TreeNode | null;
constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
this.val = val === undefined ? 0 : val; // 节点值
this.left = left === undefined ? null : left; // 左子节点引用
this.right = right === undefined ? null : right; // 右子节点引用
}
}
2022-12-03 01:31:29 +08:00
```
=== "Dart"
```dart title=""
/* 二叉树节点类 */
class TreeNode {
int val; // 节点值
TreeNode? left; // 左子节点引用
TreeNode? right; // 右子节点引用
TreeNode(this.val, [this.left, this.right]);
}
```
=== "Rust"
```rust title=""
use std::rc::Rc;
use std::cell::RefCell;
/* 二叉树节点结构体 */
struct TreeNode {
val: i32, // 节点值
left: Option<Rc<RefCell<TreeNode>>>, // 左子节点引用
right: Option<Rc<RefCell<TreeNode>>>, // 右子节点引用
}
impl TreeNode {
/* 构造方法 */
fn new(val: i32) -> Rc<RefCell<Self>> {
Rc::new(RefCell::new(Self {
val,
left: None,
right: None
}))
}
}
```
2022-12-03 01:31:29 +08:00
=== "C"
```c title=""
/* 二叉树节点结构体 */
typedef struct TreeNode {
int val; // 节点值
int height; // 节点高度
struct TreeNode *left; // 左子节点指针
struct TreeNode *right; // 右子节点指针
} TreeNode;
/* 构造函数 */
TreeNode *newTreeNode(int val) {
TreeNode *node;
node = (TreeNode *)malloc(sizeof(TreeNode));
node->val = val;
node->height = 0;
node->left = NULL;
node->right = NULL;
return node;
}
2022-12-03 01:31:29 +08:00
```
=== "Kotlin"
```kotlin title=""
/* 二叉树节点类 */
class TreeNode(val _val: Int) { // 节点值
val left: TreeNode? = null // 左子节点引用
val right: TreeNode? = null // 右子节点引用
}
```
=== "Ruby"
```ruby title=""
```
2023-02-01 22:03:04 +08:00
=== "Zig"
```zig title=""
```
每个节点都有两个引用(指针),分别指向<u>左子节点left-child node</u><u>右子节点right-child node</u>,该节点被称为这两个子节点的<u>父节点parent node</u>。当给定一个二叉树的节点时,我们将该节点的左子节点及其以下节点形成的树称为该节点的<u>左子树left subtree</u>,同理可得<u>右子树right subtree</u>
2022-11-22 17:47:26 +08:00
**在二叉树中,除叶节点外,其他所有节点都包含子节点和非空子树**。如下图所示,如果将“节点 2”视为父节点则其左子节点和右子节点分别是“节点 4”和“节点 5”左子树是“节点 4 及其以下节点形成的树”,右子树是“节点 5 及其以下节点形成的树”。
![父节点、子节点、子树](binary_tree.assets/binary_tree_definition.png)
2022-11-22 17:47:26 +08:00
## 二叉树常见术语
2022-11-22 17:47:26 +08:00
二叉树的常用术语如下图所示。
2022-11-22 17:47:26 +08:00
- <u>根节点root node</u>:位于二叉树顶层的节点,没有父节点。
- <u>叶节点leaf node</u>:没有子节点的节点,其两个指针均指向 `None`
- <u>edge</u>:连接两个节点的线段,即节点引用(指针)。
- 节点所在的<u>level</u>:从顶至底递增,根节点所在层为 1 。
- 节点的<u>degree</u>:节点的子节点的数量。在二叉树中,度的取值范围是 0、1、2 。
- 二叉树的<u>高度height</u>:从根节点到最远叶节点所经过的边的数量。
- 节点的<u>深度depth</u>:从根节点到该节点所经过的边的数量。
- 节点的<u>高度height</u>:从距离该节点最远的叶节点到该节点所经过的边的数量。
2022-11-22 17:47:26 +08:00
![二叉树的常用术语](binary_tree.assets/binary_tree_terminology.png)
2022-11-22 17:47:26 +08:00
2023-08-29 20:57:37 +08:00
!!! tip
2022-12-18 15:59:34 +08:00
请注意,我们通常将“高度”和“深度”定义为“经过的边的数量”,但有些题目或教材可能会将其定义为“经过的节点的数量”。在这种情况下,高度和深度都需要加 1 。
2022-12-18 15:59:34 +08:00
## 二叉树基本操作
2022-11-22 17:47:26 +08:00
### 初始化二叉树
与链表类似,首先初始化节点,然后构建引用(指针)。
2022-11-22 17:47:26 +08:00
=== "Python"
2022-11-22 17:47:26 +08:00
```python title="binary_tree.py"
# 初始化二叉树
# 初始化节点
n1 = TreeNode(val=1)
n2 = TreeNode(val=2)
n3 = TreeNode(val=3)
n4 = TreeNode(val=4)
n5 = TreeNode(val=5)
# 构建节点之间的引用(指针)
n1.left = n2
n1.right = n3
n2.left = n4
n2.right = n5
2022-11-22 17:47:26 +08:00
```
=== "C++"
```cpp title="binary_tree.cpp"
/* 初始化二叉树 */
// 初始化节点
TreeNode* n1 = new TreeNode(1);
TreeNode* n2 = new TreeNode(2);
TreeNode* n3 = new TreeNode(3);
TreeNode* n4 = new TreeNode(4);
TreeNode* n5 = new TreeNode(5);
// 构建节点之间的引用(指针)
n1->left = n2;
n1->right = n3;
n2->left = n4;
n2->right = n5;
```
=== "Java"
```java title="binary_tree.java"
// 初始化节点
TreeNode n1 = new TreeNode(1);
TreeNode n2 = new TreeNode(2);
TreeNode n3 = new TreeNode(3);
TreeNode n4 = new TreeNode(4);
TreeNode n5 = new TreeNode(5);
// 构建节点之间的引用(指针)
n1.left = n2;
n1.right = n3;
n2.left = n4;
n2.right = n5;
```
=== "C#"
```csharp title="binary_tree.cs"
/* 初始化二叉树 */
// 初始化节点
TreeNode n1 = new(1);
TreeNode n2 = new(2);
TreeNode n3 = new(3);
TreeNode n4 = new(4);
TreeNode n5 = new(5);
// 构建节点之间的引用(指针)
n1.left = n2;
n1.right = n3;
n2.left = n4;
n2.right = n5;
```
=== "Go"
```go title="binary_tree.go"
/* 初始化二叉树 */
// 初始化节点
n1 := NewTreeNode(1)
n2 := NewTreeNode(2)
n3 := NewTreeNode(3)
n4 := NewTreeNode(4)
n5 := NewTreeNode(5)
// 构建节点之间的引用(指针)
n1.Left = n2
n1.Right = n3
n2.Left = n4
n2.Right = n5
```
=== "Swift"
```swift title="binary_tree.swift"
// 初始化节点
let n1 = TreeNode(x: 1)
let n2 = TreeNode(x: 2)
let n3 = TreeNode(x: 3)
let n4 = TreeNode(x: 4)
let n5 = TreeNode(x: 5)
// 构建节点之间的引用(指针)
n1.left = n2
n1.right = n3
n2.left = n4
n2.right = n5
```
=== "JS"
2022-12-03 01:31:29 +08:00
2023-02-08 04:27:55 +08:00
```javascript title="binary_tree.js"
2022-12-04 12:26:09 +08:00
/* 初始化二叉树 */
// 初始化节点
2022-12-04 12:26:09 +08:00
let n1 = new TreeNode(1),
n2 = new TreeNode(2),
n3 = new TreeNode(3),
n4 = new TreeNode(4),
n5 = new TreeNode(5);
// 构建节点之间的引用(指针)
2022-12-04 12:26:09 +08:00
n1.left = n2;
n1.right = n3;
n2.left = n4;
n2.right = n5;
2022-12-03 01:31:29 +08:00
```
=== "TS"
2022-12-03 01:31:29 +08:00
```typescript title="binary_tree.ts"
/* 初始化二叉树 */
// 初始化节点
let n1 = new TreeNode(1),
n2 = new TreeNode(2),
n3 = new TreeNode(3),
n4 = new TreeNode(4),
n5 = new TreeNode(5);
// 构建节点之间的引用(指针)
n1.left = n2;
n1.right = n3;
n2.left = n4;
n2.right = n5;
2022-12-03 01:31:29 +08:00
```
=== "Dart"
2022-12-03 01:31:29 +08:00
```dart title="binary_tree.dart"
2022-12-23 15:42:02 +08:00
/* 初始化二叉树 */
// 初始化节点
2022-12-23 15:42:02 +08:00
TreeNode n1 = new TreeNode(1);
TreeNode n2 = new TreeNode(2);
TreeNode n3 = new TreeNode(3);
TreeNode n4 = new TreeNode(4);
TreeNode n5 = new TreeNode(5);
// 构建节点之间的引用(指针)
2022-12-23 15:42:02 +08:00
n1.left = n2;
n1.right = n3;
n2.left = n4;
n2.right = n5;
2022-12-03 01:31:29 +08:00
```
=== "Rust"
2023-02-01 22:03:04 +08:00
```rust title="binary_tree.rs"
// 初始化节点
let n1 = TreeNode::new(1);
let n2 = TreeNode::new(2);
let n3 = TreeNode::new(3);
let n4 = TreeNode::new(4);
let n5 = TreeNode::new(5);
// 构建节点之间的引用(指针)
n1.borrow_mut().left = Some(n2.clone());
n1.borrow_mut().right = Some(n3);
n2.borrow_mut().left = Some(n4);
n2.borrow_mut().right = Some(n5);
2023-02-01 22:03:04 +08:00
```
=== "C"
```c title="binary_tree.c"
/* 初始化二叉树 */
// 初始化节点
TreeNode *n1 = newTreeNode(1);
TreeNode *n2 = newTreeNode(2);
TreeNode *n3 = newTreeNode(3);
TreeNode *n4 = newTreeNode(4);
TreeNode *n5 = newTreeNode(5);
// 构建节点之间的引用(指针)
n1->left = n2;
n1->right = n3;
n2->left = n4;
n2->right = n5;
```
=== "Kotlin"
```kotlin title="binary_tree.kt"
// 初始化节点
val n1 = TreeNode(1)
val n2 = TreeNode(2)
val n3 = TreeNode(3)
val n4 = TreeNode(4)
val n5 = TreeNode(5)
// 构建节点之间的引用(指针)
n1.left = n2
n1.right = n3
n2.left = n4
n2.right = n5
```
=== "Ruby"
```ruby title="binary_tree.rb"
```
=== "Zig"
2023-07-26 11:00:53 +08:00
```zig title="binary_tree.zig"
2023-07-26 11:00:53 +08:00
```
??? pythontutor "可视化运行"
https://pythontutor.com/render.html#code=class%20TreeNode%3A%0A%20%20%20%20%22%22%22%E4%BA%8C%E5%8F%89%E6%A0%91%E8%8A%82%E7%82%B9%E7%B1%BB%22%22%22%0A%20%20%20%20def%20__init__%28self,%20val%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20%E8%8A%82%E7%82%B9%E5%80%BC%0A%20%20%20%20%20%20%20%20self.left%3A%20TreeNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%B7%A6%E5%AD%90%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%20%20%20%20%20%20%20%20self.right%3A%20TreeNode%20%7C%20None%20%3D%20None%20%23%20%E5%8F%B3%E5%AD%90%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E4%BA%8C%E5%8F%89%E6%A0%91%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E8%8A%82%E7%82%B9%0A%20%20%20%20n1%20%3D%20TreeNode%28val%3D1%29%0A%20%20%20%20n2%20%3D%20TreeNode%28val%3D2%29%0A%20%20%20%20n3%20%3D%20TreeNode%28val%3D3%29%0A%20%20%20%20n4%20%3D%20TreeNode%28val%3D4%29%0A%20%20%20%20n5%20%3D%20TreeNode%28val%3D5%29%0A%20%20%20%20%23%20%E6%9E%84%E5%BB%BA%E8%8A%82%E7%82%B9%E4%B9%8B%E9%97%B4%E7%9A%84%E5%BC%95%E7%94%A8%EF%BC%88%E6%8C%87%E9%92%88%EF%BC%89%0A%20%20%20%20n1.left%20%3D%20n2%0A%20%20%20%20n1.right%20%3D%20n3%0A%20%20%20%20n2.left%20%3D%20n4%0A%20%20%20%20n2.right%20%3D%20n5&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false
### 插入与删除节点
与链表类似,在二叉树中插入与删除节点可以通过修改指针来实现。下图给出了一个示例。
2022-11-22 17:47:26 +08:00
![在二叉树中插入与删除节点](binary_tree.assets/binary_tree_add_remove.png)
2022-11-22 17:47:26 +08:00
=== "Python"
```python title="binary_tree.py"
# 插入与删除节点
p = TreeNode(0)
# 在 n1 -> n2 中间插入节点 P
n1.left = p
p.left = n2
# 删除节点 P
n1.left = n2
```
=== "C++"
```cpp title="binary_tree.cpp"
/* 插入与删除节点 */
TreeNode* P = new TreeNode(0);
// 在 n1 -> n2 中间插入节点 P
n1->left = P;
P->left = n2;
// 删除节点 P
n1->left = n2;
```
=== "Java"
```java title="binary_tree.java"
TreeNode P = new TreeNode(0);
// 在 n1 -> n2 中间插入节点 P
n1.left = P;
P.left = n2;
// 删除节点 P
n1.left = n2;
```
=== "C#"
```csharp title="binary_tree.cs"
/* 插入与删除节点 */
TreeNode P = new(0);
// 在 n1 -> n2 中间插入节点 P
n1.left = P;
P.left = n2;
// 删除节点 P
n1.left = n2;
```
=== "Go"
```go title="binary_tree.go"
/* 插入与删除节点 */
// 在 n1 -> n2 中间插入节点 P
p := NewTreeNode(0)
n1.Left = p
p.Left = n2
// 删除节点 P
n1.Left = n2
```
2022-11-22 17:47:26 +08:00
=== "Swift"
```swift title="binary_tree.swift"
let P = TreeNode(x: 0)
// 在 n1 -> n2 中间插入节点 P
n1.left = P
P.left = n2
// 删除节点 P
n1.left = n2
```
=== "JS"
2022-12-03 01:31:29 +08:00
2023-02-08 04:27:55 +08:00
```javascript title="binary_tree.js"
/* 插入与删除节点 */
2022-12-04 12:26:09 +08:00
let P = new TreeNode(0);
// 在 n1 -> n2 中间插入节点 P
2022-12-04 12:26:09 +08:00
n1.left = P;
P.left = n2;
// 删除节点 P
2022-12-04 12:26:09 +08:00
n1.left = n2;
2022-12-03 01:31:29 +08:00
```
=== "TS"
2022-12-03 01:31:29 +08:00
```typescript title="binary_tree.ts"
/* 插入与删除节点 */
const P = new TreeNode(0);
// 在 n1 -> n2 中间插入节点 P
n1.left = P;
P.left = n2;
// 删除节点 P
n1.left = n2;
2022-12-03 01:31:29 +08:00
```
=== "Dart"
2022-12-03 01:31:29 +08:00
```dart title="binary_tree.dart"
/* 插入与删除节点 */
2022-12-23 15:42:02 +08:00
TreeNode P = new TreeNode(0);
// 在 n1 -> n2 中间插入节点 P
2022-12-23 15:42:02 +08:00
n1.left = P;
P.left = n2;
// 删除节点 P
2022-12-23 15:42:02 +08:00
n1.left = n2;
2022-12-03 01:31:29 +08:00
```
=== "Rust"
2023-02-01 22:03:04 +08:00
```rust title="binary_tree.rs"
let p = TreeNode::new(0);
// 在 n1 -> n2 中间插入节点 P
n1.borrow_mut().left = Some(p.clone());
p.borrow_mut().left = Some(n2.clone());
// 删除节点 p
n1.borrow_mut().left = Some(n2);
2023-02-01 22:03:04 +08:00
```
=== "C"
```c title="binary_tree.c"
/* 插入与删除节点 */
TreeNode *P = newTreeNode(0);
// 在 n1 -> n2 中间插入节点 P
n1->left = P;
P->left = n2;
// 删除节点 P
n1->left = n2;
```
=== "Kotlin"
```kotlin title="binary_tree.kt"
val P = TreeNode(0)
// 在 n1 -> n2 中间插入节点 P
n1.left = P
P.left = n2
// 删除节点 P
n1.left = n2
```
=== "Ruby"
```ruby title="binary_tree.rb"
```
=== "Zig"
2023-07-26 11:00:53 +08:00
```zig title="binary_tree.zig"
2023-07-26 11:00:53 +08:00
```
??? pythontutor "可视化运行"
https://pythontutor.com/render.html#code=class%20TreeNode%3A%0A%20%20%20%20%22%22%22%E4%BA%8C%E5%8F%89%E6%A0%91%E8%8A%82%E7%82%B9%E7%B1%BB%22%22%22%0A%20%20%20%20def%20__init__%28self,%20val%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20%E8%8A%82%E7%82%B9%E5%80%BC%0A%20%20%20%20%20%20%20%20self.left%3A%20TreeNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%B7%A6%E5%AD%90%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%20%20%20%20%20%20%20%20self.right%3A%20TreeNode%20%7C%20None%20%3D%20None%20%23%20%E5%8F%B3%E5%AD%90%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E4%BA%8C%E5%8F%89%E6%A0%91%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E8%8A%82%E7%82%B9%0A%20%20%20%20n1%20%3D%20TreeNode%28val%3D1%29%0A%20%20%20%20n2%20%3D%20TreeNode%28val%3D2%29%0A%20%20%20%20n3%20%3D%20TreeNode%28val%3D3%29%0A%20%20%20%20n4%20%3D%20TreeNode%28val%3D4%29%0A%20%20%20%20n5%20%3D%20TreeNode%28val%3D5%29%0A%20%20%20%20%23%20%E6%9E%84%E5%BB%BA%E8%8A%82%E7%82%B9%E4%B9%8B%E9%97%B4%E7%9A%84%E5%BC%95%E7%94%A8%EF%BC%88%E6%8C%87%E9%92%88%EF%BC%89%0A%20%20%20%20n1.left%20%3D%20n2%0A%20%20%20%20n1.right%20%3D%20n3%0A%20%20%20%20n2.left%20%3D%20n4%0A%20%20%20%20n2.right%20%3D%20n5%0A%0A%20%20%20%20%23%20%E6%8F%92%E5%85%A5%E4%B8%8E%E5%88%A0%E9%99%A4%E8%8A%82%E7%82%B9%0A%20%20%20%20p%20%3D%20TreeNode%280%29%0A%20%20%20%20%23%20%E5%9C%A8%20n1%20-%3E%20n2%20%E4%B8%AD%E9%97%B4%E6%8F%92%E5%85%A5%E8%8A%82%E7%82%B9%20P%0A%20%20%20%20n1.left%20%3D%20p%0A%20%20%20%20p.left%20%3D%20n2%0A%20%20%20%20%23%20%E5%88%A0%E9%99%A4%E8%8A%82%E7%82%B9%20P%0A%20%20%20%20n1.left%20%3D%20n2&cumulative=false&curInstr=37&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false
2022-11-22 17:47:26 +08:00
!!! note
需要注意的是,插入节点可能会改变二叉树的原有逻辑结构,而删除节点通常意味着删除该节点及其所有子树。因此,在二叉树中,插入与删除通常是由一套操作配合完成的,以实现有实际意义的操作。
2022-11-22 17:47:26 +08:00
## 常见二叉树类型
2022-11-22 17:47:26 +08:00
### 完美二叉树
2022-11-22 17:47:26 +08:00
如下图所示,<u>完美二叉树perfect binary tree</u>所有层的节点都被完全填满。在完美二叉树中,叶节点的度为 $0$ ,其余所有节点的度都为 $2$ ;若树的高度为 $h$ ,则节点总数为 $2^{h+1} - 1$ ,呈现标准的指数级关系,反映了自然界中常见的细胞分裂现象。
2022-11-22 17:47:26 +08:00
!!! tip
2022-11-22 17:47:26 +08:00
请注意,在中文社区中,完美二叉树常被称为<u>满二叉树</u>
2022-11-22 17:47:26 +08:00
![完美二叉树](binary_tree.assets/perfect_binary_tree.png)
2022-11-22 17:47:26 +08:00
### 完全二叉树
2022-11-22 17:47:26 +08:00
如下图所示,<u>完全二叉树complete binary tree</u>只有最底层的节点未被填满,且最底层节点尽量靠左填充。
2022-11-22 17:47:26 +08:00
![完全二叉树](binary_tree.assets/complete_binary_tree.png)
2022-11-22 17:47:26 +08:00
### 完满二叉树
如下图所示,<u>完满二叉树full binary tree</u>除了叶节点之外,其余所有节点都有两个子节点。
![完满二叉树](binary_tree.assets/full_binary_tree.png)
### 平衡二叉树
如下图所示,<u>平衡二叉树balanced binary tree</u>中任意节点的左子树和右子树的高度之差的绝对值不超过 1 。
![平衡二叉树](binary_tree.assets/balanced_binary_tree.png)
## 二叉树的退化
2022-12-03 01:31:29 +08:00
下图展示了二叉树的理想结构与退化结构。当二叉树的每层节点都被填满时,达到“完美二叉树”;而当所有节点都偏向一侧时,二叉树退化为“链表”。
2022-12-03 01:31:29 +08:00
2023-07-26 08:59:36 +08:00
- 完美二叉树是理想情况,可以充分发挥二叉树“分治”的优势。
- 链表则是另一个极端,各项操作都变为线性操作,时间复杂度退化至 $O(n)$ 。
2022-12-03 01:31:29 +08:00
![二叉树的最佳结构与最差结构](binary_tree.assets/binary_tree_best_worst_cases.png)
2022-12-03 01:31:29 +08:00
如下表所示,在最佳结构和最差结构下,二叉树的叶节点数量、节点总数、高度等达到极大值或极小值。
<p align="center"><id> &nbsp; 二叉树的最佳结构与最差结构 </p>
2022-12-03 01:31:29 +08:00
| | 完美二叉树 | 链表 |
| --------------------------- | ------------------ | ------- |
| 第 $i$ 层的节点数量 | $2^{i-1}$ | $1$ |
| 高度为 $h$ 的树的叶节点数量 | $2^h$ | $1$ |
| 高度为 $h$ 的树的节点总数 | $2^{h+1} - 1$ | $h + 1$ |
| 节点总数为 $n$ 的树的高度 | $\log_2 (n+1) - 1$ | $n - 1$ |