hello-algo/zh-hant/codes/swift/chapter_tree/avl_tree.swift

231 lines
7.3 KiB
Swift
Raw Normal View History

feat: Traditional Chinese version (#1163) * First commit * Update mkdocs.yml * Translate all the docs to traditional Chinese * Translate the code files. * Translate the docker file * Fix mkdocs.yml * Translate all the figures from SC to TC * 二叉搜尋樹 -> 二元搜尋樹 * Update terminology. * Update terminology * 构造函数/构造方法 -> 建構子 异或 -> 互斥或 * 擴充套件 -> 擴展 * constant - 常量 - 常數 * 類 -> 類別 * AVL -> AVL 樹 * 數組 -> 陣列 * 係統 -> 系統 斐波那契數列 -> 費波那契數列 運算元量 -> 運算量 引數 -> 參數 * 聯絡 -> 關聯 * 麵試 -> 面試 * 面向物件 -> 物件導向 歸併排序 -> 合併排序 范式 -> 範式 * Fix 算法 -> 演算法 * 錶示 -> 表示 反碼 -> 一補數 補碼 -> 二補數 列列尾部 -> 佇列尾部 區域性性 -> 區域性 一摞 -> 一疊 * Synchronize with main branch * 賬號 -> 帳號 推匯 -> 推導 * Sync with main branch * First commit * Update mkdocs.yml * Translate all the docs to traditional Chinese * Translate the code files. * Translate the docker file * Fix mkdocs.yml * Translate all the figures from SC to TC * 二叉搜尋樹 -> 二元搜尋樹 * Update terminology * 构造函数/构造方法 -> 建構子 异或 -> 互斥或 * 擴充套件 -> 擴展 * constant - 常量 - 常數 * 類 -> 類別 * AVL -> AVL 樹 * 數組 -> 陣列 * 係統 -> 系統 斐波那契數列 -> 費波那契數列 運算元量 -> 運算量 引數 -> 參數 * 聯絡 -> 關聯 * 麵試 -> 面試 * 面向物件 -> 物件導向 歸併排序 -> 合併排序 范式 -> 範式 * Fix 算法 -> 演算法 * 錶示 -> 表示 反碼 -> 一補數 補碼 -> 二補數 列列尾部 -> 佇列尾部 區域性性 -> 區域性 一摞 -> 一疊 * Synchronize with main branch * 賬號 -> 帳號 推匯 -> 推導 * Sync with main branch * Update terminology.md * 操作数量(num. of operations)-> 操作數量 * 字首和->前綴和 * Update figures * 歸 -> 迴 記憶體洩漏 -> 記憶體流失 * Fix the bug of the file filter * 支援 -> 支持 Add zh-Hant/README.md * Add the zh-Hant chapter covers. Bug fixes. * 外掛 -> 擴充功能 * Add the landing page for zh-Hant version * Unify the font of the chapter covers for the zh, en, and zh-Hant version * Move zh-Hant/ to zh-hant/ * Translate terminology.md to traditional Chinese
2024-04-06 02:30:11 +08:00
/**
* File: avl_tree.swift
* Created Time: 2023-01-28
* Author: nuomi1 (nuomi1@qq.com)
*/
import utils
/* AVL */
class AVLTree {
fileprivate var root: TreeNode? //
init() {}
/* */
func height(node: TreeNode?) -> Int {
// -1 0
node?.height ?? -1
}
/* */
private func updateHeight(node: TreeNode?) {
// + 1
node?.height = max(height(node: node?.left), height(node: node?.right)) + 1
}
/* */
func balanceFactor(node: TreeNode?) -> Int {
// 0
guard let node = node else { return 0 }
// = -
return height(node: node.left) - height(node: node.right)
}
/* */
private func rightRotate(node: TreeNode?) -> TreeNode? {
let child = node?.left
let grandChild = child?.right
// child node
child?.right = node
node?.left = grandChild
//
updateHeight(node: node)
updateHeight(node: child)
//
return child
}
/* */
private func leftRotate(node: TreeNode?) -> TreeNode? {
let child = node?.right
let grandChild = child?.left
// child node
child?.left = node
node?.right = grandChild
//
updateHeight(node: node)
updateHeight(node: child)
//
return child
}
/* 使 */
private func rotate(node: TreeNode?) -> TreeNode? {
// node
let balanceFactor = balanceFactor(node: node)
//
if balanceFactor > 1 {
if self.balanceFactor(node: node?.left) >= 0 {
//
return rightRotate(node: node)
} else {
//
node?.left = leftRotate(node: node?.left)
return rightRotate(node: node)
}
}
//
if balanceFactor < -1 {
if self.balanceFactor(node: node?.right) <= 0 {
//
return leftRotate(node: node)
} else {
//
node?.right = rightRotate(node: node?.right)
return leftRotate(node: node)
}
}
//
return node
}
/* */
func insert(val: Int) {
root = insertHelper(node: root, val: val)
}
/* */
private func insertHelper(node: TreeNode?, val: Int) -> TreeNode? {
var node = node
if node == nil {
return TreeNode(x: val)
}
/* 1. */
if val < node!.val {
node?.left = insertHelper(node: node?.left, val: val)
} else if val > node!.val {
node?.right = insertHelper(node: node?.right, val: val)
} else {
return node //
}
updateHeight(node: node) //
/* 2. 使 */
node = rotate(node: node)
//
return node
}
/* */
func remove(val: Int) {
root = removeHelper(node: root, val: val)
}
/* */
private func removeHelper(node: TreeNode?, val: Int) -> TreeNode? {
var node = node
if node == nil {
return nil
}
/* 1. */
if val < node!.val {
node?.left = removeHelper(node: node?.left, val: val)
} else if val > node!.val {
node?.right = removeHelper(node: node?.right, val: val)
} else {
if node?.left == nil || node?.right == nil {
let child = node?.left ?? node?.right
// = 0 node
if child == nil {
return nil
}
// = 1 node
else {
node = child
}
} else {
// = 2
var temp = node?.right
while temp?.left != nil {
temp = temp?.left
}
node?.right = removeHelper(node: node?.right, val: temp!.val)
node?.val = temp!.val
}
}
updateHeight(node: node) //
/* 2. 使 */
node = rotate(node: node)
//
return node
}
/* */
func search(val: Int) -> TreeNode? {
var cur = root
while cur != nil {
// cur
if cur!.val < val {
cur = cur?.right
}
// cur
else if cur!.val > val {
cur = cur?.left
}
//
else {
break
}
}
//
return cur
}
}
@main
enum _AVLTree {
static func testInsert(tree: AVLTree, val: Int) {
tree.insert(val: val)
print("\n插入節點 \(val)AVL 樹為")
PrintUtil.printTree(root: tree.root)
}
static func testRemove(tree: AVLTree, val: Int) {
tree.remove(val: val)
print("\n刪除節點 \(val)AVL 樹為")
PrintUtil.printTree(root: tree.root)
}
/* Driver Code */
static func main() {
/* AVL */
let avlTree = AVLTree()
/* */
// AVL
testInsert(tree: avlTree, val: 1)
testInsert(tree: avlTree, val: 2)
testInsert(tree: avlTree, val: 3)
testInsert(tree: avlTree, val: 4)
testInsert(tree: avlTree, val: 5)
testInsert(tree: avlTree, val: 8)
testInsert(tree: avlTree, val: 7)
testInsert(tree: avlTree, val: 9)
testInsert(tree: avlTree, val: 10)
testInsert(tree: avlTree, val: 6)
/* */
testInsert(tree: avlTree, val: 7)
/* */
// AVL
testRemove(tree: avlTree, val: 8) // 0
testRemove(tree: avlTree, val: 5) // 1
testRemove(tree: avlTree, val: 4) // 2
/* */
let node = avlTree.search(val: 7)
print("\n查詢到的節點物件為 \(node!),節點值 = \(node!.val)")
}
}