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
+18
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$ 个数字,最常用的存储方式是「数组」,那么对于这串乱序的数字,常见操作的效率为: