feat: add Swift codes for binary_tree article

This commit is contained in:
nuomi1
2023-01-18 21:34:44 +08:00
parent 0bef99d438
commit 3ba87bcd7b
3 changed files with 71 additions and 3 deletions

View File

@ -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"]),
]
)

View File

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