Add Swift language blocks to the docs.

This commit is contained in:
Yudong Jin
2023-01-08 19:41:05 +08:00
parent 3ba37dba3a
commit 73e3452838
22 changed files with 414 additions and 70 deletions

View File

@@ -94,6 +94,12 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
}
```
=== "Swift"
```swift title="avl_tree.swift"
```
「结点高度」是最远叶结点到该结点的距离,即走过的「边」的数量。需要特别注意,**叶结点的高度为 0 ,空结点的高度为 -1** 。我们封装两个工具函数,分别用于获取与更新结点的高度。
=== "Java"
@@ -176,6 +182,12 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
}
```
=== "Swift"
```swift title="avl_tree.swift"
```
### 结点平衡因子
结点的「平衡因子 Balance Factor」是 **结点的左子树高度减去右子树高度**,并定义空结点的平衡因子为 0 。同样地,我们将获取结点平衡因子封装成函数,以便后续使用。
@@ -247,6 +259,12 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
}
```
=== "Swift"
```swift title="avl_tree.swift"
```
!!! note
设平衡因子为 $f$ ,则一棵 AVL 树的任意结点的平衡因子皆满足 $-1 \le f \le 1$ 。
@@ -361,6 +379,12 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
```
=== "Swift"
```swift title="avl_tree.swift"
```
### Case 2 - 左旋
类似地,如果将取上述失衡二叉树的“镜像”,那么则需要「左旋」操作。
@@ -457,6 +481,12 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
}
```
=== "Swift"
```swift title="avl_tree.swift"
```
### Case 3 - 先左后右
对于下图的失衡结点 3 **单一使用左旋或右旋都无法使子树恢复平衡**,此时需要「先左旋后右旋」,即先对 `child` 执行「左旋」,再对 `node` 执行「右旋」。
@@ -626,6 +656,12 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
}
```
=== "Swift"
```swift title="avl_tree.swift"
```
## AVL 树常用操作
### 插入结点
@@ -744,6 +780,12 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
}
```
=== "Swift"
```swift title="avl_tree.swift"
```
### 删除结点
「AVL 树」删除结点操作与「二叉搜索树」删除结点操作总体相同。类似地,**在删除结点后,也需要从底至顶地执行旋转操作,使所有失衡结点恢复平衡**。
@@ -902,6 +944,12 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
}
```
=== "Swift"
```swift title="avl_tree.swift"
```
### 查找结点
「AVL 树」的结点查找操作与「二叉搜索树」一致,在此不再赘述。

View File

@@ -192,6 +192,12 @@ comments: true
}
```
=== "Swift"
```swift title="binary_search_tree.swift"
```
### 插入结点
给定一个待插入元素 `num` ,为了保持二叉搜索树“左子树 < 根结点 < 右子树”的性质,插入操作分为两步:
@@ -422,6 +428,12 @@ comments: true
}
```
=== "Swift"
```swift title="binary_search_tree.swift"
```
为了插入结点,需要借助 **辅助结点 `prev`** 保存上一轮循环的结点,这样在遍历到 $\text{null}$ 时,我们也可以获取到其父结点,从而完成结点插入操作。
与查找结点相同,插入结点使用 $O(\log n)$ 时间。
@@ -808,6 +820,12 @@ comments: true
}
```
=== "Swift"
```swift title="binary_search_tree.swift"
```
## 二叉搜索树的优势
假设给定 $n$ 个数字,最常用的存储方式是「数组」,那么对于这串乱序的数字,常见操作的效率为:

View File

@@ -106,6 +106,12 @@ comments: true
}
```
=== "Swift"
```swift title=""
```
结点的两个指针分别指向「左子结点 Left Child Node」和「右子结点 Right Child Node」并且称该结点为两个子结点的「父结点 Parent Node」。给定二叉树某结点将左子结点以下的树称为该结点的「左子树 Left Subtree」右子树同理。
除了叶结点外,每个结点都有子结点和子树。例如,若将上图的「结点 2」看作父结点那么其左子结点和右子结点分别为「结点 4」和「结点 5」左子树和右子树分别为「结点 4 以下的树」和「结点 5 以下的树」。
@@ -263,6 +269,12 @@ comments: true
n2.right = n5;
```
=== "Swift"
```swift title="binary_tree.swift"
```
**插入与删除结点**。与链表类似,插入与删除结点都可以通过修改指针实现。
![binary_tree_add_remove](binary_tree.assets/binary_tree_add_remove.png)
@@ -358,6 +370,12 @@ comments: true
n1.left = n2;
```
=== "Swift"
```swift title="binary_tree.swift"
```
!!! note
插入结点会改变二叉树的原有逻辑结构,删除结点往往意味着删除了该结点的所有子树。因此,二叉树中的插入与删除一般都是由一套操作配合完成的,这样才能实现有意义的操作。
@@ -495,6 +513,12 @@ comments: true
int?[] tree = { 1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15 };
```
=== "Swift"
```swift title=""
```
![array_representation_with_empty](binary_tree.assets/array_representation_with_empty.png)
回顾「完全二叉树」的定义,其只有最底层有空结点,并且最底层的结点尽量靠左,因而所有空结点都一定出现在层序遍历序列的末尾。**因为我们先验地确定了空位的位置,所以在使用数组表示完全二叉树时,可以省略存储“空位”**。因此,完全二叉树非常适合使用数组来表示。

View File

@@ -185,6 +185,12 @@ comments: true
```
=== "Swift"
```swift title="binary_tree_bfs.swift"
```
## 前序、中序、后序遍历
相对地,前、中、后序遍历皆属于「深度优先遍历 Depth-First Traversal」其体现着一种“先走到尽头再回头继续”的回溯遍历方式。
@@ -443,6 +449,12 @@ comments: true
}
```
=== "Swift"
```swift title="binary_tree_dfs.swift"
```
!!! note
使用循环一样可以实现前、中、后序遍历,但代码相对繁琐,有兴趣的同学可以自行实现。