mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-12 07:26:07 +00:00
Sort the coding languages by applications. (#721)
This commit is contained in:
@@ -24,12 +24,12 @@
|
||||
|
||||
为了解决此问题,**我们可以考虑在层序遍历序列中显式地写出所有 $\text{None}$** 。如下图所示,这样处理后,层序遍历序列就可以唯一表示二叉树了。
|
||||
|
||||
=== "Java"
|
||||
=== "Python"
|
||||
|
||||
```java title=""
|
||||
/* 二叉树的数组表示 */
|
||||
// 使用 int 的包装类 Integer ,就可以使用 null 来标记空位
|
||||
Integer[] tree = { 1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15 };
|
||||
```python title=""
|
||||
# 二叉树的数组表示
|
||||
# 使用 None 来表示空位
|
||||
tree = [1, 2, 3, 4, None, 6, 7, 8, 9, None, None, 12, None, None, 15]
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
@@ -40,12 +40,20 @@
|
||||
vector<int> tree = {1, 2, 3, 4, INT_MAX, 6, 7, 8, 9, INT_MAX, INT_MAX, 12, INT_MAX, INT_MAX, 15};
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
=== "Java"
|
||||
|
||||
```python title=""
|
||||
# 二叉树的数组表示
|
||||
# 使用 None 来表示空位
|
||||
tree = [1, 2, 3, 4, None, 6, 7, 8, 9, None, None, 12, None, None, 15]
|
||||
```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 };
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
@@ -56,6 +64,14 @@
|
||||
tree := []any{1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title=""
|
||||
/* 二叉树的数组表示 */
|
||||
// 使用 Int? 可空类型 ,就可以使用 nil 来标记空位
|
||||
let tree: [Int?] = [1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15]
|
||||
```
|
||||
|
||||
=== "JS"
|
||||
|
||||
```javascript title=""
|
||||
@@ -72,36 +88,6 @@
|
||||
let tree: (number | null)[] = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title=""
|
||||
/* 二叉树的数组表示 */
|
||||
// 使用 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};
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title=""
|
||||
/* 二叉树的数组表示 */
|
||||
// 使用 int? 可空类型 ,就可以使用 null 来标记空位
|
||||
int?[] tree = { 1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15 };
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title=""
|
||||
/* 二叉树的数组表示 */
|
||||
// 使用 Int? 可空类型 ,就可以使用 nil 来标记空位
|
||||
let tree: [Int?] = [1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15]
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title=""
|
||||
|
||||
```
|
||||
|
||||
=== "Dart"
|
||||
|
||||
```dart title=""
|
||||
@@ -116,6 +102,20 @@
|
||||
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title=""
|
||||
/* 二叉树的数组表示 */
|
||||
// 使用 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};
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title=""
|
||||
|
||||
```
|
||||
|
||||

|
||||
|
||||
值得说明的是,**完全二叉树非常适合使用数组来表示**。回顾完全二叉树的定义,$\text{None}$ 只出现在最底层且靠右的位置,**因此所有 $\text{None}$ 一定出现在层序遍历序列的末尾**。
|
||||
@@ -129,9 +129,9 @@
|
||||
- 给定某节点,获取它的值、左(右)子节点、父节点。
|
||||
- 获取前序遍历、中序遍历、后序遍历、层序遍历序列。
|
||||
|
||||
=== "Java"
|
||||
=== "Python"
|
||||
|
||||
```java title="array_binary_tree.java"
|
||||
```python title="array_binary_tree.py"
|
||||
[class]{ArrayBinaryTree}-[func]{}
|
||||
```
|
||||
|
||||
@@ -141,9 +141,15 @@
|
||||
[class]{ArrayBinaryTree}-[func]{}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
=== "Java"
|
||||
|
||||
```python title="array_binary_tree.py"
|
||||
```java title="array_binary_tree.java"
|
||||
[class]{ArrayBinaryTree}-[func]{}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="array_binary_tree.cs"
|
||||
[class]{ArrayBinaryTree}-[func]{}
|
||||
```
|
||||
|
||||
@@ -153,6 +159,12 @@
|
||||
[class]{arrayBinaryTree}-[func]{}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="array_binary_tree.swift"
|
||||
[class]{ArrayBinaryTree}-[func]{}
|
||||
```
|
||||
|
||||
=== "JS"
|
||||
|
||||
```javascript title="array_binary_tree.js"
|
||||
@@ -165,30 +177,6 @@
|
||||
[class]{ArrayBinaryTree}-[func]{}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="array_binary_tree.c"
|
||||
[class]{arrayBinaryTree}-[func]{}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="array_binary_tree.cs"
|
||||
[class]{ArrayBinaryTree}-[func]{}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="array_binary_tree.swift"
|
||||
[class]{ArrayBinaryTree}-[func]{}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="array_binary_tree.zig"
|
||||
[class]{ArrayBinaryTree}-[func]{}
|
||||
```
|
||||
|
||||
=== "Dart"
|
||||
|
||||
```dart title="array_binary_tree.dart"
|
||||
@@ -201,6 +189,18 @@
|
||||
[class]{ArrayBinaryTree}-[func]{}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="array_binary_tree.c"
|
||||
[class]{arrayBinaryTree}-[func]{}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="array_binary_tree.zig"
|
||||
[class]{ArrayBinaryTree}-[func]{}
|
||||
```
|
||||
|
||||
## 优势与局限性
|
||||
|
||||
二叉树的数组表示主要有以下优点。
|
||||
|
||||
+302
-302
@@ -20,17 +20,16 @@ AVL 树既是二叉搜索树也是平衡二叉树,同时满足这两类二叉
|
||||
|
||||
由于 AVL 树的相关操作需要获取节点高度,因此我们需要为节点类添加 `height` 变量。
|
||||
|
||||
=== "Java"
|
||||
=== "Python"
|
||||
|
||||
```java title=""
|
||||
/* AVL 树节点类 */
|
||||
class TreeNode {
|
||||
public int val; // 节点值
|
||||
public int height; // 节点高度
|
||||
public TreeNode left; // 左子节点
|
||||
public TreeNode right; // 右子节点
|
||||
public TreeNode(int x) { val = x; }
|
||||
}
|
||||
```python title=""
|
||||
class TreeNode:
|
||||
"""AVL 树节点类"""
|
||||
def __init__(self, val: int):
|
||||
self.val: int = val # 节点值
|
||||
self.height: int = 0 # 节点高度
|
||||
self.left: Optional[TreeNode] = None # 左子节点引用
|
||||
self.right: Optional[TreeNode] = None # 右子节点引用
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
@@ -47,16 +46,30 @@ AVL 树既是二叉搜索树也是平衡二叉树,同时满足这两类二叉
|
||||
};
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
=== "Java"
|
||||
|
||||
```python title=""
|
||||
class TreeNode:
|
||||
"""AVL 树节点类"""
|
||||
def __init__(self, val: int):
|
||||
self.val: int = val # 节点值
|
||||
self.height: int = 0 # 节点高度
|
||||
self.left: Optional[TreeNode] = None # 左子节点引用
|
||||
self.right: Optional[TreeNode] = None # 右子节点引用
|
||||
```java title=""
|
||||
/* AVL 树节点类 */
|
||||
class TreeNode {
|
||||
public int val; // 节点值
|
||||
public int height; // 节点高度
|
||||
public TreeNode left; // 左子节点
|
||||
public TreeNode right; // 右子节点
|
||||
public TreeNode(int x) { val = x; }
|
||||
}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title=""
|
||||
/* AVL 树节点类 */
|
||||
class TreeNode {
|
||||
public int val; // 节点值
|
||||
public int height; // 节点高度
|
||||
public TreeNode? left; // 左子节点
|
||||
public TreeNode? right; // 右子节点
|
||||
public TreeNode(int x) { val = x; }
|
||||
}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
@@ -71,6 +84,23 @@ AVL 树既是二叉搜索树也是平衡二叉树,同时满足这两类二叉
|
||||
}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title=""
|
||||
/* AVL 树节点类 */
|
||||
class TreeNode {
|
||||
var val: Int // 节点值
|
||||
var height: Int // 节点高度
|
||||
var left: TreeNode? // 左子节点
|
||||
var right: TreeNode? // 右子节点
|
||||
|
||||
init(x: Int) {
|
||||
val = x
|
||||
height = 0
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
=== "JS"
|
||||
|
||||
```javascript title=""
|
||||
@@ -107,6 +137,25 @@ AVL 树既是二叉搜索树也是平衡二叉树,同时满足这两类二叉
|
||||
}
|
||||
```
|
||||
|
||||
=== "Dart"
|
||||
|
||||
```dart title=""
|
||||
/* AVL 树节点类 */
|
||||
class TreeNode {
|
||||
int val; // 节点值
|
||||
int height; // 节点高度
|
||||
TreeNode? left; // 左子节点
|
||||
TreeNode? right; // 右子节点
|
||||
TreeNode(this.val, [this.height = 0, this.left, this.right]);
|
||||
}
|
||||
```
|
||||
|
||||
=== "Rust"
|
||||
|
||||
```rust title=""
|
||||
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title=""
|
||||
@@ -133,69 +182,20 @@ AVL 树既是二叉搜索树也是平衡二叉树,同时满足这两类二叉
|
||||
}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title=""
|
||||
/* AVL 树节点类 */
|
||||
class TreeNode {
|
||||
public int val; // 节点值
|
||||
public int height; // 节点高度
|
||||
public TreeNode? left; // 左子节点
|
||||
public TreeNode? right; // 右子节点
|
||||
public TreeNode(int x) { val = x; }
|
||||
}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title=""
|
||||
/* AVL 树节点类 */
|
||||
class TreeNode {
|
||||
var val: Int // 节点值
|
||||
var height: Int // 节点高度
|
||||
var left: TreeNode? // 左子节点
|
||||
var right: TreeNode? // 右子节点
|
||||
|
||||
init(x: Int) {
|
||||
val = x
|
||||
height = 0
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title=""
|
||||
|
||||
```
|
||||
|
||||
=== "Dart"
|
||||
|
||||
```dart title=""
|
||||
/* AVL 树节点类 */
|
||||
class TreeNode {
|
||||
int val; // 节点值
|
||||
int height; // 节点高度
|
||||
TreeNode? left; // 左子节点
|
||||
TreeNode? right; // 右子节点
|
||||
TreeNode(this.val, [this.height = 0, this.left, this.right]);
|
||||
}
|
||||
```
|
||||
|
||||
=== "Rust"
|
||||
|
||||
```rust title=""
|
||||
|
||||
```
|
||||
|
||||
“节点高度”是指从该节点到最远叶节点的距离,即所经过的“边”的数量。需要特别注意的是,叶节点的高度为 0 ,而空节点的高度为 -1 。我们将创建两个工具函数,分别用于获取和更新节点的高度。
|
||||
|
||||
=== "Java"
|
||||
=== "Python"
|
||||
|
||||
```java title="avl_tree.java"
|
||||
```python title="avl_tree.py"
|
||||
[class]{AVLTree}-[func]{height}
|
||||
|
||||
[class]{AVLTree}-[func]{updateHeight}
|
||||
[class]{AVLTree}-[func]{__update_height}
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
@@ -206,12 +206,20 @@ AVL 树既是二叉搜索树也是平衡二叉树,同时满足这两类二叉
|
||||
[class]{AVLTree}-[func]{updateHeight}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
=== "Java"
|
||||
|
||||
```python title="avl_tree.py"
|
||||
```java title="avl_tree.java"
|
||||
[class]{AVLTree}-[func]{height}
|
||||
|
||||
[class]{AVLTree}-[func]{__update_height}
|
||||
[class]{AVLTree}-[func]{updateHeight}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="avl_tree.cs"
|
||||
[class]{AVLTree}-[func]{height}
|
||||
|
||||
[class]{AVLTree}-[func]{updateHeight}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
@@ -222,6 +230,14 @@ AVL 树既是二叉搜索树也是平衡二叉树,同时满足这两类二叉
|
||||
[class]{aVLTree}-[func]{updateHeight}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="avl_tree.swift"
|
||||
[class]{AVLTree}-[func]{height}
|
||||
|
||||
[class]{AVLTree}-[func]{updateHeight}
|
||||
```
|
||||
|
||||
=== "JS"
|
||||
|
||||
```javascript title="avl_tree.js"
|
||||
@@ -238,38 +254,6 @@ AVL 树既是二叉搜索树也是平衡二叉树,同时满足这两类二叉
|
||||
[class]{AVLTree}-[func]{updateHeight}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="avl_tree.c"
|
||||
[class]{}-[func]{height}
|
||||
|
||||
[class]{}-[func]{updateHeight}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="avl_tree.cs"
|
||||
[class]{AVLTree}-[func]{height}
|
||||
|
||||
[class]{AVLTree}-[func]{updateHeight}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="avl_tree.swift"
|
||||
[class]{AVLTree}-[func]{height}
|
||||
|
||||
[class]{AVLTree}-[func]{updateHeight}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="avl_tree.zig"
|
||||
[class]{AVLTree}-[func]{height}
|
||||
|
||||
[class]{AVLTree}-[func]{updateHeight}
|
||||
```
|
||||
|
||||
=== "Dart"
|
||||
|
||||
```dart title="avl_tree.dart"
|
||||
@@ -286,14 +270,30 @@ AVL 树既是二叉搜索树也是平衡二叉树,同时满足这两类二叉
|
||||
[class]{AVLTree}-[func]{update_height}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="avl_tree.c"
|
||||
[class]{}-[func]{height}
|
||||
|
||||
[class]{}-[func]{updateHeight}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="avl_tree.zig"
|
||||
[class]{AVLTree}-[func]{height}
|
||||
|
||||
[class]{AVLTree}-[func]{updateHeight}
|
||||
```
|
||||
|
||||
### 节点平衡因子
|
||||
|
||||
节点的「平衡因子 balance factor」定义为节点左子树的高度减去右子树的高度,同时规定空节点的平衡因子为 0 。我们同样将获取节点平衡因子的功能封装成函数,方便后续使用。
|
||||
|
||||
=== "Java"
|
||||
=== "Python"
|
||||
|
||||
```java title="avl_tree.java"
|
||||
[class]{AVLTree}-[func]{balanceFactor}
|
||||
```python title="avl_tree.py"
|
||||
[class]{AVLTree}-[func]{balance_factor}
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
@@ -302,10 +302,16 @@ AVL 树既是二叉搜索树也是平衡二叉树,同时满足这两类二叉
|
||||
[class]{AVLTree}-[func]{balanceFactor}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
=== "Java"
|
||||
|
||||
```python title="avl_tree.py"
|
||||
[class]{AVLTree}-[func]{balance_factor}
|
||||
```java title="avl_tree.java"
|
||||
[class]{AVLTree}-[func]{balanceFactor}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="avl_tree.cs"
|
||||
[class]{AVLTree}-[func]{balanceFactor}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
@@ -314,6 +320,12 @@ AVL 树既是二叉搜索树也是平衡二叉树,同时满足这两类二叉
|
||||
[class]{aVLTree}-[func]{balanceFactor}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="avl_tree.swift"
|
||||
[class]{AVLTree}-[func]{balanceFactor}
|
||||
```
|
||||
|
||||
=== "JS"
|
||||
|
||||
```javascript title="avl_tree.js"
|
||||
@@ -326,30 +338,6 @@ AVL 树既是二叉搜索树也是平衡二叉树,同时满足这两类二叉
|
||||
[class]{AVLTree}-[func]{balanceFactor}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="avl_tree.c"
|
||||
[class]{}-[func]{balanceFactor}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="avl_tree.cs"
|
||||
[class]{AVLTree}-[func]{balanceFactor}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="avl_tree.swift"
|
||||
[class]{AVLTree}-[func]{balanceFactor}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="avl_tree.zig"
|
||||
[class]{AVLTree}-[func]{balanceFactor}
|
||||
```
|
||||
|
||||
=== "Dart"
|
||||
|
||||
```dart title="avl_tree.dart"
|
||||
@@ -362,6 +350,18 @@ AVL 树既是二叉搜索树也是平衡二叉树,同时满足这两类二叉
|
||||
[class]{AVLTree}-[func]{balance_factor}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="avl_tree.c"
|
||||
[class]{}-[func]{balanceFactor}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="avl_tree.zig"
|
||||
[class]{AVLTree}-[func]{balanceFactor}
|
||||
```
|
||||
|
||||
!!! note
|
||||
|
||||
设平衡因子为 $f$ ,则一棵 AVL 树的任意节点的平衡因子皆满足 $-1 \le f \le 1$ 。
|
||||
@@ -394,10 +394,10 @@ AVL 树的特点在于“旋转”操作,它能够在不影响二叉树的中
|
||||
|
||||
“向右旋转”是一种形象化的说法,实际上需要通过修改节点指针来实现,代码如下所示。
|
||||
|
||||
=== "Java"
|
||||
=== "Python"
|
||||
|
||||
```java title="avl_tree.java"
|
||||
[class]{AVLTree}-[func]{rightRotate}
|
||||
```python title="avl_tree.py"
|
||||
[class]{AVLTree}-[func]{__right_rotate}
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
@@ -406,10 +406,16 @@ AVL 树的特点在于“旋转”操作,它能够在不影响二叉树的中
|
||||
[class]{AVLTree}-[func]{rightRotate}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
=== "Java"
|
||||
|
||||
```python title="avl_tree.py"
|
||||
[class]{AVLTree}-[func]{__right_rotate}
|
||||
```java title="avl_tree.java"
|
||||
[class]{AVLTree}-[func]{rightRotate}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="avl_tree.cs"
|
||||
[class]{AVLTree}-[func]{rightRotate}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
@@ -418,6 +424,12 @@ AVL 树的特点在于“旋转”操作,它能够在不影响二叉树的中
|
||||
[class]{aVLTree}-[func]{rightRotate}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="avl_tree.swift"
|
||||
[class]{AVLTree}-[func]{rightRotate}
|
||||
```
|
||||
|
||||
=== "JS"
|
||||
|
||||
```javascript title="avl_tree.js"
|
||||
@@ -430,30 +442,6 @@ AVL 树的特点在于“旋转”操作,它能够在不影响二叉树的中
|
||||
[class]{AVLTree}-[func]{rightRotate}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="avl_tree.c"
|
||||
[class]{}-[func]{rightRotate}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="avl_tree.cs"
|
||||
[class]{AVLTree}-[func]{rightRotate}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="avl_tree.swift"
|
||||
[class]{AVLTree}-[func]{rightRotate}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="avl_tree.zig"
|
||||
[class]{AVLTree}-[func]{rightRotate}
|
||||
```
|
||||
|
||||
=== "Dart"
|
||||
|
||||
```dart title="avl_tree.dart"
|
||||
@@ -466,6 +454,18 @@ AVL 树的特点在于“旋转”操作,它能够在不影响二叉树的中
|
||||
[class]{AVLTree}-[func]{right_rotate}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="avl_tree.c"
|
||||
[class]{}-[func]{rightRotate}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="avl_tree.zig"
|
||||
[class]{AVLTree}-[func]{rightRotate}
|
||||
```
|
||||
|
||||
### 左旋
|
||||
|
||||
相应的,如果考虑上述失衡二叉树的“镜像”,则需要执行下图所示的“左旋”操作。
|
||||
@@ -478,10 +478,10 @@ AVL 树的特点在于“旋转”操作,它能够在不影响二叉树的中
|
||||
|
||||
可以观察到,**右旋和左旋操作在逻辑上是镜像对称的,它们分别解决的两种失衡情况也是对称的**。基于对称性,我们只需将右旋的实现代码中的所有的 `left` 替换为 `right` ,将所有的 `right` 替换为 `left` ,即可得到左旋的实现代码。
|
||||
|
||||
=== "Java"
|
||||
=== "Python"
|
||||
|
||||
```java title="avl_tree.java"
|
||||
[class]{AVLTree}-[func]{leftRotate}
|
||||
```python title="avl_tree.py"
|
||||
[class]{AVLTree}-[func]{__left_rotate}
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
@@ -490,10 +490,16 @@ AVL 树的特点在于“旋转”操作,它能够在不影响二叉树的中
|
||||
[class]{AVLTree}-[func]{leftRotate}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
=== "Java"
|
||||
|
||||
```python title="avl_tree.py"
|
||||
[class]{AVLTree}-[func]{__left_rotate}
|
||||
```java title="avl_tree.java"
|
||||
[class]{AVLTree}-[func]{leftRotate}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="avl_tree.cs"
|
||||
[class]{AVLTree}-[func]{leftRotate}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
@@ -502,6 +508,12 @@ AVL 树的特点在于“旋转”操作,它能够在不影响二叉树的中
|
||||
[class]{aVLTree}-[func]{leftRotate}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="avl_tree.swift"
|
||||
[class]{AVLTree}-[func]{leftRotate}
|
||||
```
|
||||
|
||||
=== "JS"
|
||||
|
||||
```javascript title="avl_tree.js"
|
||||
@@ -514,30 +526,6 @@ AVL 树的特点在于“旋转”操作,它能够在不影响二叉树的中
|
||||
[class]{AVLTree}-[func]{leftRotate}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="avl_tree.c"
|
||||
[class]{}-[func]{leftRotate}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="avl_tree.cs"
|
||||
[class]{AVLTree}-[func]{leftRotate}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="avl_tree.swift"
|
||||
[class]{AVLTree}-[func]{leftRotate}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="avl_tree.zig"
|
||||
[class]{AVLTree}-[func]{leftRotate}
|
||||
```
|
||||
|
||||
=== "Dart"
|
||||
|
||||
```dart title="avl_tree.dart"
|
||||
@@ -550,6 +538,18 @@ AVL 树的特点在于“旋转”操作,它能够在不影响二叉树的中
|
||||
[class]{AVLTree}-[func]{left_rotate}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="avl_tree.c"
|
||||
[class]{}-[func]{leftRotate}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="avl_tree.zig"
|
||||
[class]{AVLTree}-[func]{leftRotate}
|
||||
```
|
||||
|
||||
### 先左旋后右旋
|
||||
|
||||
对于下图中的失衡节点 3 ,仅使用左旋或右旋都无法使子树恢复平衡。此时需要先对 `child` 执行“左旋”,再对 `node` 执行“右旋”。
|
||||
@@ -581,10 +581,10 @@ AVL 树的特点在于“旋转”操作,它能够在不影响二叉树的中
|
||||
|
||||
为了便于使用,我们将旋转操作封装成一个函数。**有了这个函数,我们就能对各种失衡情况进行旋转,使失衡节点重新恢复平衡**。
|
||||
|
||||
=== "Java"
|
||||
=== "Python"
|
||||
|
||||
```java title="avl_tree.java"
|
||||
[class]{AVLTree}-[func]{rotate}
|
||||
```python title="avl_tree.py"
|
||||
[class]{AVLTree}-[func]{__rotate}
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
@@ -593,10 +593,16 @@ AVL 树的特点在于“旋转”操作,它能够在不影响二叉树的中
|
||||
[class]{AVLTree}-[func]{rotate}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
=== "Java"
|
||||
|
||||
```python title="avl_tree.py"
|
||||
[class]{AVLTree}-[func]{__rotate}
|
||||
```java title="avl_tree.java"
|
||||
[class]{AVLTree}-[func]{rotate}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="avl_tree.cs"
|
||||
[class]{AVLTree}-[func]{rotate}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
@@ -605,6 +611,12 @@ AVL 树的特点在于“旋转”操作,它能够在不影响二叉树的中
|
||||
[class]{aVLTree}-[func]{rotate}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="avl_tree.swift"
|
||||
[class]{AVLTree}-[func]{rotate}
|
||||
```
|
||||
|
||||
=== "JS"
|
||||
|
||||
```javascript title="avl_tree.js"
|
||||
@@ -617,30 +629,6 @@ AVL 树的特点在于“旋转”操作,它能够在不影响二叉树的中
|
||||
[class]{AVLTree}-[func]{rotate}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="avl_tree.c"
|
||||
[class]{}-[func]{rotate}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="avl_tree.cs"
|
||||
[class]{AVLTree}-[func]{rotate}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="avl_tree.swift"
|
||||
[class]{AVLTree}-[func]{rotate}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="avl_tree.zig"
|
||||
[class]{AVLTree}-[func]{rotate}
|
||||
```
|
||||
|
||||
=== "Dart"
|
||||
|
||||
```dart title="avl_tree.dart"
|
||||
@@ -653,18 +641,30 @@ AVL 树的特点在于“旋转”操作,它能够在不影响二叉树的中
|
||||
[class]{AVLTree}-[func]{rotate}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="avl_tree.c"
|
||||
[class]{}-[func]{rotate}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="avl_tree.zig"
|
||||
[class]{AVLTree}-[func]{rotate}
|
||||
```
|
||||
|
||||
## AVL 树常用操作
|
||||
|
||||
### 插入节点
|
||||
|
||||
AVL 树的节点插入操作与二叉搜索树在主体上类似。唯一的区别在于,在 AVL 树中插入节点后,从该节点到根节点的路径上可能会出现一系列失衡节点。因此,**我们需要从这个节点开始,自底向上执行旋转操作,使所有失衡节点恢复平衡**。
|
||||
|
||||
=== "Java"
|
||||
=== "Python"
|
||||
|
||||
```java title="avl_tree.java"
|
||||
```python title="avl_tree.py"
|
||||
[class]{AVLTree}-[func]{insert}
|
||||
|
||||
[class]{AVLTree}-[func]{insertHelper}
|
||||
[class]{AVLTree}-[func]{__insert_helper}
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
@@ -675,12 +675,20 @@ AVL 树的节点插入操作与二叉搜索树在主体上类似。唯一的区
|
||||
[class]{AVLTree}-[func]{insertHelper}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
=== "Java"
|
||||
|
||||
```python title="avl_tree.py"
|
||||
```java title="avl_tree.java"
|
||||
[class]{AVLTree}-[func]{insert}
|
||||
|
||||
[class]{AVLTree}-[func]{__insert_helper}
|
||||
[class]{AVLTree}-[func]{insertHelper}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="avl_tree.cs"
|
||||
[class]{AVLTree}-[func]{insert}
|
||||
|
||||
[class]{AVLTree}-[func]{insertHelper}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
@@ -691,6 +699,14 @@ AVL 树的节点插入操作与二叉搜索树在主体上类似。唯一的区
|
||||
[class]{aVLTree}-[func]{insertHelper}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="avl_tree.swift"
|
||||
[class]{AVLTree}-[func]{insert}
|
||||
|
||||
[class]{AVLTree}-[func]{insertHelper}
|
||||
```
|
||||
|
||||
=== "JS"
|
||||
|
||||
```javascript title="avl_tree.js"
|
||||
@@ -707,38 +723,6 @@ AVL 树的节点插入操作与二叉搜索树在主体上类似。唯一的区
|
||||
[class]{AVLTree}-[func]{insertHelper}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="avl_tree.c"
|
||||
[class]{aVLTree}-[func]{insert}
|
||||
|
||||
[class]{}-[func]{insertHelper}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="avl_tree.cs"
|
||||
[class]{AVLTree}-[func]{insert}
|
||||
|
||||
[class]{AVLTree}-[func]{insertHelper}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="avl_tree.swift"
|
||||
[class]{AVLTree}-[func]{insert}
|
||||
|
||||
[class]{AVLTree}-[func]{insertHelper}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="avl_tree.zig"
|
||||
[class]{AVLTree}-[func]{insert}
|
||||
|
||||
[class]{AVLTree}-[func]{insertHelper}
|
||||
```
|
||||
|
||||
=== "Dart"
|
||||
|
||||
```dart title="avl_tree.dart"
|
||||
@@ -755,16 +739,32 @@ AVL 树的节点插入操作与二叉搜索树在主体上类似。唯一的区
|
||||
[class]{AVLTree}-[func]{insert_helper}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="avl_tree.c"
|
||||
[class]{aVLTree}-[func]{insert}
|
||||
|
||||
[class]{}-[func]{insertHelper}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="avl_tree.zig"
|
||||
[class]{AVLTree}-[func]{insert}
|
||||
|
||||
[class]{AVLTree}-[func]{insertHelper}
|
||||
```
|
||||
|
||||
### 删除节点
|
||||
|
||||
类似地,在二叉搜索树的删除节点方法的基础上,需要从底至顶地执行旋转操作,使所有失衡节点恢复平衡。
|
||||
|
||||
=== "Java"
|
||||
=== "Python"
|
||||
|
||||
```java title="avl_tree.java"
|
||||
```python title="avl_tree.py"
|
||||
[class]{AVLTree}-[func]{remove}
|
||||
|
||||
[class]{AVLTree}-[func]{removeHelper}
|
||||
[class]{AVLTree}-[func]{__remove_helper}
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
@@ -775,12 +775,20 @@ AVL 树的节点插入操作与二叉搜索树在主体上类似。唯一的区
|
||||
[class]{AVLTree}-[func]{removeHelper}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
=== "Java"
|
||||
|
||||
```python title="avl_tree.py"
|
||||
```java title="avl_tree.java"
|
||||
[class]{AVLTree}-[func]{remove}
|
||||
|
||||
[class]{AVLTree}-[func]{__remove_helper}
|
||||
[class]{AVLTree}-[func]{removeHelper}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="avl_tree.cs"
|
||||
[class]{AVLTree}-[func]{remove}
|
||||
|
||||
[class]{AVLTree}-[func]{removeHelper}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
@@ -791,6 +799,14 @@ AVL 树的节点插入操作与二叉搜索树在主体上类似。唯一的区
|
||||
[class]{aVLTree}-[func]{removeHelper}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="avl_tree.swift"
|
||||
[class]{AVLTree}-[func]{remove}
|
||||
|
||||
[class]{AVLTree}-[func]{removeHelper}
|
||||
```
|
||||
|
||||
=== "JS"
|
||||
|
||||
```javascript title="avl_tree.js"
|
||||
@@ -807,38 +823,6 @@ AVL 树的节点插入操作与二叉搜索树在主体上类似。唯一的区
|
||||
[class]{AVLTree}-[func]{removeHelper}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="avl_tree.c"
|
||||
[class]{aVLTree}-[func]{removeNode}
|
||||
|
||||
[class]{}-[func]{removeHelper}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="avl_tree.cs"
|
||||
[class]{AVLTree}-[func]{remove}
|
||||
|
||||
[class]{AVLTree}-[func]{removeHelper}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="avl_tree.swift"
|
||||
[class]{AVLTree}-[func]{remove}
|
||||
|
||||
[class]{AVLTree}-[func]{removeHelper}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="avl_tree.zig"
|
||||
[class]{AVLTree}-[func]{remove}
|
||||
|
||||
[class]{AVLTree}-[func]{removeHelper}
|
||||
```
|
||||
|
||||
=== "Dart"
|
||||
|
||||
```dart title="avl_tree.dart"
|
||||
@@ -855,6 +839,22 @@ AVL 树的节点插入操作与二叉搜索树在主体上类似。唯一的区
|
||||
[class]{AVLTree}-[func]{remove_helper}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="avl_tree.c"
|
||||
[class]{aVLTree}-[func]{removeNode}
|
||||
|
||||
[class]{}-[func]{removeHelper}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="avl_tree.zig"
|
||||
[class]{AVLTree}-[func]{remove}
|
||||
|
||||
[class]{AVLTree}-[func]{removeHelper}
|
||||
```
|
||||
|
||||
### 查找节点
|
||||
|
||||
AVL 树的节点查找操作与二叉搜索树一致,在此不再赘述。
|
||||
|
||||
@@ -33,9 +33,9 @@
|
||||
|
||||
二叉搜索树的查找操作与二分查找算法的工作原理一致,都是每轮排除一半情况。循环次数最多为二叉树的高度,当二叉树平衡时,使用 $O(\log n)$ 时间。
|
||||
|
||||
=== "Java"
|
||||
=== "Python"
|
||||
|
||||
```java title="binary_search_tree.java"
|
||||
```python title="binary_search_tree.py"
|
||||
[class]{BinarySearchTree}-[func]{search}
|
||||
```
|
||||
|
||||
@@ -45,9 +45,15 @@
|
||||
[class]{BinarySearchTree}-[func]{search}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
=== "Java"
|
||||
|
||||
```python title="binary_search_tree.py"
|
||||
```java title="binary_search_tree.java"
|
||||
[class]{BinarySearchTree}-[func]{search}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="binary_search_tree.cs"
|
||||
[class]{BinarySearchTree}-[func]{search}
|
||||
```
|
||||
|
||||
@@ -57,6 +63,12 @@
|
||||
[class]{binarySearchTree}-[func]{search}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="binary_search_tree.swift"
|
||||
[class]{BinarySearchTree}-[func]{search}
|
||||
```
|
||||
|
||||
=== "JS"
|
||||
|
||||
```javascript title="binary_search_tree.js"
|
||||
@@ -69,30 +81,6 @@
|
||||
[class]{BinarySearchTree}-[func]{search}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="binary_search_tree.c"
|
||||
[class]{binarySearchTree}-[func]{search}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="binary_search_tree.cs"
|
||||
[class]{BinarySearchTree}-[func]{search}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="binary_search_tree.swift"
|
||||
[class]{BinarySearchTree}-[func]{search}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="binary_search_tree.zig"
|
||||
[class]{BinarySearchTree}-[func]{search}
|
||||
```
|
||||
|
||||
=== "Dart"
|
||||
|
||||
```dart title="binary_search_tree.dart"
|
||||
@@ -105,6 +93,18 @@
|
||||
[class]{BinarySearchTree}-[func]{search}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="binary_search_tree.c"
|
||||
[class]{binarySearchTree}-[func]{search}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="binary_search_tree.zig"
|
||||
[class]{BinarySearchTree}-[func]{search}
|
||||
```
|
||||
|
||||
### 插入节点
|
||||
|
||||
给定一个待插入元素 `num` ,为了保持二叉搜索树“左子树 < 根节点 < 右子树”的性质,插入操作流程如下图所示。
|
||||
@@ -119,9 +119,9 @@
|
||||
- 二叉搜索树不允许存在重复节点,否则将违反其定义。因此,若待插入节点在树中已存在,则不执行插入,直接返回。
|
||||
- 为了实现插入节点,我们需要借助节点 `pre` 保存上一轮循环的节点。这样在遍历至 $\text{None}$ 时,我们可以获取到其父节点,从而完成节点插入操作。
|
||||
|
||||
=== "Java"
|
||||
=== "Python"
|
||||
|
||||
```java title="binary_search_tree.java"
|
||||
```python title="binary_search_tree.py"
|
||||
[class]{BinarySearchTree}-[func]{insert}
|
||||
```
|
||||
|
||||
@@ -131,9 +131,15 @@
|
||||
[class]{BinarySearchTree}-[func]{insert}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
=== "Java"
|
||||
|
||||
```python title="binary_search_tree.py"
|
||||
```java title="binary_search_tree.java"
|
||||
[class]{BinarySearchTree}-[func]{insert}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="binary_search_tree.cs"
|
||||
[class]{BinarySearchTree}-[func]{insert}
|
||||
```
|
||||
|
||||
@@ -143,6 +149,12 @@
|
||||
[class]{binarySearchTree}-[func]{insert}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="binary_search_tree.swift"
|
||||
[class]{BinarySearchTree}-[func]{insert}
|
||||
```
|
||||
|
||||
=== "JS"
|
||||
|
||||
```javascript title="binary_search_tree.js"
|
||||
@@ -155,30 +167,6 @@
|
||||
[class]{BinarySearchTree}-[func]{insert}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="binary_search_tree.c"
|
||||
[class]{binarySearchTree}-[func]{insert}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="binary_search_tree.cs"
|
||||
[class]{BinarySearchTree}-[func]{insert}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="binary_search_tree.swift"
|
||||
[class]{BinarySearchTree}-[func]{insert}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="binary_search_tree.zig"
|
||||
[class]{BinarySearchTree}-[func]{insert}
|
||||
```
|
||||
|
||||
=== "Dart"
|
||||
|
||||
```dart title="binary_search_tree.dart"
|
||||
@@ -191,6 +179,18 @@
|
||||
[class]{BinarySearchTree}-[func]{insert}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="binary_search_tree.c"
|
||||
[class]{binarySearchTree}-[func]{insert}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="binary_search_tree.zig"
|
||||
[class]{BinarySearchTree}-[func]{insert}
|
||||
```
|
||||
|
||||
与查找节点相同,插入节点使用 $O(\log n)$ 时间。
|
||||
|
||||
### 删除节点
|
||||
@@ -230,9 +230,9 @@
|
||||
|
||||
删除节点操作同样使用 $O(\log n)$ 时间,其中查找待删除节点需要 $O(\log n)$ 时间,获取中序遍历后继节点需要 $O(\log n)$ 时间。
|
||||
|
||||
=== "Java"
|
||||
=== "Python"
|
||||
|
||||
```java title="binary_search_tree.java"
|
||||
```python title="binary_search_tree.py"
|
||||
[class]{BinarySearchTree}-[func]{remove}
|
||||
```
|
||||
|
||||
@@ -242,9 +242,15 @@
|
||||
[class]{BinarySearchTree}-[func]{remove}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
=== "Java"
|
||||
|
||||
```python title="binary_search_tree.py"
|
||||
```java title="binary_search_tree.java"
|
||||
[class]{BinarySearchTree}-[func]{remove}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="binary_search_tree.cs"
|
||||
[class]{BinarySearchTree}-[func]{remove}
|
||||
```
|
||||
|
||||
@@ -254,6 +260,12 @@
|
||||
[class]{binarySearchTree}-[func]{remove}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="binary_search_tree.swift"
|
||||
[class]{BinarySearchTree}-[func]{remove}
|
||||
```
|
||||
|
||||
=== "JS"
|
||||
|
||||
```javascript title="binary_search_tree.js"
|
||||
@@ -266,30 +278,6 @@
|
||||
[class]{BinarySearchTree}-[func]{remove}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="binary_search_tree.c"
|
||||
[class]{binarySearchTree}-[func]{removeNode}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="binary_search_tree.cs"
|
||||
[class]{BinarySearchTree}-[func]{remove}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="binary_search_tree.swift"
|
||||
[class]{BinarySearchTree}-[func]{remove}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="binary_search_tree.zig"
|
||||
[class]{BinarySearchTree}-[func]{remove}
|
||||
```
|
||||
|
||||
=== "Dart"
|
||||
|
||||
```dart title="binary_search_tree.dart"
|
||||
@@ -302,6 +290,18 @@
|
||||
[class]{BinarySearchTree}-[func]{remove}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="binary_search_tree.c"
|
||||
[class]{binarySearchTree}-[func]{removeNode}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="binary_search_tree.zig"
|
||||
[class]{BinarySearchTree}-[func]{remove}
|
||||
```
|
||||
|
||||
### 中序遍历有序
|
||||
|
||||
如下图所示,二叉树的中序遍历遵循“左 $\rightarrow$ 根 $\rightarrow$ 右”的遍历顺序,而二叉搜索树满足“左子节点 $<$ 根节点 $<$ 右子节点”的大小关系。
|
||||
|
||||
+203
-203
@@ -2,16 +2,15 @@
|
||||
|
||||
「二叉树 binary tree」是一种非线性数据结构,代表着祖先与后代之间的派生关系,体现着“一分为二”的分治逻辑。与链表类似,二叉树的基本单元是节点,每个节点包含:值、左子节点引用、右子节点引用。
|
||||
|
||||
=== "Java"
|
||||
=== "Python"
|
||||
|
||||
```java title=""
|
||||
/* 二叉树节点类 */
|
||||
class TreeNode {
|
||||
int val; // 节点值
|
||||
TreeNode left; // 左子节点引用
|
||||
TreeNode right; // 右子节点引用
|
||||
TreeNode(int x) { val = x; }
|
||||
}
|
||||
```python title=""
|
||||
class TreeNode:
|
||||
"""二叉树节点类"""
|
||||
def __init__(self, val: int):
|
||||
self.val: int = val # 节点值
|
||||
self.left: Optional[TreeNode] = None # 左子节点引用
|
||||
self.right: Optional[TreeNode] = None # 右子节点引用
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
@@ -26,15 +25,28 @@
|
||||
};
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
=== "Java"
|
||||
|
||||
```python title=""
|
||||
class TreeNode:
|
||||
"""二叉树节点类"""
|
||||
def __init__(self, val: int):
|
||||
self.val: int = val # 节点值
|
||||
self.left: Optional[TreeNode] = None # 左子节点引用
|
||||
self.right: Optional[TreeNode] = None # 右子节点引用
|
||||
```java title=""
|
||||
/* 二叉树节点类 */
|
||||
class TreeNode {
|
||||
int val; // 节点值
|
||||
TreeNode left; // 左子节点引用
|
||||
TreeNode right; // 右子节点引用
|
||||
TreeNode(int x) { val = x; }
|
||||
}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title=""
|
||||
/* 二叉树节点类 */
|
||||
class TreeNode {
|
||||
int val; // 节点值
|
||||
TreeNode? left; // 左子节点引用
|
||||
TreeNode? right; // 右子节点引用
|
||||
TreeNode(int x) { val = x; }
|
||||
}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
@@ -56,6 +68,21 @@
|
||||
}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title=""
|
||||
/* 二叉树节点类 */
|
||||
class TreeNode {
|
||||
var val: Int // 节点值
|
||||
var left: TreeNode? // 左子节点引用
|
||||
var right: TreeNode? // 右子节点引用
|
||||
|
||||
init(x: Int) {
|
||||
val = x
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
=== "JS"
|
||||
|
||||
```javascript title=""
|
||||
@@ -84,6 +111,24 @@
|
||||
}
|
||||
```
|
||||
|
||||
=== "Dart"
|
||||
|
||||
```dart title=""
|
||||
/* 二叉树节点类 */
|
||||
class TreeNode {
|
||||
int val; // 节点值
|
||||
TreeNode? left; // 左子节点引用
|
||||
TreeNode? right; // 右子节点引用
|
||||
TreeNode(this.val, [this.left, this.right]);
|
||||
}
|
||||
```
|
||||
|
||||
=== "Rust"
|
||||
|
||||
```rust title=""
|
||||
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title=""
|
||||
@@ -110,57 +155,12 @@
|
||||
}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title=""
|
||||
/* 二叉树节点类 */
|
||||
class TreeNode {
|
||||
int val; // 节点值
|
||||
TreeNode? left; // 左子节点引用
|
||||
TreeNode? right; // 右子节点引用
|
||||
TreeNode(int x) { val = x; }
|
||||
}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title=""
|
||||
/* 二叉树节点类 */
|
||||
class TreeNode {
|
||||
var val: Int // 节点值
|
||||
var left: TreeNode? // 左子节点引用
|
||||
var right: TreeNode? // 右子节点引用
|
||||
|
||||
init(x: Int) {
|
||||
val = x
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title=""
|
||||
|
||||
```
|
||||
|
||||
=== "Dart"
|
||||
|
||||
```dart title=""
|
||||
/* 二叉树节点类 */
|
||||
class TreeNode {
|
||||
int val; // 节点值
|
||||
TreeNode? left; // 左子节点引用
|
||||
TreeNode? right; // 右子节点引用
|
||||
TreeNode(this.val, [this.left, this.right]);
|
||||
}
|
||||
```
|
||||
|
||||
=== "Rust"
|
||||
|
||||
```rust title=""
|
||||
|
||||
```
|
||||
|
||||
每个节点都有两个引用(指针),分别指向「左子节点 left-child node」和「右子节点 right-child node」,该节点被称为这两个子节点的「父节点 parent node」。当给定一个二叉树的节点时,我们将该节点的左子节点及其以下节点形成的树称为该节点的「左子树 left subtree」,同理可得「右子树 right subtree」。
|
||||
|
||||
**在二叉树中,除叶节点外,其他所有节点都包含子节点和非空子树**。如下图所示,如果将“节点 2”视为父节点,则其左子节点和右子节点分别是“节点 4”和“节点 5”,左子树是“节点 4 及其以下节点形成的树”,右子树是“节点 5 及其以下节点形成的树”。
|
||||
@@ -192,20 +192,21 @@
|
||||
|
||||
与链表类似,首先初始化节点,然后构建引用(指针)。
|
||||
|
||||
=== "Java"
|
||||
=== "Python"
|
||||
|
||||
```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;
|
||||
```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
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
@@ -225,21 +226,37 @@
|
||||
n2->right = n5;
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
=== "Java"
|
||||
|
||||
```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
|
||||
```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 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;
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
@@ -259,6 +276,22 @@
|
||||
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"
|
||||
|
||||
```javascript title="binary_tree.js"
|
||||
@@ -293,62 +326,6 @@
|
||||
n2.right = n5;
|
||||
```
|
||||
|
||||
=== "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;
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="binary_tree.cs"
|
||||
/* 初始化二叉树 */
|
||||
// 初始化节点
|
||||
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;
|
||||
```
|
||||
|
||||
=== "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
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="binary_tree.zig"
|
||||
|
||||
```
|
||||
|
||||
=== "Dart"
|
||||
|
||||
```dart title="binary_tree.dart"
|
||||
@@ -372,21 +349,45 @@
|
||||
|
||||
```
|
||||
|
||||
=== "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;
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="binary_tree.zig"
|
||||
|
||||
```
|
||||
|
||||
### 插入与删除节点
|
||||
|
||||
与链表类似,在二叉树中插入与删除节点可以通过修改指针来实现。下图给出了一个示例。
|
||||
|
||||

|
||||
|
||||
=== "Java"
|
||||
=== "Python"
|
||||
|
||||
```java title="binary_tree.java"
|
||||
TreeNode P = new TreeNode(0);
|
||||
// 在 n1 -> n2 中间插入节点 P
|
||||
n1.left = P;
|
||||
P.left = n2;
|
||||
// 删除节点 P
|
||||
n1.left = n2;
|
||||
```python title="binary_tree.py"
|
||||
# 插入与删除节点
|
||||
p = TreeNode(0)
|
||||
# 在 n1 -> n2 中间插入节点 P
|
||||
n1.left = p
|
||||
p.left = n2
|
||||
# 删除节点 P
|
||||
n1.left = n2
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
@@ -401,16 +402,27 @@
|
||||
n1->left = n2;
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
=== "Java"
|
||||
|
||||
```python title="binary_tree.py"
|
||||
# 插入与删除节点
|
||||
p = TreeNode(0)
|
||||
# 在 n1 -> n2 中间插入节点 P
|
||||
n1.left = p
|
||||
p.left = n2
|
||||
# 删除节点 P
|
||||
n1.left = n2
|
||||
```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 TreeNode(0);
|
||||
// 在 n1 -> n2 中间插入节点 P
|
||||
n1.left = P;
|
||||
P.left = n2;
|
||||
// 删除节点 P
|
||||
n1.left = n2;
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
@@ -425,6 +437,17 @@
|
||||
n1.Left = n2
|
||||
```
|
||||
|
||||
=== "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"
|
||||
|
||||
```javascript title="binary_tree.js"
|
||||
@@ -449,47 +472,6 @@
|
||||
n1.left = n2;
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="binary_tree.c"
|
||||
/* 插入与删除节点 */
|
||||
TreeNode *P = newTreeNode(0);
|
||||
// 在 n1 -> n2 中间插入节点 P
|
||||
n1->left = P;
|
||||
P->left = n2;
|
||||
// 删除节点 P
|
||||
n1->left = n2;
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="binary_tree.cs"
|
||||
/* 插入与删除节点 */
|
||||
TreeNode P = new TreeNode(0);
|
||||
// 在 n1 -> n2 中间插入节点 P
|
||||
n1.left = P;
|
||||
P.left = n2;
|
||||
// 删除节点 P
|
||||
n1.left = n2;
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="binary_tree.swift"
|
||||
let P = TreeNode(x: 0)
|
||||
// 在 n1 -> n2 中间插入节点 P
|
||||
n1.left = P
|
||||
P.left = n2
|
||||
// 删除节点 P
|
||||
n1.left = n2
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="binary_tree.zig"
|
||||
|
||||
```
|
||||
|
||||
=== "Dart"
|
||||
|
||||
```dart title="binary_tree.dart"
|
||||
@@ -508,6 +490,24 @@
|
||||
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="binary_tree.c"
|
||||
/* 插入与删除节点 */
|
||||
TreeNode *P = newTreeNode(0);
|
||||
// 在 n1 -> n2 中间插入节点 P
|
||||
n1->left = P;
|
||||
P->left = n2;
|
||||
// 删除节点 P
|
||||
n1->left = n2;
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="binary_tree.zig"
|
||||
|
||||
```
|
||||
|
||||
!!! note
|
||||
|
||||
需要注意的是,插入节点可能会改变二叉树的原有逻辑结构,而删除节点通常意味着删除该节点及其所有子树。因此,在二叉树中,插入与删除操作通常是由一套操作配合完成的,以实现有实际意义的操作。
|
||||
|
||||
@@ -16,10 +16,10 @@
|
||||
|
||||
广度优先遍历通常借助“队列”来实现。队列遵循“先进先出”的规则,而广度优先遍历则遵循“逐层推进”的规则,两者背后的思想是一致的。
|
||||
|
||||
=== "Java"
|
||||
=== "Python"
|
||||
|
||||
```java title="binary_tree_bfs.java"
|
||||
[class]{binary_tree_bfs}-[func]{levelOrder}
|
||||
```python title="binary_tree_bfs.py"
|
||||
[class]{}-[func]{level_order}
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
@@ -28,10 +28,16 @@
|
||||
[class]{}-[func]{levelOrder}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
=== "Java"
|
||||
|
||||
```python title="binary_tree_bfs.py"
|
||||
[class]{}-[func]{level_order}
|
||||
```java title="binary_tree_bfs.java"
|
||||
[class]{binary_tree_bfs}-[func]{levelOrder}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="binary_tree_bfs.cs"
|
||||
[class]{binary_tree_bfs}-[func]{levelOrder}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
@@ -40,6 +46,12 @@
|
||||
[class]{}-[func]{levelOrder}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="binary_tree_bfs.swift"
|
||||
[class]{}-[func]{levelOrder}
|
||||
```
|
||||
|
||||
=== "JS"
|
||||
|
||||
```javascript title="binary_tree_bfs.js"
|
||||
@@ -52,30 +64,6 @@
|
||||
[class]{}-[func]{levelOrder}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="binary_tree_bfs.c"
|
||||
[class]{}-[func]{levelOrder}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="binary_tree_bfs.cs"
|
||||
[class]{binary_tree_bfs}-[func]{levelOrder}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="binary_tree_bfs.swift"
|
||||
[class]{}-[func]{levelOrder}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="binary_tree_bfs.zig"
|
||||
[class]{}-[func]{levelOrder}
|
||||
```
|
||||
|
||||
=== "Dart"
|
||||
|
||||
```dart title="binary_tree_bfs.dart"
|
||||
@@ -88,6 +76,18 @@
|
||||
[class]{}-[func]{level_order}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="binary_tree_bfs.c"
|
||||
[class]{}-[func]{levelOrder}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="binary_tree_bfs.zig"
|
||||
[class]{}-[func]{levelOrder}
|
||||
```
|
||||
|
||||
### 复杂度分析
|
||||
|
||||
- **时间复杂度 $O(n)$** :所有节点被访问一次,使用 $O(n)$ 时间,其中 $n$ 为节点数量。
|
||||
@@ -105,14 +105,14 @@
|
||||
|
||||
深度优先搜索通常基于递归实现:
|
||||
|
||||
=== "Java"
|
||||
=== "Python"
|
||||
|
||||
```java title="binary_tree_dfs.java"
|
||||
[class]{binary_tree_dfs}-[func]{preOrder}
|
||||
```python title="binary_tree_dfs.py"
|
||||
[class]{}-[func]{pre_order}
|
||||
|
||||
[class]{binary_tree_dfs}-[func]{inOrder}
|
||||
[class]{}-[func]{in_order}
|
||||
|
||||
[class]{binary_tree_dfs}-[func]{postOrder}
|
||||
[class]{}-[func]{post_order}
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
@@ -125,14 +125,24 @@
|
||||
[class]{}-[func]{postOrder}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
=== "Java"
|
||||
|
||||
```python title="binary_tree_dfs.py"
|
||||
[class]{}-[func]{pre_order}
|
||||
```java title="binary_tree_dfs.java"
|
||||
[class]{binary_tree_dfs}-[func]{preOrder}
|
||||
|
||||
[class]{}-[func]{in_order}
|
||||
[class]{binary_tree_dfs}-[func]{inOrder}
|
||||
|
||||
[class]{}-[func]{post_order}
|
||||
[class]{binary_tree_dfs}-[func]{postOrder}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="binary_tree_dfs.cs"
|
||||
[class]{binary_tree_dfs}-[func]{preOrder}
|
||||
|
||||
[class]{binary_tree_dfs}-[func]{inOrder}
|
||||
|
||||
[class]{binary_tree_dfs}-[func]{postOrder}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
@@ -145,6 +155,16 @@
|
||||
[class]{}-[func]{postOrder}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="binary_tree_dfs.swift"
|
||||
[class]{}-[func]{preOrder}
|
||||
|
||||
[class]{}-[func]{inOrder}
|
||||
|
||||
[class]{}-[func]{postOrder}
|
||||
```
|
||||
|
||||
=== "JS"
|
||||
|
||||
```javascript title="binary_tree_dfs.js"
|
||||
@@ -165,46 +185,6 @@
|
||||
[class]{}-[func]{postOrder}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="binary_tree_dfs.c"
|
||||
[class]{}-[func]{preOrder}
|
||||
|
||||
[class]{}-[func]{inOrder}
|
||||
|
||||
[class]{}-[func]{postOrder}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="binary_tree_dfs.cs"
|
||||
[class]{binary_tree_dfs}-[func]{preOrder}
|
||||
|
||||
[class]{binary_tree_dfs}-[func]{inOrder}
|
||||
|
||||
[class]{binary_tree_dfs}-[func]{postOrder}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="binary_tree_dfs.swift"
|
||||
[class]{}-[func]{preOrder}
|
||||
|
||||
[class]{}-[func]{inOrder}
|
||||
|
||||
[class]{}-[func]{postOrder}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="binary_tree_dfs.zig"
|
||||
[class]{}-[func]{preOrder}
|
||||
|
||||
[class]{}-[func]{inOrder}
|
||||
|
||||
[class]{}-[func]{postOrder}
|
||||
```
|
||||
|
||||
=== "Dart"
|
||||
|
||||
```dart title="binary_tree_dfs.dart"
|
||||
@@ -225,6 +205,26 @@
|
||||
[class]{}-[func]{post_order}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="binary_tree_dfs.c"
|
||||
[class]{}-[func]{preOrder}
|
||||
|
||||
[class]{}-[func]{inOrder}
|
||||
|
||||
[class]{}-[func]{postOrder}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="binary_tree_dfs.zig"
|
||||
[class]{}-[func]{preOrder}
|
||||
|
||||
[class]{}-[func]{inOrder}
|
||||
|
||||
[class]{}-[func]{postOrder}
|
||||
```
|
||||
|
||||
!!! note
|
||||
|
||||
深度优先搜索也可以基于迭代实现,有兴趣的同学可以自行研究。
|
||||
|
||||
Reference in New Issue
Block a user