2023-04-17 21:00:20 +08:00
|
|
|
---
|
|
|
|
comments: true
|
|
|
|
---
|
|
|
|
|
2023-08-19 22:07:27 +08:00
|
|
|
# 7.3 二叉树数组表示
|
2023-04-17 21:00:20 +08:00
|
|
|
|
|
|
|
在链表表示下,二叉树的存储单元为节点 `TreeNode` ,节点之间通过指针相连接。在上节中,我们学习了在链表表示下的二叉树的各项基本操作。
|
|
|
|
|
2023-08-20 23:27:50 +08:00
|
|
|
那么,我们能否用数组来表示二叉树呢?答案是肯定的。
|
2023-04-17 21:00:20 +08:00
|
|
|
|
2023-08-19 22:07:27 +08:00
|
|
|
## 7.3.1 表示完美二叉树
|
2023-04-17 21:00:20 +08:00
|
|
|
|
2023-07-24 03:03:29 +08:00
|
|
|
先分析一个简单案例。给定一个完美二叉树,我们将所有节点按照层序遍历的顺序存储在一个数组中,则每个节点都对应唯一的数组索引。
|
2023-04-17 21:00:20 +08:00
|
|
|
|
2023-08-22 13:50:12 +08:00
|
|
|
根据层序遍历的特性,我们可以推导出父节点索引与子节点索引之间的“映射公式”:**若节点的索引为 $i$ ,则该节点的左子节点索引为 $2i + 1$ ,右子节点索引为 $2i + 2$** 。图 7-12 展示了各个节点索引之间的映射关系。
|
2023-04-17 21:00:20 +08:00
|
|
|
|
2023-06-17 00:46:08 +08:00
|
|
|
![完美二叉树的数组表示](array_representation_of_tree.assets/array_representation_binary_tree.png)
|
2023-04-17 21:00:20 +08:00
|
|
|
|
2023-08-22 13:50:12 +08:00
|
|
|
<p align="center"> 图 7-12 完美二叉树的数组表示 </p>
|
2023-04-17 21:00:20 +08:00
|
|
|
|
2023-07-24 03:03:29 +08:00
|
|
|
**映射公式的角色相当于链表中的指针**。给定数组中的任意一个节点,我们都可以通过映射公式来访问它的左(右)子节点。
|
2023-04-17 21:00:20 +08:00
|
|
|
|
2023-08-19 22:07:27 +08:00
|
|
|
## 7.3.2 表示任意二叉树
|
2023-04-17 21:00:20 +08:00
|
|
|
|
2023-08-21 19:32:37 +08:00
|
|
|
完美二叉树是一个特例,在二叉树的中间层通常存在许多 $\text{None}$ 。由于层序遍历序列并不包含这些 $\text{None}$ ,因此我们无法仅凭该序列来推测 $\text{None}$ 的数量和分布位置。**这意味着存在多种二叉树结构都符合该层序遍历序列**。
|
|
|
|
|
2023-08-22 13:50:12 +08:00
|
|
|
如图 7-13 所示,给定一个非完美二叉树,上述的数组表示方法已经失效。
|
2023-04-17 21:00:20 +08:00
|
|
|
|
2023-06-17 00:46:08 +08:00
|
|
|
![层序遍历序列对应多种二叉树可能性](array_representation_of_tree.assets/array_representation_without_empty.png)
|
2023-04-17 21:00:20 +08:00
|
|
|
|
2023-08-22 13:50:12 +08:00
|
|
|
<p align="center"> 图 7-13 层序遍历序列对应多种二叉树可能性 </p>
|
2023-04-17 21:00:20 +08:00
|
|
|
|
2023-08-22 13:50:12 +08:00
|
|
|
为了解决此问题,**我们可以考虑在层序遍历序列中显式地写出所有 $\text{None}$** 。如图 7-14 所示,这样处理后,层序遍历序列就可以唯一表示二叉树了。
|
2023-04-17 21:00:20 +08:00
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
=== "Python"
|
2023-04-17 21:00:20 +08:00
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
```python title=""
|
|
|
|
# 二叉树的数组表示
|
|
|
|
# 使用 None 来表示空位
|
|
|
|
tree = [1, 2, 3, 4, None, 6, 7, 8, 9, None, None, 12, None, None, 15]
|
2023-04-17 21:00:20 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
```cpp title=""
|
|
|
|
/* 二叉树的数组表示 */
|
2023-07-19 16:10:06 +08:00
|
|
|
// 使用 int 最大值 INT_MAX 标记空位
|
2023-04-18 20:19:07 +08:00
|
|
|
vector<int> tree = {1, 2, 3, 4, INT_MAX, 6, 7, 8, 9, INT_MAX, INT_MAX, 12, INT_MAX, INT_MAX, 15};
|
2023-04-17 21:00:20 +08:00
|
|
|
```
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
=== "Java"
|
2023-04-17 21:00:20 +08:00
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
```java title=""
|
|
|
|
/* 二叉树的数组表示 */
|
|
|
|
// 使用 int 的包装类 Integer ,就可以使用 null 来标记空位
|
|
|
|
Integer[] tree = { 1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15 };
|
|
|
|
```
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
```csharp title=""
|
|
|
|
/* 二叉树的数组表示 */
|
|
|
|
// 使用 int? 可空类型 ,就可以使用 null 来标记空位
|
|
|
|
int?[] tree = { 1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15 };
|
2023-04-17 21:00:20 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
```go title=""
|
|
|
|
/* 二叉树的数组表示 */
|
|
|
|
// 使用 any 类型的切片, 就可以使用 nil 来标记空位
|
|
|
|
tree := []any{1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15}
|
|
|
|
```
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
```swift title=""
|
|
|
|
/* 二叉树的数组表示 */
|
|
|
|
// 使用 Int? 可空类型 ,就可以使用 nil 来标记空位
|
|
|
|
let tree: [Int?] = [1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15]
|
|
|
|
```
|
|
|
|
|
2023-07-26 15:34:46 +08:00
|
|
|
=== "JS"
|
2023-04-17 21:00:20 +08:00
|
|
|
|
|
|
|
```javascript title=""
|
|
|
|
/* 二叉树的数组表示 */
|
2023-07-24 03:03:29 +08:00
|
|
|
// 使用 null 来表示空位
|
2023-04-17 21:00:20 +08:00
|
|
|
let tree = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];
|
|
|
|
```
|
|
|
|
|
2023-07-26 15:34:46 +08:00
|
|
|
=== "TS"
|
2023-04-17 21:00:20 +08:00
|
|
|
|
|
|
|
```typescript title=""
|
|
|
|
/* 二叉树的数组表示 */
|
2023-07-24 03:03:29 +08:00
|
|
|
// 使用 null 来表示空位
|
2023-04-17 21:00:20 +08:00
|
|
|
let tree: (number | null)[] = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];
|
|
|
|
```
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
=== "Dart"
|
2023-04-17 21:00:20 +08:00
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
```dart title=""
|
2023-04-17 21:00:20 +08:00
|
|
|
/* 二叉树的数组表示 */
|
|
|
|
// 使用 int? 可空类型 ,就可以使用 null 来标记空位
|
2023-09-04 03:16:55 +08:00
|
|
|
List<int?> tree = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];
|
2023-04-17 21:00:20 +08:00
|
|
|
```
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
=== "Rust"
|
2023-04-17 21:00:20 +08:00
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
```rust title=""
|
2023-04-17 21:00:20 +08:00
|
|
|
|
|
|
|
```
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
=== "C"
|
2023-06-02 02:38:24 +08:00
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
```c title=""
|
2023-06-02 15:07:33 +08:00
|
|
|
/* 二叉树的数组表示 */
|
2023-09-04 03:16:55 +08:00
|
|
|
// 使用 int 最大值标记空位,因此要求节点值不能为 INT_MAX
|
|
|
|
int tree[] = {1, 2, 3, 4, INT_MAX, 6, 7, 8, 9, INT_MAX, INT_MAX, 12, INT_MAX, INT_MAX, 15};
|
2023-06-02 02:38:24 +08:00
|
|
|
```
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
=== "Zig"
|
2023-07-26 10:57:40 +08:00
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
```zig title=""
|
2023-07-26 10:57:40 +08:00
|
|
|
|
|
|
|
```
|
|
|
|
|
2023-06-17 00:46:08 +08:00
|
|
|
![任意类型二叉树的数组表示](array_representation_of_tree.assets/array_representation_with_empty.png)
|
2023-04-17 21:00:20 +08:00
|
|
|
|
2023-08-22 13:50:12 +08:00
|
|
|
<p align="center"> 图 7-14 任意类型二叉树的数组表示 </p>
|
2023-04-17 21:00:20 +08:00
|
|
|
|
2023-08-21 19:32:37 +08:00
|
|
|
值得说明的是,**完全二叉树非常适合使用数组来表示**。回顾完全二叉树的定义,$\text{None}$ 只出现在最底层且靠右的位置,**因此所有 $\text{None}$ 一定出现在层序遍历序列的末尾**。
|
|
|
|
|
2023-08-22 13:50:12 +08:00
|
|
|
这意味着使用数组表示完全二叉树时,可以省略存储所有 $\text{None}$ ,非常方便。图 7-15 给出了一个例子。
|
2023-07-19 16:10:06 +08:00
|
|
|
|
2023-07-24 03:03:29 +08:00
|
|
|
![完全二叉树的数组表示](array_representation_of_tree.assets/array_representation_complete_binary_tree.png)
|
|
|
|
|
2023-08-22 13:50:12 +08:00
|
|
|
<p align="center"> 图 7-15 完全二叉树的数组表示 </p>
|
2023-07-24 03:03:29 +08:00
|
|
|
|
2023-08-27 23:40:56 +08:00
|
|
|
以下代码实现了一个基于数组表示的二叉树,包括以下几种操作。
|
2023-07-24 03:03:29 +08:00
|
|
|
|
2023-07-26 08:58:52 +08:00
|
|
|
- 给定某节点,获取它的值、左(右)子节点、父节点。
|
|
|
|
- 获取前序遍历、中序遍历、后序遍历、层序遍历序列。
|
2023-07-19 16:10:06 +08:00
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
=== "Python"
|
2023-07-19 16:10:06 +08:00
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
```python title="array_binary_tree.py"
|
|
|
|
class ArrayBinaryTree:
|
|
|
|
"""数组表示下的二叉树类"""
|
2023-07-19 16:10:06 +08:00
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
def __init__(self, arr: list[int | None]):
|
|
|
|
"""构造方法"""
|
|
|
|
self.__tree = list(arr)
|
2023-07-19 16:10:06 +08:00
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
def size(self):
|
|
|
|
"""节点数量"""
|
|
|
|
return len(self.__tree)
|
2023-07-19 16:10:06 +08:00
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
def val(self, i: int) -> int:
|
|
|
|
"""获取索引为 i 节点的值"""
|
|
|
|
# 若索引越界,则返回 None ,代表空位
|
|
|
|
if i < 0 or i >= self.size():
|
|
|
|
return None
|
|
|
|
return self.__tree[i]
|
2023-07-19 16:10:06 +08:00
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
def left(self, i: int) -> int | None:
|
|
|
|
"""获取索引为 i 节点的左子节点的索引"""
|
|
|
|
return 2 * i + 1
|
2023-07-19 16:10:06 +08:00
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
def right(self, i: int) -> int | None:
|
|
|
|
"""获取索引为 i 节点的右子节点的索引"""
|
|
|
|
return 2 * i + 2
|
2023-07-19 16:10:06 +08:00
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
def parent(self, i: int) -> int | None:
|
|
|
|
"""获取索引为 i 节点的父节点的索引"""
|
|
|
|
return (i - 1) // 2
|
2023-07-19 16:10:06 +08:00
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
def level_order(self) -> list[int]:
|
|
|
|
"""层序遍历"""
|
|
|
|
self.res = []
|
|
|
|
# 直接遍历数组
|
|
|
|
for i in range(self.size()):
|
|
|
|
if self.val(i) is not None:
|
|
|
|
self.res.append(self.val(i))
|
|
|
|
return self.res
|
2023-07-19 16:10:06 +08:00
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
def __dfs(self, i: int, order: str):
|
|
|
|
"""深度优先遍历"""
|
|
|
|
if self.val(i) is None:
|
|
|
|
return
|
|
|
|
# 前序遍历
|
|
|
|
if order == "pre":
|
|
|
|
self.res.append(self.val(i))
|
|
|
|
self.__dfs(self.left(i), order)
|
|
|
|
# 中序遍历
|
|
|
|
if order == "in":
|
|
|
|
self.res.append(self.val(i))
|
|
|
|
self.__dfs(self.right(i), order)
|
|
|
|
# 后序遍历
|
|
|
|
if order == "post":
|
|
|
|
self.res.append(self.val(i))
|
2023-07-19 16:10:06 +08:00
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
def pre_order(self) -> list[int]:
|
|
|
|
"""前序遍历"""
|
|
|
|
self.res = []
|
|
|
|
self.__dfs(0, order="pre")
|
|
|
|
return self.res
|
2023-07-19 16:10:06 +08:00
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
def in_order(self) -> list[int]:
|
|
|
|
"""中序遍历"""
|
|
|
|
self.res = []
|
|
|
|
self.__dfs(0, order="in")
|
|
|
|
return self.res
|
2023-07-19 16:10:06 +08:00
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
def post_order(self) -> list[int]:
|
|
|
|
"""后序遍历"""
|
|
|
|
self.res = []
|
|
|
|
self.__dfs(0, order="post")
|
|
|
|
return self.res
|
2023-07-19 16:10:06 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
```cpp title="array_binary_tree.cpp"
|
|
|
|
/* 数组表示下的二叉树类 */
|
|
|
|
class ArrayBinaryTree {
|
|
|
|
public:
|
|
|
|
/* 构造方法 */
|
|
|
|
ArrayBinaryTree(vector<int> arr) {
|
|
|
|
tree = arr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 节点数量 */
|
|
|
|
int size() {
|
|
|
|
return tree.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 获取索引为 i 节点的值 */
|
|
|
|
int val(int i) {
|
|
|
|
// 若索引越界,则返回 INT_MAX ,代表空位
|
|
|
|
if (i < 0 || i >= size())
|
|
|
|
return INT_MAX;
|
|
|
|
return tree[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 获取索引为 i 节点的左子节点的索引 */
|
|
|
|
int left(int i) {
|
|
|
|
return 2 * i + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 获取索引为 i 节点的右子节点的索引 */
|
|
|
|
int right(int i) {
|
|
|
|
return 2 * i + 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 获取索引为 i 节点的父节点的索引 */
|
|
|
|
int parent(int i) {
|
|
|
|
return (i - 1) / 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 层序遍历 */
|
|
|
|
vector<int> levelOrder() {
|
|
|
|
vector<int> res;
|
|
|
|
// 直接遍历数组
|
|
|
|
for (int i = 0; i < size(); i++) {
|
|
|
|
if (val(i) != INT_MAX)
|
|
|
|
res.push_back(val(i));
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 前序遍历 */
|
|
|
|
vector<int> preOrder() {
|
|
|
|
vector<int> res;
|
|
|
|
dfs(0, "pre", res);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 中序遍历 */
|
|
|
|
vector<int> inOrder() {
|
|
|
|
vector<int> res;
|
|
|
|
dfs(0, "in", res);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 后序遍历 */
|
|
|
|
vector<int> postOrder() {
|
|
|
|
vector<int> res;
|
|
|
|
dfs(0, "post", res);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
vector<int> tree;
|
|
|
|
|
|
|
|
/* 深度优先遍历 */
|
|
|
|
void dfs(int i, string order, vector<int> &res) {
|
|
|
|
// 若为空位,则返回
|
|
|
|
if (val(i) == INT_MAX)
|
|
|
|
return;
|
|
|
|
// 前序遍历
|
|
|
|
if (order == "pre")
|
|
|
|
res.push_back(val(i));
|
|
|
|
dfs(left(i), order, res);
|
|
|
|
// 中序遍历
|
|
|
|
if (order == "in")
|
|
|
|
res.push_back(val(i));
|
|
|
|
dfs(right(i), order, res);
|
|
|
|
// 后序遍历
|
|
|
|
if (order == "post")
|
|
|
|
res.push_back(val(i));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
```
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
=== "Java"
|
2023-07-19 16:10:06 +08:00
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
```java title="array_binary_tree.java"
|
|
|
|
/* 数组表示下的二叉树类 */
|
|
|
|
class ArrayBinaryTree {
|
|
|
|
private List<Integer> tree;
|
2023-07-19 16:10:06 +08:00
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
/* 构造方法 */
|
|
|
|
public ArrayBinaryTree(List<Integer> arr) {
|
|
|
|
tree = new ArrayList<>(arr);
|
|
|
|
}
|
2023-07-19 16:10:06 +08:00
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
/* 节点数量 */
|
|
|
|
public int size() {
|
|
|
|
return tree.size();
|
|
|
|
}
|
2023-07-19 16:10:06 +08:00
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
/* 获取索引为 i 节点的值 */
|
|
|
|
public Integer val(int i) {
|
|
|
|
// 若索引越界,则返回 null ,代表空位
|
|
|
|
if (i < 0 || i >= size())
|
|
|
|
return null;
|
|
|
|
return tree.get(i);
|
|
|
|
}
|
2023-07-19 16:10:06 +08:00
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
/* 获取索引为 i 节点的左子节点的索引 */
|
|
|
|
public Integer left(int i) {
|
|
|
|
return 2 * i + 1;
|
|
|
|
}
|
2023-07-19 16:10:06 +08:00
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
/* 获取索引为 i 节点的右子节点的索引 */
|
|
|
|
public Integer right(int i) {
|
|
|
|
return 2 * i + 2;
|
|
|
|
}
|
2023-07-19 16:10:06 +08:00
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
/* 获取索引为 i 节点的父节点的索引 */
|
|
|
|
public Integer parent(int i) {
|
|
|
|
return (i - 1) / 2;
|
|
|
|
}
|
2023-07-19 16:10:06 +08:00
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
/* 层序遍历 */
|
|
|
|
public List<Integer> levelOrder() {
|
|
|
|
List<Integer> res = new ArrayList<>();
|
|
|
|
// 直接遍历数组
|
|
|
|
for (int i = 0; i < size(); i++) {
|
|
|
|
if (val(i) != null)
|
|
|
|
res.add(val(i));
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
2023-07-19 16:10:06 +08:00
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
/* 深度优先遍历 */
|
|
|
|
private void dfs(Integer i, String order, List<Integer> res) {
|
|
|
|
// 若为空位,则返回
|
|
|
|
if (val(i) == null)
|
|
|
|
return;
|
|
|
|
// 前序遍历
|
|
|
|
if (order == "pre")
|
|
|
|
res.add(val(i));
|
|
|
|
dfs(left(i), order, res);
|
|
|
|
// 中序遍历
|
|
|
|
if (order == "in")
|
|
|
|
res.add(val(i));
|
|
|
|
dfs(right(i), order, res);
|
|
|
|
// 后序遍历
|
|
|
|
if (order == "post")
|
|
|
|
res.add(val(i));
|
|
|
|
}
|
2023-07-19 16:10:06 +08:00
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
/* 前序遍历 */
|
|
|
|
public List<Integer> preOrder() {
|
|
|
|
List<Integer> res = new ArrayList<>();
|
|
|
|
dfs(0, "pre", res);
|
|
|
|
return res;
|
|
|
|
}
|
2023-07-19 16:10:06 +08:00
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
/* 中序遍历 */
|
|
|
|
public List<Integer> inOrder() {
|
|
|
|
List<Integer> res = new ArrayList<>();
|
|
|
|
dfs(0, "in", res);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 后序遍历 */
|
|
|
|
public List<Integer> postOrder() {
|
|
|
|
List<Integer> res = new ArrayList<>();
|
|
|
|
dfs(0, "post", res);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
```csharp title="array_binary_tree.cs"
|
|
|
|
/* 数组表示下的二叉树类 */
|
|
|
|
class ArrayBinaryTree {
|
|
|
|
private List<int?> tree;
|
|
|
|
|
|
|
|
/* 构造方法 */
|
|
|
|
public ArrayBinaryTree(List<int?> arr) {
|
|
|
|
tree = new List<int?>(arr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 节点数量 */
|
|
|
|
public int size() {
|
|
|
|
return tree.Count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 获取索引为 i 节点的值 */
|
|
|
|
public int? val(int i) {
|
|
|
|
// 若索引越界,则返回 null ,代表空位
|
|
|
|
if (i < 0 || i >= size())
|
|
|
|
return null;
|
|
|
|
return tree[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 获取索引为 i 节点的左子节点的索引 */
|
|
|
|
public int left(int i) {
|
|
|
|
return 2 * i + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 获取索引为 i 节点的右子节点的索引 */
|
|
|
|
public int right(int i) {
|
|
|
|
return 2 * i + 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 获取索引为 i 节点的父节点的索引 */
|
|
|
|
public int parent(int i) {
|
|
|
|
return (i - 1) / 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 层序遍历 */
|
|
|
|
public List<int> levelOrder() {
|
|
|
|
List<int> res = new List<int>();
|
|
|
|
// 直接遍历数组
|
|
|
|
for (int i = 0; i < size(); i++) {
|
|
|
|
if (val(i).HasValue)
|
|
|
|
res.Add(val(i).Value);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 深度优先遍历 */
|
|
|
|
private void dfs(int i, string order, List<int> res) {
|
|
|
|
// 若为空位,则返回
|
|
|
|
if (!val(i).HasValue)
|
|
|
|
return;
|
|
|
|
// 前序遍历
|
|
|
|
if (order == "pre")
|
|
|
|
res.Add(val(i).Value);
|
|
|
|
dfs(left(i), order, res);
|
|
|
|
// 中序遍历
|
|
|
|
if (order == "in")
|
|
|
|
res.Add(val(i).Value);
|
|
|
|
dfs(right(i), order, res);
|
|
|
|
// 后序遍历
|
|
|
|
if (order == "post")
|
|
|
|
res.Add(val(i).Value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 前序遍历 */
|
|
|
|
public List<int> preOrder() {
|
|
|
|
List<int> res = new List<int>();
|
|
|
|
dfs(0, "pre", res);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 中序遍历 */
|
|
|
|
public List<int> inOrder() {
|
|
|
|
List<int> res = new List<int>();
|
|
|
|
dfs(0, "in", res);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 后序遍历 */
|
|
|
|
public List<int> postOrder() {
|
|
|
|
List<int> res = new List<int>();
|
|
|
|
dfs(0, "post", res);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
2023-07-19 16:10:06 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
```go title="array_binary_tree.go"
|
2023-07-26 15:34:46 +08:00
|
|
|
/* 数组表示下的二叉树类 */
|
|
|
|
type arrayBinaryTree struct {
|
|
|
|
tree []any
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 构造方法 */
|
|
|
|
func newArrayBinaryTree(arr []any) *arrayBinaryTree {
|
|
|
|
return &arrayBinaryTree{
|
|
|
|
tree: arr,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 节点数量 */
|
|
|
|
func (abt *arrayBinaryTree) size() int {
|
|
|
|
return len(abt.tree)
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 获取索引为 i 节点的值 */
|
|
|
|
func (abt *arrayBinaryTree) val(i int) any {
|
|
|
|
// 若索引越界,则返回 null ,代表空位
|
|
|
|
if i < 0 || i >= abt.size() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return abt.tree[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 获取索引为 i 节点的左子节点的索引 */
|
|
|
|
func (abt *arrayBinaryTree) left(i int) int {
|
|
|
|
return 2*i + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 获取索引为 i 节点的右子节点的索引 */
|
|
|
|
func (abt *arrayBinaryTree) right(i int) int {
|
|
|
|
return 2*i + 2
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 获取索引为 i 节点的父节点的索引 */
|
|
|
|
func (abt *arrayBinaryTree) parent(i int) int {
|
|
|
|
return (i - 1) / 2
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 层序遍历 */
|
|
|
|
func (abt *arrayBinaryTree) levelOrder() []any {
|
|
|
|
var res []any
|
|
|
|
// 直接遍历数组
|
|
|
|
for i := 0; i < abt.size(); i++ {
|
|
|
|
if abt.val(i) != nil {
|
|
|
|
res = append(res, abt.val(i))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 深度优先遍历 */
|
|
|
|
func (abt *arrayBinaryTree) dfs(i int, order string, res *[]any) {
|
|
|
|
// 若为空位,则返回
|
|
|
|
if abt.val(i) == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// 前序遍历
|
|
|
|
if order == "pre" {
|
|
|
|
*res = append(*res, abt.val(i))
|
|
|
|
}
|
|
|
|
abt.dfs(abt.left(i), order, res)
|
|
|
|
// 中序遍历
|
|
|
|
if order == "in" {
|
|
|
|
*res = append(*res, abt.val(i))
|
|
|
|
}
|
|
|
|
abt.dfs(abt.right(i), order, res)
|
|
|
|
// 后序遍历
|
|
|
|
if order == "post" {
|
|
|
|
*res = append(*res, abt.val(i))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 前序遍历 */
|
|
|
|
func (abt *arrayBinaryTree) preOrder() []any {
|
|
|
|
var res []any
|
|
|
|
abt.dfs(0, "pre", &res)
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 中序遍历 */
|
|
|
|
func (abt *arrayBinaryTree) inOrder() []any {
|
|
|
|
var res []any
|
|
|
|
abt.dfs(0, "in", &res)
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 后序遍历 */
|
|
|
|
func (abt *arrayBinaryTree) postOrder() []any {
|
|
|
|
var res []any
|
|
|
|
abt.dfs(0, "post", &res)
|
|
|
|
return res
|
|
|
|
}
|
2023-07-19 16:10:06 +08:00
|
|
|
```
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
=== "Swift"
|
2023-07-19 16:10:06 +08:00
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
```swift title="array_binary_tree.swift"
|
2023-08-13 19:36:03 +08:00
|
|
|
/* 数组表示下的二叉树类 */
|
|
|
|
class ArrayBinaryTree {
|
2023-09-04 03:16:55 +08:00
|
|
|
private var tree: [Int?]
|
2023-08-13 19:36:03 +08:00
|
|
|
|
|
|
|
/* 构造方法 */
|
2023-09-04 03:16:55 +08:00
|
|
|
init(arr: [Int?]) {
|
|
|
|
tree = arr
|
2023-08-13 19:36:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 节点数量 */
|
2023-09-04 03:16:55 +08:00
|
|
|
func size() -> Int {
|
|
|
|
tree.count
|
2023-08-13 19:36:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 获取索引为 i 节点的值 */
|
2023-09-04 03:16:55 +08:00
|
|
|
func val(i: Int) -> Int? {
|
2023-08-13 19:36:03 +08:00
|
|
|
// 若索引越界,则返回 null ,代表空位
|
2023-09-04 03:16:55 +08:00
|
|
|
if i < 0 || i >= size() {
|
|
|
|
return nil
|
2023-08-13 19:36:03 +08:00
|
|
|
}
|
2023-09-04 03:16:55 +08:00
|
|
|
return tree[i]
|
2023-08-13 19:36:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 获取索引为 i 节点的左子节点的索引 */
|
2023-09-04 03:16:55 +08:00
|
|
|
func left(i: Int) -> Int {
|
|
|
|
2 * i + 1
|
2023-08-13 19:36:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 获取索引为 i 节点的右子节点的索引 */
|
2023-09-04 03:16:55 +08:00
|
|
|
func right(i: Int) -> Int {
|
|
|
|
2 * i + 2
|
2023-08-13 19:36:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 获取索引为 i 节点的父节点的索引 */
|
2023-09-04 03:16:55 +08:00
|
|
|
func parent(i: Int) -> Int {
|
|
|
|
(i - 1) / 2
|
2023-08-13 19:36:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 层序遍历 */
|
2023-09-04 03:16:55 +08:00
|
|
|
func levelOrder() -> [Int] {
|
|
|
|
var res: [Int] = []
|
2023-08-13 19:36:03 +08:00
|
|
|
// 直接遍历数组
|
2023-09-04 03:16:55 +08:00
|
|
|
for i in stride(from: 0, to: size(), by: 1) {
|
|
|
|
if let val = val(i: i) {
|
|
|
|
res.append(val)
|
|
|
|
}
|
2023-08-13 19:36:03 +08:00
|
|
|
}
|
2023-09-04 03:16:55 +08:00
|
|
|
return res
|
2023-08-13 19:36:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 深度优先遍历 */
|
2023-09-04 03:16:55 +08:00
|
|
|
private func dfs(i: Int, order: String, res: inout [Int]) {
|
2023-08-13 19:36:03 +08:00
|
|
|
// 若为空位,则返回
|
2023-09-04 03:16:55 +08:00
|
|
|
guard let val = val(i: i) else {
|
|
|
|
return
|
|
|
|
}
|
2023-08-13 19:36:03 +08:00
|
|
|
// 前序遍历
|
2023-09-04 03:16:55 +08:00
|
|
|
if order == "pre" {
|
|
|
|
res.append(val)
|
|
|
|
}
|
|
|
|
dfs(i: left(i: i), order: order, res: &res)
|
2023-08-13 19:36:03 +08:00
|
|
|
// 中序遍历
|
2023-09-04 03:16:55 +08:00
|
|
|
if order == "in" {
|
|
|
|
res.append(val)
|
|
|
|
}
|
|
|
|
dfs(i: right(i: i), order: order, res: &res)
|
2023-08-13 19:36:03 +08:00
|
|
|
// 后序遍历
|
2023-09-04 03:16:55 +08:00
|
|
|
if order == "post" {
|
|
|
|
res.append(val)
|
|
|
|
}
|
2023-08-13 19:36:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 前序遍历 */
|
2023-09-04 03:16:55 +08:00
|
|
|
func preOrder() -> [Int] {
|
|
|
|
var res: [Int] = []
|
|
|
|
dfs(i: 0, order: "pre", res: &res)
|
|
|
|
return res
|
2023-08-13 19:36:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 中序遍历 */
|
2023-09-04 03:16:55 +08:00
|
|
|
func inOrder() -> [Int] {
|
|
|
|
var res: [Int] = []
|
|
|
|
dfs(i: 0, order: "in", res: &res)
|
|
|
|
return res
|
2023-08-13 19:36:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 后序遍历 */
|
2023-09-04 03:16:55 +08:00
|
|
|
func postOrder() -> [Int] {
|
|
|
|
var res: [Int] = []
|
|
|
|
dfs(i: 0, order: "post", res: &res)
|
|
|
|
return res
|
2023-08-04 05:24:49 +08:00
|
|
|
}
|
|
|
|
}
|
2023-07-19 16:10:06 +08:00
|
|
|
```
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
=== "JS"
|
2023-07-19 16:10:06 +08:00
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
```javascript title="array_binary_tree.js"
|
2023-07-21 15:14:40 +08:00
|
|
|
/* 数组表示下的二叉树类 */
|
|
|
|
class ArrayBinaryTree {
|
2023-09-04 03:16:55 +08:00
|
|
|
#tree;
|
2023-07-21 15:14:40 +08:00
|
|
|
|
|
|
|
/* 构造方法 */
|
2023-09-04 03:16:55 +08:00
|
|
|
constructor(arr) {
|
|
|
|
this.#tree = arr;
|
2023-07-21 15:14:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 节点数量 */
|
2023-09-04 03:16:55 +08:00
|
|
|
size() {
|
|
|
|
return this.#tree.length;
|
2023-07-21 15:14:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 获取索引为 i 节点的值 */
|
2023-09-04 03:16:55 +08:00
|
|
|
val(i) {
|
2023-07-21 15:14:40 +08:00
|
|
|
// 若索引越界,则返回 null ,代表空位
|
2023-09-04 03:16:55 +08:00
|
|
|
if (i < 0 || i >= this.size()) return null;
|
|
|
|
return this.#tree[i];
|
2023-07-21 15:14:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 获取索引为 i 节点的左子节点的索引 */
|
2023-09-04 03:16:55 +08:00
|
|
|
left(i) {
|
2023-07-21 15:14:40 +08:00
|
|
|
return 2 * i + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 获取索引为 i 节点的右子节点的索引 */
|
2023-09-04 03:16:55 +08:00
|
|
|
right(i) {
|
2023-07-21 15:14:40 +08:00
|
|
|
return 2 * i + 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 获取索引为 i 节点的父节点的索引 */
|
2023-09-04 03:16:55 +08:00
|
|
|
parent(i) {
|
2023-07-21 15:14:40 +08:00
|
|
|
return (i - 1) / 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 层序遍历 */
|
2023-09-04 03:16:55 +08:00
|
|
|
levelOrder() {
|
|
|
|
let res = [];
|
2023-07-21 15:14:40 +08:00
|
|
|
// 直接遍历数组
|
2023-09-04 03:16:55 +08:00
|
|
|
for (let i = 0; i < this.size(); i++) {
|
|
|
|
if (this.val(i) !== null) res.push(this.val(i));
|
2023-07-21 15:14:40 +08:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 深度优先遍历 */
|
2023-09-04 03:16:55 +08:00
|
|
|
#dfs(i, order, res) {
|
2023-07-21 15:14:40 +08:00
|
|
|
// 若为空位,则返回
|
2023-09-04 03:16:55 +08:00
|
|
|
if (this.val(i) === null) return;
|
2023-07-21 15:14:40 +08:00
|
|
|
// 前序遍历
|
2023-09-04 03:16:55 +08:00
|
|
|
if (order === 'pre') res.push(this.val(i));
|
|
|
|
this.#dfs(this.left(i), order, res);
|
2023-07-21 15:14:40 +08:00
|
|
|
// 中序遍历
|
2023-09-04 03:16:55 +08:00
|
|
|
if (order === 'in') res.push(this.val(i));
|
|
|
|
this.#dfs(this.right(i), order, res);
|
2023-07-21 15:14:40 +08:00
|
|
|
// 后序遍历
|
2023-09-04 03:16:55 +08:00
|
|
|
if (order === 'post') res.push(this.val(i));
|
2023-07-21 15:14:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 前序遍历 */
|
2023-09-04 03:16:55 +08:00
|
|
|
preOrder() {
|
|
|
|
const res = [];
|
|
|
|
this.#dfs(0, 'pre', res);
|
2023-07-21 15:14:40 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 中序遍历 */
|
2023-09-04 03:16:55 +08:00
|
|
|
inOrder() {
|
|
|
|
const res = [];
|
|
|
|
this.#dfs(0, 'in', res);
|
2023-07-21 15:14:40 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 后序遍历 */
|
2023-09-04 03:16:55 +08:00
|
|
|
postOrder() {
|
|
|
|
const res = [];
|
|
|
|
this.#dfs(0, 'post', res);
|
2023-07-21 15:14:40 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
2023-07-19 16:10:06 +08:00
|
|
|
```
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
=== "TS"
|
2023-07-19 16:10:06 +08:00
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
```typescript title="array_binary_tree.ts"
|
2023-07-24 13:09:43 +08:00
|
|
|
/* 数组表示下的二叉树类 */
|
|
|
|
class ArrayBinaryTree {
|
2023-09-04 03:16:55 +08:00
|
|
|
#tree: (number | null)[];
|
2023-07-24 13:09:43 +08:00
|
|
|
|
|
|
|
/* 构造方法 */
|
2023-09-04 03:16:55 +08:00
|
|
|
constructor(arr: (number | null)[]) {
|
|
|
|
this.#tree = arr;
|
2023-07-24 13:09:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 节点数量 */
|
2023-09-04 03:16:55 +08:00
|
|
|
size(): number {
|
|
|
|
return this.#tree.length;
|
2023-07-24 13:09:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 获取索引为 i 节点的值 */
|
2023-09-04 03:16:55 +08:00
|
|
|
val(i: number): number | null {
|
2023-07-24 13:09:43 +08:00
|
|
|
// 若索引越界,则返回 null ,代表空位
|
2023-09-04 03:16:55 +08:00
|
|
|
if (i < 0 || i >= this.size()) return null;
|
|
|
|
return this.#tree[i];
|
2023-07-24 13:09:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 获取索引为 i 节点的左子节点的索引 */
|
2023-09-04 03:16:55 +08:00
|
|
|
left(i: number): number {
|
|
|
|
return 2 * i + 1;
|
2023-07-24 13:09:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 获取索引为 i 节点的右子节点的索引 */
|
2023-09-04 03:16:55 +08:00
|
|
|
right(i: number): number {
|
|
|
|
return 2 * i + 2;
|
2023-07-24 13:09:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 获取索引为 i 节点的父节点的索引 */
|
2023-09-04 03:16:55 +08:00
|
|
|
parent(i: number): number {
|
|
|
|
return (i - 1) / 2;
|
2023-07-24 13:09:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 层序遍历 */
|
2023-09-04 03:16:55 +08:00
|
|
|
levelOrder(): number[] {
|
|
|
|
let res = [];
|
2023-07-24 13:09:43 +08:00
|
|
|
// 直接遍历数组
|
2023-09-04 03:16:55 +08:00
|
|
|
for (let i = 0; i < this.size(); i++) {
|
|
|
|
if (this.val(i) !== null) res.push(this.val(i));
|
2023-07-24 13:09:43 +08:00
|
|
|
}
|
2023-09-04 03:16:55 +08:00
|
|
|
return res;
|
2023-07-24 13:09:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 深度优先遍历 */
|
2023-09-04 03:16:55 +08:00
|
|
|
#dfs(i: number, order: Order, res: (number | null)[]): void {
|
2023-07-24 13:09:43 +08:00
|
|
|
// 若为空位,则返回
|
2023-09-04 03:16:55 +08:00
|
|
|
if (this.val(i) === null) return;
|
2023-07-24 13:09:43 +08:00
|
|
|
// 前序遍历
|
2023-09-04 03:16:55 +08:00
|
|
|
if (order === 'pre') res.push(this.val(i));
|
|
|
|
this.#dfs(this.left(i), order, res);
|
2023-07-24 13:09:43 +08:00
|
|
|
// 中序遍历
|
2023-09-04 03:16:55 +08:00
|
|
|
if (order === 'in') res.push(this.val(i));
|
|
|
|
this.#dfs(this.right(i), order, res);
|
2023-07-24 13:09:43 +08:00
|
|
|
// 后序遍历
|
2023-09-04 03:16:55 +08:00
|
|
|
if (order === 'post') res.push(this.val(i));
|
2023-07-24 13:09:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 前序遍历 */
|
2023-09-04 03:16:55 +08:00
|
|
|
preOrder(): (number | null)[] {
|
|
|
|
const res = [];
|
|
|
|
this.#dfs(0, 'pre', res);
|
|
|
|
return res;
|
2023-07-24 13:09:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 中序遍历 */
|
2023-09-04 03:16:55 +08:00
|
|
|
inOrder(): (number | null)[] {
|
|
|
|
const res = [];
|
|
|
|
this.#dfs(0, 'in', res);
|
|
|
|
return res;
|
2023-07-24 13:09:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 后序遍历 */
|
2023-09-04 03:16:55 +08:00
|
|
|
postOrder(): (number | null)[] {
|
|
|
|
const res = [];
|
|
|
|
this.#dfs(0, 'post', res);
|
|
|
|
return res;
|
2023-07-24 13:09:43 +08:00
|
|
|
}
|
|
|
|
}
|
2023-07-19 16:10:06 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
=== "Dart"
|
|
|
|
|
|
|
|
```dart title="array_binary_tree.dart"
|
2023-08-17 05:12:05 +08:00
|
|
|
/* 数组表示下的二叉树类 */
|
|
|
|
class ArrayBinaryTree {
|
|
|
|
late List<int?> _tree;
|
|
|
|
|
|
|
|
/* 构造方法 */
|
|
|
|
ArrayBinaryTree(this._tree);
|
|
|
|
|
|
|
|
/* 节点数量 */
|
|
|
|
int size() {
|
|
|
|
return _tree.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 获取索引为 i 节点的值 */
|
|
|
|
int? val(int i) {
|
|
|
|
// 若索引越界,则返回 null ,代表空位
|
|
|
|
if (i < 0 || i >= size()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return _tree[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 获取索引为 i 节点的左子节点的索引 */
|
|
|
|
int? left(int i) {
|
|
|
|
return 2 * i + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 获取索引为 i 节点的右子节点的索引 */
|
|
|
|
int? right(int i) {
|
|
|
|
return 2 * i + 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 获取索引为 i 节点的父节点的索引 */
|
|
|
|
int? parent(int i) {
|
|
|
|
return (i - 1) ~/ 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 层序遍历 */
|
|
|
|
List<int> levelOrder() {
|
|
|
|
List<int> res = [];
|
|
|
|
for (int i = 0; i < size(); i++) {
|
|
|
|
if (val(i) != null) {
|
|
|
|
res.add(val(i)!);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 深度优先遍历 */
|
|
|
|
void dfs(int i, String order, List<int?> res) {
|
|
|
|
// 若为空位,则返回
|
|
|
|
if (val(i) == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// 前序遍历
|
|
|
|
if (order == 'pre') {
|
|
|
|
res.add(val(i));
|
|
|
|
}
|
|
|
|
dfs(left(i)!, order, res);
|
|
|
|
// 中序遍历
|
|
|
|
if (order == 'in') {
|
|
|
|
res.add(val(i));
|
|
|
|
}
|
|
|
|
dfs(right(i)!, order, res);
|
|
|
|
// 后序遍历
|
|
|
|
if (order == 'post') {
|
|
|
|
res.add(val(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 前序遍历 */
|
|
|
|
List<int?> preOrder() {
|
|
|
|
List<int?> res = [];
|
|
|
|
dfs(0, 'pre', res);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 中序遍历 */
|
|
|
|
List<int?> inOrder() {
|
|
|
|
List<int?> res = [];
|
|
|
|
dfs(0, 'in', res);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 后序遍历 */
|
|
|
|
List<int?> postOrder() {
|
|
|
|
List<int?> res = [];
|
|
|
|
dfs(0, 'post', res);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
2023-07-19 16:10:06 +08:00
|
|
|
```
|
|
|
|
|
2023-07-26 10:57:40 +08:00
|
|
|
=== "Rust"
|
|
|
|
|
|
|
|
```rust title="array_binary_tree.rs"
|
|
|
|
/* 数组表示下的二叉树类 */
|
|
|
|
struct ArrayBinaryTree {
|
|
|
|
tree: Vec<Option<i32>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ArrayBinaryTree {
|
|
|
|
/* 构造方法 */
|
|
|
|
fn new(arr: Vec<Option<i32>>) -> Self {
|
|
|
|
Self { tree: arr }
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 节点数量 */
|
|
|
|
fn size(&self) -> i32 {
|
|
|
|
self.tree.len() as i32
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 获取索引为 i 节点的值 */
|
|
|
|
fn val(&self, i: i32) -> Option<i32> {
|
|
|
|
// 若索引越界,则返回 None ,代表空位
|
|
|
|
if i < 0 || i >= self.size() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
self.tree[i as usize]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 获取索引为 i 节点的左子节点的索引 */
|
|
|
|
fn left(&self, i: i32) -> i32 {
|
|
|
|
2 * i + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 获取索引为 i 节点的右子节点的索引 */
|
|
|
|
fn right(&self, i: i32) -> i32 {
|
|
|
|
2 * i + 2
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 获取索引为 i 节点的父节点的索引 */
|
|
|
|
fn parent(&self, i: i32) -> i32 {
|
|
|
|
(i - 1) / 2
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 层序遍历 */
|
|
|
|
fn level_order(&self) -> Vec<i32> {
|
|
|
|
let mut res = vec![];
|
|
|
|
// 直接遍历数组
|
|
|
|
for i in 0..self.size() {
|
|
|
|
if let Some(val) = self.val(i) {
|
|
|
|
res.push(val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 深度优先遍历 */
|
|
|
|
fn dfs(&self, i: i32, order: &str, res: &mut Vec<i32>) {
|
|
|
|
if self.val(i).is_none() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let val = self.val(i).unwrap();
|
|
|
|
// 前序遍历
|
|
|
|
if order == "pre" {
|
|
|
|
res.push(val);
|
|
|
|
}
|
|
|
|
self.dfs(self.left(i), order, res);
|
|
|
|
// 中序遍历
|
|
|
|
if order == "in" {
|
|
|
|
res.push(val);
|
|
|
|
}
|
|
|
|
self.dfs(self.right(i), order, res);
|
|
|
|
// 后序遍历
|
|
|
|
if order == "post" {
|
|
|
|
res.push(val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 前序遍历 */
|
|
|
|
fn pre_order(&self) -> Vec<i32> {
|
|
|
|
let mut res = vec![];
|
|
|
|
self.dfs(0, "pre", &mut res);
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 中序遍历 */
|
|
|
|
fn in_order(&self) -> Vec<i32> {
|
|
|
|
let mut res = vec![];
|
|
|
|
self.dfs(0, "in", &mut res);
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 后序遍历 */
|
|
|
|
fn post_order(&self) -> Vec<i32> {
|
|
|
|
let mut res = vec![];
|
|
|
|
self.dfs(0, "post", &mut res);
|
|
|
|
res
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
=== "C"
|
|
|
|
|
|
|
|
```c title="array_binary_tree.c"
|
|
|
|
/* 数组表示下的二叉树类 */
|
|
|
|
struct arrayBinaryTree {
|
|
|
|
vector *tree;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct arrayBinaryTree arrayBinaryTree;
|
|
|
|
|
|
|
|
/* 构造函数 */
|
|
|
|
arrayBinaryTree *newArrayBinaryTree(vector *arr) {
|
|
|
|
arrayBinaryTree *newABT = malloc(sizeof(arrayBinaryTree));
|
|
|
|
newABT->tree = arr;
|
|
|
|
return newABT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 节点数量 */
|
|
|
|
int size(arrayBinaryTree *abt) {
|
|
|
|
return abt->tree->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 获取索引为 i 节点的值 */
|
|
|
|
int val(arrayBinaryTree *abt, int i) {
|
|
|
|
// 若索引越界,则返回 INT_MAX ,代表空位
|
|
|
|
if (i < 0 || i >= size(abt))
|
|
|
|
return INT_MAX;
|
|
|
|
return *(int *)abt->tree->data[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 深度优先遍历 */
|
|
|
|
void dfs(arrayBinaryTree *abt, int i, const char *order, vector *res) {
|
|
|
|
// 若为空位,则返回
|
|
|
|
if (val(abt, i) == INT_MAX)
|
|
|
|
return;
|
|
|
|
// 前序遍历
|
|
|
|
if (strcmp(order, "pre") == 0) {
|
|
|
|
int tmp = val(abt, i);
|
|
|
|
vectorPushback(res, &tmp, sizeof(tmp));
|
|
|
|
}
|
|
|
|
dfs(abt, left(i), order, res);
|
|
|
|
// 中序遍历
|
|
|
|
if (strcmp(order, "in") == 0) {
|
|
|
|
int tmp = val(abt, i);
|
|
|
|
vectorPushback(res, &tmp, sizeof(tmp));
|
|
|
|
}
|
|
|
|
dfs(abt, right(i), order, res);
|
|
|
|
// 后序遍历
|
|
|
|
if (strcmp(order, "post") == 0) {
|
|
|
|
int tmp = val(abt, i);
|
|
|
|
vectorPushback(res, &tmp, sizeof(tmp));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 层序遍历 */
|
|
|
|
vector *levelOrder(arrayBinaryTree *abt) {
|
|
|
|
vector *res = newVector();
|
|
|
|
// 直接遍历数组
|
|
|
|
for (int i = 0; i < size(abt); i++) {
|
|
|
|
if (val(abt, i) != INT_MAX) {
|
|
|
|
int tmp = val(abt, i);
|
|
|
|
vectorPushback(res, &tmp, sizeof(int));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 前序遍历 */
|
|
|
|
vector *preOrder(arrayBinaryTree *abt) {
|
|
|
|
vector *res = newVector();
|
|
|
|
dfs(abt, 0, "pre", res);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 中序遍历 */
|
|
|
|
vector *inOrder(arrayBinaryTree *abt) {
|
|
|
|
vector *res = newVector();
|
|
|
|
dfs(abt, 0, "in", res);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 后序遍历 */
|
|
|
|
vector *postOrder(arrayBinaryTree *abt) {
|
|
|
|
vector *res = newVector();
|
|
|
|
dfs(abt, 0, "post", res);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
```zig title="array_binary_tree.zig"
|
|
|
|
[class]{ArrayBinaryTree}-[func]{}
|
|
|
|
```
|
|
|
|
|
2023-08-19 22:07:27 +08:00
|
|
|
## 7.3.3 优势与局限性
|
2023-04-17 21:00:20 +08:00
|
|
|
|
2023-08-27 23:40:56 +08:00
|
|
|
二叉树的数组表示主要有以下优点。
|
2023-04-17 21:00:20 +08:00
|
|
|
|
2023-07-26 08:58:52 +08:00
|
|
|
- 数组存储在连续的内存空间中,对缓存友好,访问与遍历速度较快。
|
|
|
|
- 不需要存储指针,比较节省空间。
|
|
|
|
- 允许随机访问节点。
|
2023-04-17 21:00:20 +08:00
|
|
|
|
2023-08-27 23:40:56 +08:00
|
|
|
然而,数组表示也存在一些局限性。
|
2023-04-17 21:00:20 +08:00
|
|
|
|
2023-07-26 08:58:52 +08:00
|
|
|
- 数组存储需要连续内存空间,因此不适合存储数据量过大的树。
|
|
|
|
- 增删节点需要通过数组插入与删除操作实现,效率较低。
|
|
|
|
- 当二叉树中存在大量 $\text{None}$ 时,数组中包含的节点数据比重较低,空间利用率较低。
|