mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 21:24:53 +08: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()
|
||||
|
||||
Reference in New Issue
Block a user