mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-25 23:46:29 +08:00
feat: add Swift codes for binary_tree_traversal article
This commit is contained in:
parent
46429bcb23
commit
d52b60804b
5 changed files with 191 additions and 1 deletions
|
@ -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"]),
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
42
codes/swift/chapter_tree/binary_tree_bfs.swift
Normal file
42
codes/swift/chapter_tree/binary_tree_bfs.swift
Normal file
|
@ -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)")
|
||||||
|
}
|
||||||
|
}
|
70
codes/swift/chapter_tree/binary_tree_dfs.swift
Normal file
70
codes/swift/chapter_tree/binary_tree_dfs.swift
Normal file
|
@ -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…
Reference in a new issue