mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-14 12:40:58 +00:00
Many bug fixes and improvements (#1270)
* Add Ruby and Kotlin icons Add the avatar of @curtishd * Update README * Synchronize zh-hant and zh versions. * Translate the pythontutor blocks to traditional Chinese * Fix en/mkdocs.yml * Update the landing page of the en version. * Fix the Dockerfile * Refine the en landingpage * Fix en landing page * Reset the README.md
This commit is contained in:
@@ -10,14 +10,15 @@ import utils.TreeNode
|
||||
import utils.printTree
|
||||
|
||||
/* 陣列表示下的二元樹類別 */
|
||||
class ArrayBinaryTree(private val tree: List<Int?>) {
|
||||
/* 建構子 */
|
||||
class ArrayBinaryTree(private val tree: MutableList<Int?>) {
|
||||
/* 串列容量 */
|
||||
fun size(): Int {
|
||||
return tree.size
|
||||
}
|
||||
|
||||
/* 獲取索引為 i 節點的值 */
|
||||
fun value(i: Int): Int? {
|
||||
fun _val(i: Int): Int? {
|
||||
// 若索引越界,則返回 null ,代表空位
|
||||
if (i < 0 || i >= size()) return null
|
||||
return tree[i]
|
||||
@@ -39,11 +40,12 @@ class ArrayBinaryTree(private val tree: List<Int?>) {
|
||||
}
|
||||
|
||||
/* 層序走訪 */
|
||||
fun levelOrder(): List<Int?> {
|
||||
val res = ArrayList<Int?>()
|
||||
fun levelOrder(): MutableList<Int?> {
|
||||
val res = mutableListOf<Int?>()
|
||||
// 直接走訪陣列
|
||||
for (i in 0..<size()) {
|
||||
if (value(i) != null) res.add(value(i))
|
||||
if (_val(i) != null)
|
||||
res.add(_val(i))
|
||||
}
|
||||
return res
|
||||
}
|
||||
@@ -51,34 +53,38 @@ class ArrayBinaryTree(private val tree: List<Int?>) {
|
||||
/* 深度優先走訪 */
|
||||
fun dfs(i: Int, order: String, res: MutableList<Int?>) {
|
||||
// 若為空位,則返回
|
||||
if (value(i) == null) return
|
||||
if (_val(i) == null)
|
||||
return
|
||||
// 前序走訪
|
||||
if ("pre" == order) res.add(value(i))
|
||||
if ("pre" == order)
|
||||
res.add(_val(i))
|
||||
dfs(left(i), order, res)
|
||||
// 中序走訪
|
||||
if ("in" == order) res.add(value(i))
|
||||
if ("in" == order)
|
||||
res.add(_val(i))
|
||||
dfs(right(i), order, res)
|
||||
// 後序走訪
|
||||
if ("post" == order) res.add(value(i))
|
||||
if ("post" == order)
|
||||
res.add(_val(i))
|
||||
}
|
||||
|
||||
/* 前序走訪 */
|
||||
fun preOrder(): List<Int?> {
|
||||
val res = ArrayList<Int?>()
|
||||
fun preOrder(): MutableList<Int?> {
|
||||
val res = mutableListOf<Int?>()
|
||||
dfs(0, "pre", res)
|
||||
return res
|
||||
}
|
||||
|
||||
/* 中序走訪 */
|
||||
fun inOrder(): List<Int?> {
|
||||
val res = ArrayList<Int?>()
|
||||
fun inOrder(): MutableList<Int?> {
|
||||
val res = mutableListOf<Int?>()
|
||||
dfs(0, "in", res)
|
||||
return res
|
||||
}
|
||||
|
||||
/* 後序走訪 */
|
||||
fun postOrder(): List<Int?> {
|
||||
val res = ArrayList<Int?>()
|
||||
fun postOrder(): MutableList<Int?> {
|
||||
val res = mutableListOf<Int?>()
|
||||
dfs(0, "post", res)
|
||||
return res
|
||||
}
|
||||
@@ -105,10 +111,10 @@ fun main() {
|
||||
val l = abt.left(i)
|
||||
val r = abt.right(i)
|
||||
val p = abt.parent(i)
|
||||
println("當前節點的索引為 $i ,值為 ${abt.value(i)}")
|
||||
println("其左子節點的索引為 $l ,值為 ${abt.value(l)}")
|
||||
println("其右子節點的索引為 $r ,值為 ${abt.value(r)}")
|
||||
println("其父節點的索引為 $p ,值為 ${abt.value(p)}")
|
||||
println("當前節點的索引為 $i ,值為 ${abt._val(i)}")
|
||||
println("其左子節點的索引為 $l ,值為 ${abt._val(l)}")
|
||||
println("其右子節點的索引為 $r ,值為 ${abt._val(r)}")
|
||||
println("其父節點的索引為 $p ,值為 ${abt._val(p)}")
|
||||
|
||||
// 走訪樹
|
||||
var res = abt.levelOrder()
|
||||
|
||||
@@ -23,7 +23,7 @@ class AVLTree {
|
||||
/* 更新節點高度 */
|
||||
private fun updateHeight(node: TreeNode?) {
|
||||
// 節點高度等於最高子樹高度 + 1
|
||||
node?.height = (max(height(node?.left).toDouble(), height(node?.right).toDouble()) + 1).toInt()
|
||||
node?.height = max(height(node?.left), height(node?.right)) + 1
|
||||
}
|
||||
|
||||
/* 獲取平衡因子 */
|
||||
@@ -93,20 +93,22 @@ class AVLTree {
|
||||
}
|
||||
|
||||
/* 插入節點 */
|
||||
fun insert(value: Int) {
|
||||
root = insertHelper(root, value)
|
||||
fun insert(_val: Int) {
|
||||
root = insertHelper(root, _val)
|
||||
}
|
||||
|
||||
/* 遞迴插入節點(輔助方法) */
|
||||
private fun insertHelper(n: TreeNode?, value: Int): TreeNode {
|
||||
private fun insertHelper(n: TreeNode?, _val: Int): TreeNode {
|
||||
if (n == null)
|
||||
return TreeNode(value)
|
||||
return TreeNode(_val)
|
||||
var node = n
|
||||
/* 1. 查詢插入位置並插入節點 */
|
||||
if (value < node.value) node.left = insertHelper(node.left, value)
|
||||
else if (value > node.value) node.right = insertHelper(node.right, value)
|
||||
else return node // 重複節點不插入,直接返回
|
||||
|
||||
if (_val < node._val)
|
||||
node.left = insertHelper(node.left, _val)
|
||||
else if (_val > node._val)
|
||||
node.right = insertHelper(node.right, _val)
|
||||
else
|
||||
return node // 重複節點不插入,直接返回
|
||||
updateHeight(node) // 更新節點高度
|
||||
/* 2. 執行旋轉操作,使該子樹重新恢復平衡 */
|
||||
node = rotate(node)
|
||||
@@ -115,30 +117,38 @@ class AVLTree {
|
||||
}
|
||||
|
||||
/* 刪除節點 */
|
||||
fun remove(value: Int) {
|
||||
root = removeHelper(root, value)
|
||||
fun remove(_val: Int) {
|
||||
root = removeHelper(root, _val)
|
||||
}
|
||||
|
||||
/* 遞迴刪除節點(輔助方法) */
|
||||
private fun removeHelper(n: TreeNode?, value: Int): TreeNode? {
|
||||
private fun removeHelper(n: TreeNode?, _val: Int): TreeNode? {
|
||||
var node = n ?: return null
|
||||
/* 1. 查詢節點並刪除 */
|
||||
if (value < node.value) node.left = removeHelper(node.left, value)
|
||||
else if (value > node.value) node.right = removeHelper(node.right, value)
|
||||
if (_val < node._val)
|
||||
node.left = removeHelper(node.left, _val)
|
||||
else if (_val > node._val)
|
||||
node.right = removeHelper(node.right, _val)
|
||||
else {
|
||||
if (node.left == null || node.right == null) {
|
||||
val child = if (node.left != null) node.left else node.right
|
||||
val child = if (node.left != null)
|
||||
node.left
|
||||
else
|
||||
node.right
|
||||
// 子節點數量 = 0 ,直接刪除 node 並返回
|
||||
if (child == null) return null
|
||||
else node = child
|
||||
if (child == null)
|
||||
return null
|
||||
// 子節點數量 = 1 ,直接刪除 node
|
||||
else
|
||||
node = child
|
||||
} else {
|
||||
// 子節點數量 = 2 ,則將中序走訪的下個節點刪除,並用該節點替換當前節點
|
||||
var temp = node.right
|
||||
while (temp!!.left != null) {
|
||||
temp = temp.left
|
||||
}
|
||||
node.right = removeHelper(node.right, temp.value)
|
||||
node.value = temp.value
|
||||
node.right = removeHelper(node.right, temp._val)
|
||||
node._val = temp._val
|
||||
}
|
||||
}
|
||||
updateHeight(node) // 更新節點高度
|
||||
@@ -149,29 +159,34 @@ class AVLTree {
|
||||
}
|
||||
|
||||
/* 查詢節點 */
|
||||
fun search(value: Int): TreeNode? {
|
||||
fun search(_val: Int): TreeNode? {
|
||||
var cur = root
|
||||
// 迴圈查詢,越過葉節點後跳出
|
||||
while (cur != null) {
|
||||
// 目標節點在 cur 的右子樹中
|
||||
cur = if (cur.value < value) cur.right!!
|
||||
else (if (cur.value > value) cur.left
|
||||
else break)!!
|
||||
cur = if (cur._val < _val)
|
||||
cur.right!!
|
||||
// 目標節點在 cur 的左子樹中
|
||||
else if (cur._val > _val)
|
||||
cur.left
|
||||
// 找到目標節點,跳出迴圈
|
||||
else
|
||||
break
|
||||
}
|
||||
// 返回目標節點
|
||||
return cur
|
||||
}
|
||||
}
|
||||
|
||||
fun testInsert(tree: AVLTree, value: Int) {
|
||||
tree.insert(value)
|
||||
println("\n插入節點 $value 後,AVL 樹為")
|
||||
fun testInsert(tree: AVLTree, _val: Int) {
|
||||
tree.insert(_val)
|
||||
println("\n插入節點 $_val 後,AVL 樹為")
|
||||
printTree(tree.root)
|
||||
}
|
||||
|
||||
fun testRemove(tree: AVLTree, value: Int) {
|
||||
tree.remove(value)
|
||||
println("\n刪除節點 $value 後,AVL 樹為")
|
||||
fun testRemove(tree: AVLTree, _val: Int) {
|
||||
tree.remove(_val)
|
||||
println("\n刪除節點 $_val 後,AVL 樹為")
|
||||
printTree(tree.root)
|
||||
}
|
||||
|
||||
@@ -204,5 +219,5 @@ fun main() {
|
||||
|
||||
/* 查詢節點 */
|
||||
val node = avlTree.search(7)
|
||||
println("\n 查詢到的節點物件為 $node,節點值 = ${node?.value}")
|
||||
println("\n 查詢到的節點物件為 $node,節點值 = ${node?._val}")
|
||||
}
|
||||
@@ -11,6 +11,7 @@ import utils.printTree
|
||||
|
||||
/* 二元搜尋樹 */
|
||||
class BinarySearchTree {
|
||||
// 初始化空樹
|
||||
private var root: TreeNode? = null
|
||||
|
||||
/* 獲取二元樹根節點 */
|
||||
@@ -24,11 +25,14 @@ class BinarySearchTree {
|
||||
// 迴圈查詢,越過葉節點後跳出
|
||||
while (cur != null) {
|
||||
// 目標節點在 cur 的右子樹中
|
||||
cur = if (cur.value < num) cur.right
|
||||
cur = if (cur._val < num)
|
||||
cur.right
|
||||
// 目標節點在 cur 的左子樹中
|
||||
else if (cur.value > num) cur.left
|
||||
else if (cur._val > num)
|
||||
cur.left
|
||||
// 找到目標節點,跳出迴圈
|
||||
else break
|
||||
else
|
||||
break
|
||||
}
|
||||
// 返回目標節點
|
||||
return cur
|
||||
@@ -46,45 +50,60 @@ class BinarySearchTree {
|
||||
// 迴圈查詢,越過葉節點後跳出
|
||||
while (cur != null) {
|
||||
// 找到重複節點,直接返回
|
||||
if (cur.value == num) return
|
||||
if (cur._val == num)
|
||||
return
|
||||
pre = cur
|
||||
// 插入位置在 cur 的右子樹中
|
||||
cur = if (cur.value < num) cur.right
|
||||
cur = if (cur._val < num)
|
||||
cur.right
|
||||
// 插入位置在 cur 的左子樹中
|
||||
else cur.left
|
||||
else
|
||||
cur.left
|
||||
}
|
||||
// 插入節點
|
||||
val node = TreeNode(num)
|
||||
if (pre?.value!! < num) pre.right = node
|
||||
else pre.left = node
|
||||
if (pre?._val!! < num)
|
||||
pre.right = node
|
||||
else
|
||||
pre.left = node
|
||||
}
|
||||
|
||||
/* 刪除節點 */
|
||||
fun remove(num: Int) {
|
||||
// 若樹為空,直接提前返回
|
||||
if (root == null) return
|
||||
if (root == null)
|
||||
return
|
||||
var cur = root
|
||||
var pre: TreeNode? = null
|
||||
// 迴圈查詢,越過葉節點後跳出
|
||||
while (cur != null) {
|
||||
// 找到待刪除節點,跳出迴圈
|
||||
if (cur.value == num) break
|
||||
if (cur._val == num)
|
||||
break
|
||||
pre = cur
|
||||
// 待刪除節點在 cur 的右子樹中
|
||||
cur = if (cur.value < num) cur.right
|
||||
cur = if (cur._val < num)
|
||||
cur.right
|
||||
// 待刪除節點在 cur 的左子樹中
|
||||
else cur.left
|
||||
else
|
||||
cur.left
|
||||
}
|
||||
// 若無待刪除節點,則直接返回
|
||||
if (cur == null) return
|
||||
if (cur == null)
|
||||
return
|
||||
// 子節點數量 = 0 or 1
|
||||
if (cur.left == null || cur.right == null) {
|
||||
// 當子節點數量 = 0 / 1 時, child = null / 該子節點
|
||||
val child = if (cur.left != null) cur.left else cur.right
|
||||
val child = if (cur.left != null)
|
||||
cur.left
|
||||
else
|
||||
cur.right
|
||||
// 刪除節點 cur
|
||||
if (cur != root) {
|
||||
if (pre!!.left == cur) pre.left = child
|
||||
else pre.right = child
|
||||
if (pre!!.left == cur)
|
||||
pre.left = child
|
||||
else
|
||||
pre.right = child
|
||||
} else {
|
||||
// 若刪除節點為根節點,則重新指定根節點
|
||||
root = child
|
||||
@@ -97,9 +116,9 @@ class BinarySearchTree {
|
||||
tmp = tmp.left
|
||||
}
|
||||
// 遞迴刪除節點 tmp
|
||||
remove(tmp.value)
|
||||
remove(tmp._val)
|
||||
// 用 tmp 覆蓋 cur
|
||||
cur.value = tmp.value
|
||||
cur._val = tmp._val
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -118,7 +137,7 @@ fun main() {
|
||||
|
||||
/* 查詢節點 */
|
||||
val node = bst.search(7)
|
||||
println("查詢到的節點物件為 $node,節點值 = ${node?.value}")
|
||||
println("查詢到的節點物件為 $node,節點值 = ${node?._val}")
|
||||
|
||||
/* 插入節點 */
|
||||
bst.insert(16)
|
||||
|
||||
@@ -16,13 +16,14 @@ fun levelOrder(root: TreeNode?): MutableList<Int> {
|
||||
val queue = LinkedList<TreeNode?>()
|
||||
queue.add(root)
|
||||
// 初始化一個串列,用於儲存走訪序列
|
||||
val list = ArrayList<Int>()
|
||||
while (!queue.isEmpty()) {
|
||||
val node = queue.poll() // 隊列出隊
|
||||
list.add(node?.value!!) // 儲存節點值
|
||||
if (node.left != null) queue.offer(node.left) // 左子節點入列
|
||||
|
||||
if (node.right != null) queue.offer(node.right) // 右子節點入列
|
||||
val list = mutableListOf<Int>()
|
||||
while (queue.isNotEmpty()) {
|
||||
val node = queue.poll() // 隊列出隊
|
||||
list.add(node?._val!!) // 儲存節點值
|
||||
if (node.left != null)
|
||||
queue.offer(node.left) // 左子節點入列
|
||||
if (node.right != null)
|
||||
queue.offer(node.right) // 右子節點入列
|
||||
}
|
||||
return list
|
||||
}
|
||||
|
||||
@@ -10,13 +10,13 @@ import utils.TreeNode
|
||||
import utils.printTree
|
||||
|
||||
// 初始化串列,用於儲存走訪序列
|
||||
var list = ArrayList<Int>()
|
||||
var list = mutableListOf<Int>()
|
||||
|
||||
/* 前序走訪 */
|
||||
fun preOrder(root: TreeNode?) {
|
||||
if (root == null) return
|
||||
// 訪問優先順序:根節點 -> 左子樹 -> 右子樹
|
||||
list.add(root.value)
|
||||
list.add(root._val)
|
||||
preOrder(root.left)
|
||||
preOrder(root.right)
|
||||
}
|
||||
@@ -26,7 +26,7 @@ fun inOrder(root: TreeNode?) {
|
||||
if (root == null) return
|
||||
// 訪問優先順序:左子樹 -> 根節點 -> 右子樹
|
||||
inOrder(root.left)
|
||||
list.add(root.value)
|
||||
list.add(root._val)
|
||||
inOrder(root.right)
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ fun postOrder(root: TreeNode?) {
|
||||
// 訪問優先順序:左子樹 -> 右子樹 -> 根節點
|
||||
postOrder(root.left)
|
||||
postOrder(root.right)
|
||||
list.add(root.value)
|
||||
list.add(root._val)
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
|
||||
Reference in New Issue
Block a user