diff --git a/codes/swift/Package.swift b/codes/swift/Package.swift index 3c2229e32..8622baf3f 100644 --- a/codes/swift/Package.swift +++ b/codes/swift/Package.swift @@ -23,6 +23,8 @@ let package = Package( .executable(name: "hash_map", targets: ["hash_map"]), .executable(name: "array_hash_map", targets: ["array_hash_map"]), .executable(name: "binary_tree", targets: ["binary_tree"]), + .executable(name: "binary_tree_bfs", targets: ["binary_tree_bfs"]), + .executable(name: "binary_tree_dfs", targets: ["binary_tree_dfs"]), ], targets: [ .target(name: "utils", path: "utils"), @@ -44,5 +46,7 @@ let package = Package( .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"]), + .executableTarget(name: "binary_tree_bfs", dependencies: ["utils"], path: "chapter_tree", sources: ["binary_tree_bfs.swift"]), + .executableTarget(name: "binary_tree_dfs", dependencies: ["utils"], path: "chapter_tree", sources: ["binary_tree_dfs.swift"]), ] ) diff --git a/codes/swift/chapter_tree/binary_tree_bfs.swift b/codes/swift/chapter_tree/binary_tree_bfs.swift new file mode 100644 index 000000000..bdd09559e --- /dev/null +++ b/codes/swift/chapter_tree/binary_tree_bfs.swift @@ -0,0 +1,42 @@ +/** + * File: binary_tree_bfs.swift + * Created Time: 2023-01-18 + * Author: nuomi1 (nuomi1@qq.com) + */ + +import utils + +/* 层序遍历 */ +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 +} + +@main +enum BinaryTreeBFS { + /* Driver Code */ + static func main() { + /* 初始化二叉树 */ + // 这里借助了一个从数组直接生成二叉树的函数 + let node = TreeNode.listToTree(list: [1, 2, 3, 4, 5, 6, 7])! + print("\n初始化二叉树\n") + PrintUtil.printTree(root: node) + + /* 层序遍历 */ + let list = hierOrder(root: node) + print("\n层序遍历的结点打印序列 = \(list)") + } +} diff --git a/codes/swift/chapter_tree/binary_tree_dfs.swift b/codes/swift/chapter_tree/binary_tree_dfs.swift new file mode 100644 index 000000000..d68d764c2 --- /dev/null +++ b/codes/swift/chapter_tree/binary_tree_dfs.swift @@ -0,0 +1,70 @@ +/** + * File: binary_tree_dfs.swift + * Created Time: 2023-01-18 + * Author: nuomi1 (nuomi1@qq.com) + */ + +import utils + +// 初始化列表,用于存储遍历序列 +var list: [Int] = [] + +/* 前序遍历 */ +func preOrder(root: TreeNode?) { + guard let root = root else { + return + } + // 访问优先级:根结点 -> 左子树 -> 右子树 + list.append(root.val) + preOrder(root: root.left) + preOrder(root: root.right) +} + +/* 中序遍历 */ +func inOrder(root: TreeNode?) { + guard let root = root else { + return + } + // 访问优先级:左子树 -> 根结点 -> 右子树 + inOrder(root: root.left) + list.append(root.val) + inOrder(root: root.right) +} + +/* 后序遍历 */ +func postOrder(root: TreeNode?) { + guard let root = root else { + return + } + // 访问优先级:左子树 -> 右子树 -> 根结点 + postOrder(root: root.left) + postOrder(root: root.right) + list.append(root.val) +} + +@main +enum BinaryTreeDFS { + /* Driver Code */ + static func main() { + /* 初始化二叉树 */ + // 这里借助了一个从数组直接生成二叉树的函数 + let root = TreeNode.listToTree(list: [1, 2, 3, 4, 5, 6, 7])! + print("\n初始化二叉树\n") + PrintUtil.printTree(root: root) + + /* 前序遍历 */ + list.removeAll() + preOrder(root: root) + print("\n前序遍历的结点打印序列 = \(list)") + + /* 中序遍历 */ + list.removeAll() + inOrder(root: root) + print("\n中序遍历的结点打印序列 = \(list)") + + /* 后序遍历 */ + list.removeAll() + postOrder(root: root) + print("\n后序遍历的结点打印序列 = \(list)") + } +} diff --git a/codes/swift/utils/TreeNode.swift b/codes/swift/utils/TreeNode.swift index 161cebd57..bb5abf55d 100644 --- a/codes/swift/utils/TreeNode.swift +++ b/codes/swift/utils/TreeNode.swift @@ -14,4 +14,30 @@ public class TreeNode { val = x height = 0 } + + public static func listToTree(list: [Int]) -> TreeNode? { + let size = list.count + if size == 0 { + return nil + } + let root = TreeNode(x: list[0]) + var queue: [TreeNode] = [root] + var i = 0 + while !queue.isEmpty { + let node = queue.removeFirst() + i += 1 + if i >= size { + break + } + node.left = TreeNode(x: list[i]) + queue.append(node.left!) + i += 1 + if i >= size { + break + } + node.right = TreeNode(x: list[i]) + queue.append(node.right!) + } + return root + } } diff --git a/docs/chapter_tree/binary_tree_traversal.md b/docs/chapter_tree/binary_tree_traversal.md index 650a15297..3070097a1 100644 --- a/docs/chapter_tree/binary_tree_traversal.md +++ b/docs/chapter_tree/binary_tree_traversal.md @@ -188,7 +188,24 @@ 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 + } ``` ## 前序、中序、后序遍历 @@ -452,7 +469,38 @@ 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) + } + + /* 中序遍历 */ + func inOrder(root: TreeNode?) { + guard let root = root else { + return + } + // 访问优先级:左子树 -> 根结点 -> 右子树 + inOrder(root: root.left) + list.append(root.val) + inOrder(root: root.right) + } + /* 后序遍历 */ + func postOrder(root: TreeNode?) { + guard let root = root else { + return + } + // 访问优先级:左子树 -> 右子树 -> 根结点 + postOrder(root: root.left) + postOrder(root: root.right) + list.append(root.val) + } ``` !!! note