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

@@ -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)
回顾「完全二叉树」的定义,其只有最底层有空结点,并且最底层的结点尽量靠左,因而所有空结点都一定出现在层序遍历序列的末尾。**因为我们先验地确定了空位的位置,所以在使用数组表示完全二叉树时,可以省略存储“空位”**。因此,完全二叉树非常适合使用数组来表示。