From 3ba87bcd7b97aec59b70723939ec8843c4715a58 Mon Sep 17 00:00:00 2001 From: nuomi1 Date: Wed, 18 Jan 2023 21:34:44 +0800 Subject: [PATCH] feat: add Swift codes for binary_tree article --- codes/swift/Package.swift | 2 ++ codes/swift/chapter_tree/binary_tree.swift | 40 ++++++++++++++++++++++ docs/chapter_tree/binary_tree.md | 32 +++++++++++++++-- 3 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 codes/swift/chapter_tree/binary_tree.swift diff --git a/codes/swift/Package.swift b/codes/swift/Package.swift index 3652ac16f..3c2229e32 100644 --- a/codes/swift/Package.swift +++ b/codes/swift/Package.swift @@ -22,6 +22,7 @@ let package = Package( .executable(name: "deque", targets: ["deque"]), .executable(name: "hash_map", targets: ["hash_map"]), .executable(name: "array_hash_map", targets: ["array_hash_map"]), + .executable(name: "binary_tree", targets: ["binary_tree"]), ], targets: [ .target(name: "utils", path: "utils"), @@ -42,5 +43,6 @@ let package = Package( .executableTarget(name: "deque", path: "chapter_stack_and_queue", sources: ["deque.swift"]), .executableTarget(name: "hash_map", dependencies: ["utils"], path: "chapter_hashing", sources: ["hash_map.swift"]), .executableTarget(name: "array_hash_map", path: "chapter_hashing", sources: ["array_hash_map.swift"]), + .executableTarget(name: "binary_tree", dependencies: ["utils"], path: "chapter_tree", sources: ["binary_tree.swift"]), ] ) diff --git a/codes/swift/chapter_tree/binary_tree.swift b/codes/swift/chapter_tree/binary_tree.swift new file mode 100644 index 000000000..ad9e3c744 --- /dev/null +++ b/codes/swift/chapter_tree/binary_tree.swift @@ -0,0 +1,40 @@ +/** + * File: binary_tree.swift + * Created Time: 2023-01-18 + * Author: nuomi1 (nuomi1@qq.com) + */ + +import utils + +@main +enum BinaryTree { + /* Driver Code */ + static func main() { + /* 初始化二叉树 */ + // 初始化结点 + 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 + print("\n初始化二叉树\n") + PrintUtil.printTree(root: n1) + + /* 插入与删除结点 */ + let P = TreeNode(x: 0) + // 在 n1 -> n2 中间插入结点 P + n1.left = P + P.left = n2 + print("\n插入结点 P 后\n") + PrintUtil.printTree(root: n1) + // 删除结点 P + n1.left = n2 + print("\n删除结点 P 后\n") + PrintUtil.printTree(root: n1) + } +} diff --git a/docs/chapter_tree/binary_tree.md b/docs/chapter_tree/binary_tree.md index 0201f6a2a..8b51788d2 100644 --- a/docs/chapter_tree/binary_tree.md +++ b/docs/chapter_tree/binary_tree.md @@ -109,7 +109,16 @@ comments: true === "Swift" ```swift title="" + /* 链表结点类 */ + class TreeNode { + var val: Int // 结点值 + var left: TreeNode? // 左子结点指针 + var right: TreeNode? // 右子结点指针 + init(x: Int) { + val = x + } + } ``` 结点的两个指针分别指向「左子结点 Left Child Node」和「右子结点 Right Child Node」,并且称该结点为两个子结点的「父结点 Parent Node」。给定二叉树某结点,将左子结点以下的树称为该结点的「左子树 Left Subtree」,右子树同理。 @@ -272,7 +281,17 @@ comments: true === "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 ``` **插入与删除结点**。与链表类似,插入与删除结点都可以通过修改指针实现。 @@ -373,7 +392,12 @@ comments: true === "Swift" ```swift title="binary_tree.swift" - + let P = TreeNode(x: 0) + // 在 n1 -> n2 中间插入结点 P + n1.left = P + P.left = n2 + // 删除结点 P + n1.left = n2 ``` !!! note @@ -516,7 +540,9 @@ comments: true === "Swift" ```swift title="" - + /* 二叉树的数组表示 */ + // 使用 Int? 可空类型 ,就可以使用 nil 来标记空位 + let tree: [Int?] = [1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15] ``` ![array_representation_with_empty](binary_tree.assets/array_representation_with_empty.png)