Merge pull request #279 from nuomi1/feature/binary_tree_traversal-Swift

feat: add Swift codes for binary_tree_traversal article
pull/280/head
Yudong Jin 2 years ago committed by GitHub
commit ca970f4b34
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -23,6 +23,8 @@ let package = Package(
.executable(name: "hash_map", targets: ["hash_map"]), .executable(name: "hash_map", targets: ["hash_map"]),
.executable(name: "array_hash_map", targets: ["array_hash_map"]), .executable(name: "array_hash_map", targets: ["array_hash_map"]),
.executable(name: "binary_tree", targets: ["binary_tree"]), .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: [ targets: [
.target(name: "utils", path: "utils"), .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: "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: "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", 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"]),
] ]
) )

@ -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)")
}
}

@ -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)")
}
}

@ -14,4 +14,30 @@ public class TreeNode {
val = x val = x
height = 0 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
}
} }

@ -188,7 +188,24 @@ comments: true
=== "Swift" === "Swift"
```swift title="binary_tree_bfs.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"
```swift title="binary_tree_dfs.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 !!! note

Loading…
Cancel
Save