hello-algo/codes/swift/chapter_tree/binary_search_tree.swift

190 lines
5.4 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* File: binary_search_tree.swift
* Created Time: 2023-01-26
* Author: nuomi1 (nuomi1@qq.com)
*/
import utils
/* */
class BinarySearchTree {
private var root: TreeNode?
init(nums: [Int]) {
let nums = nums.sorted() //
root = buildTree(nums: nums, i: 0, j: nums.count - 1) //
}
/* */
func getRoot() -> TreeNode? {
root
}
/* */
func buildTree(nums: [Int], i: Int, j: Int) -> TreeNode? {
if i > j {
return nil
}
//
let mid = (i + j) / 2
let root = TreeNode(x: nums[mid])
//
root.left = buildTree(nums: nums, i: i, j: mid - 1)
root.right = buildTree(nums: nums, i: mid + 1, j: j)
return root
}
/* */
func search(num: Int) -> TreeNode? {
var cur = root
//
while cur != nil {
// cur
if cur!.val < num {
cur = cur?.right
}
// cur
else if cur!.val > num {
cur = cur?.left
}
//
else {
break
}
}
//
return cur
}
/* */
func insert(num: Int) -> TreeNode? {
//
if root == nil {
return nil
}
var cur = root
var pre: TreeNode?
//
while cur != nil {
//
if cur!.val == num {
return nil
}
pre = cur
// cur
if cur!.val < num {
cur = cur?.right
}
// cur
else {
cur = cur?.left
}
}
// val
let node = TreeNode(x: num)
if pre!.val < num {
pre?.right = node
} else {
pre?.left = node
}
return node
}
/* */
@discardableResult
func remove(num: Int) -> TreeNode? {
//
if root == nil {
return nil
}
var cur = root
var pre: TreeNode?
//
while cur != nil {
//
if cur!.val == num {
break
}
pre = cur
// cur
if cur!.val < num {
cur = cur?.right
}
// cur
else {
cur = cur?.left
}
}
//
if cur == nil {
return nil
}
// = 0 or 1
if cur?.left == nil || cur?.right == nil {
// = 0 / 1 child = null /
let child = cur?.left != nil ? cur?.left : cur?.right
// cur
if pre?.left === cur {
pre?.left = child
} else {
pre?.right = child
}
}
// = 2
else {
// cur
let nex = getInOrderNext(root: cur?.right)
let tmp = nex!.val
// nex
remove(num: nex!.val)
// nex cur
cur?.val = tmp
}
return cur
}
/* root */
func getInOrderNext(root: TreeNode?) -> TreeNode? {
var root = root
if root == nil {
return root
}
// 访
while root?.left != nil {
root = root?.left
}
return root
}
}
@main
enum _BinarySearchTree {
/* Driver Code */
static func main() {
/* */
let nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
let bst = BinarySearchTree(nums: nums)
print("\n初始化的二叉树为\n")
PrintUtil.printTree(root: bst.getRoot())
/* */
var node = bst.search(num: 7)
print("\n查找到的结点对象为 \(node!),结点值 = \(node!.val)")
/* */
node = bst.insert(num: 16)
print("\n插入结点 16 后,二叉树为\n")
PrintUtil.printTree(root: bst.getRoot())
/* */
bst.remove(num: 1)
print("\n删除结点 1 后,二叉树为\n")
PrintUtil.printTree(root: bst.getRoot())
bst.remove(num: 2)
print("\n删除结点 2 后,二叉树为\n")
PrintUtil.printTree(root: bst.getRoot())
bst.remove(num: 4)
print("\n删除结点 4 后,二叉树为\n")
PrintUtil.printTree(root: bst.getRoot())
}
}