Add build script for Swift.

This commit is contained in:
krahets
2023-02-08 20:30:05 +08:00
parent 05f0054005
commit 38751cc5f5
25 changed files with 128 additions and 1223 deletions

View File

@ -362,13 +362,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
=== "Swift"
```swift title="avl_tree.swift"
/* 获取平衡因子 */
func balanceFactor(node: TreeNode?) -> Int {
// 空结点平衡因子为 0
guard let node = node else { return 0 }
// 结点平衡因子 = 左子树高度 - 右子树高度
return height(node: node.left) - height(node: node.right)
}
[class]{AVLTree}-[func]{balanceFactor}
```
=== "Zig"
@ -485,19 +479,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
=== "Swift"
```swift title="avl_tree.swift"
/* 右旋操作 */
func rightRotate(node: TreeNode?) -> TreeNode? {
let child = node?.left
let grandChild = child?.right
// 以 child 为原点,将 node 向右旋转
child?.right = node
node?.left = grandChild
// 更新结点高度
updateHeight(node: node)
updateHeight(node: child)
// 返回旋转后子树的根结点
return child
}
[class]{AVLTree}-[func]{rightRotate}
```
=== "Zig"
@ -596,20 +578,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
=== "Swift"
```swift title="avl_tree.swift"
/* 左旋操作 */
func leftRotate(node: TreeNode?) -> TreeNode? {
let child = node?.right
let grandChild = child?.left
// 以 child 为原点,将 node 向左旋转
child?.left = node
node?.right = grandChild
// 更新结点高度
updateHeight(node: node)
updateHeight(node: child)
// 返回旋转后子树的根结点
return child
}
[class]{AVLTree}-[func]{leftRotate}
```
=== "Zig"
@ -768,35 +737,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
=== "Swift"
```swift title="avl_tree.swift"
/* 执行旋转操作,使该子树重新恢复平衡 */
func rotate(node: TreeNode?) -> TreeNode? {
// 获取结点 node 的平衡因子
let balanceFactor = balanceFactor(node: node)
// 左偏树
if balanceFactor > 1 {
if self.balanceFactor(node: node?.left) >= 0 {
// 右旋
return rightRotate(node: node)
} else {
// 先左旋后右旋
node?.left = leftRotate(node: node?.left)
return rightRotate(node: node)
}
}
// 右偏树
if balanceFactor < -1 {
if self.balanceFactor(node: node?.right) <= 0 {
// 左旋
return leftRotate(node: node)
} else {
// 先右旋后左旋
node?.right = rightRotate(node: node?.right)
return leftRotate(node: node)
}
}
// 平衡树,无需旋转,直接返回
return node
}
[class]{AVLTree}-[func]{rotate}
```
=== "Zig"

View File

@ -120,27 +120,7 @@ comments: true
=== "Swift"
```swift title="binary_search_tree.swift"
/* 查找结点 */
func search(num: Int) -> TreeNode? {
var cur = root
// 循环查找,越过叶结点后跳出
while cur != nil {
// 目标结点在 cur 的右子树中
if cur!.val < num {
cur = cur?.right
}
// 目标结点在 cur 的左子树中
else if cur!.val > num {
cur = cur?.left
}
// 找到目标结点,跳出循环
else {
break
}
}
// 返回目标结点
return cur
}
[class]{BinarySearchTree}-[func]{search}
```
=== "Zig"
@ -266,39 +246,7 @@ comments: true
=== "Swift"
```swift title="binary_search_tree.swift"
/* 插入结点 */
func insert(num: Int) -> TreeNode? {
// 若树为空,直接提前返回
if root == nil {
return nil
}
var cur = root
var pre: TreeNode?
// 循环查找,越过叶结点后跳出
while cur != nil {
// 找到重复结点,直接返回
if cur!.val == num {
return nil
}
pre = cur
// 插入位置在 cur 的右子树中
if cur!.val < num {
cur = cur?.right
}
// 插入位置在 cur 的左子树中
else {
cur = cur?.left
}
}
// 插入结点 val
let node = TreeNode(x: num)
if pre!.val < num {
pre?.right = node
} else {
pre?.left = node
}
return node
}
[class]{BinarySearchTree}-[func]{insert}
```
=== "Zig"
@ -530,71 +478,9 @@ comments: true
=== "Swift"
```swift title="binary_search_tree.swift"
/* 删除结点 */
@discardableResult
func remove(num: Int) -> TreeNode? {
// 若树为空,直接提前返回
if root == nil {
return nil
}
var cur = root
var pre: TreeNode?
// 循环查找,越过叶结点后跳出
while cur != nil {
// 找到待删除结点,跳出循环
if cur!.val == num {
break
}
pre = cur
// 待删除结点在 cur 的右子树中
if cur!.val < num {
cur = cur?.right
}
// 待删除结点在 cur 的左子树中
else {
cur = cur?.left
}
}
// 若无待删除结点,则直接返回
if cur == nil {
return nil
}
// 子结点数量 = 0 or 1
if cur?.left == nil || cur?.right == nil {
// 当子结点数量 = 0 / 1 时, child = null / 该子结点
let child = cur?.left != nil ? cur?.left : cur?.right
// 删除结点 cur
if pre?.left === cur {
pre?.left = child
} else {
pre?.right = child
}
}
// 子结点数量 = 2
else {
// 获取中序遍历中 cur 的下一个结点
let nex = getInOrderNext(root: cur?.right)
let tmp = nex!.val
// 递归删除结点 nex
remove(num: nex!.val)
// 将 nex 的值复制给 cur
cur?.val = tmp
}
return cur
}
[class]{BinarySearchTree}-[func]{remove}
/* 获取中序遍历中的下一个结点(仅适用于 root 有左子结点的情况) */
func getInOrderNext(root: TreeNode?) -> TreeNode? {
var root = root
if root == nil {
return root
}
// 循环访问左子结点,直到叶结点时为最小结点,跳出
while root?.left != nil {
root = root?.left
}
return root
}
[class]{BinarySearchTree}-[func]{getInOrderNext}
```
=== "Zig"

View File

@ -110,24 +110,7 @@ comments: true
=== "Swift"
```swift title="binary_tree_bfs.swift"
/* 层序遍历 */
func hierOrder(root: TreeNode) -> [Int] {
// 初始化队列,加入根结点
var queue: [TreeNode] = [root]
// 初始化一个列表,用于保存遍历序列
var list: [Int] = []
while !queue.isEmpty {
let node = queue.removeFirst() // 队列出队
list.append(node.val) // 保存结点值
if let left = node.left {
queue.append(left) // 左子结点入队
}
if let right = node.right {
queue.append(right) // 右子结点入队
}
}
return list
}
[class]{}-[func]{hierOrder}
```
=== "Zig"
@ -286,38 +269,11 @@ comments: true
=== "Swift"
```swift title="binary_tree_dfs.swift"
/* 前序遍历 */
func preOrder(root: TreeNode?) {
guard let root = root else {
return
}
// 访问优先级:根结点 -> 左子树 -> 右子树
list.append(root.val)
preOrder(root: root.left)
preOrder(root: root.right)
}
[class]{}-[func]{preOrder}
/* 中序遍历 */
func inOrder(root: TreeNode?) {
guard let root = root else {
return
}
// 访问优先级:左子树 -> 根结点 -> 右子树
inOrder(root: root.left)
list.append(root.val)
inOrder(root: root.right)
}
[class]{}-[func]{inOrder}
/* 后序遍历 */
func postOrder(root: TreeNode?) {
guard let root = root else {
return
}
// 访问优先级:左子树 -> 右子树 -> 根结点
postOrder(root: root.left)
postOrder(root: root.right)
list.append(root.val)
}
[class]{}-[func]{postOrder}
```
=== "Zig"