mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-06 20:54:19 +00:00
feat(Kotlin): Replace value with _val (#1254)
* ci(kotlin): Add workflow file. * Update kotlin.yml * style(kotlin): value -> _val --------- Co-authored-by: Yudong Jin <krahets@163.com>
This commit is contained in:
@@ -18,7 +18,7 @@ class ArrayBinaryTree(private val tree: MutableList<Int?>) {
|
||||
}
|
||||
|
||||
/* 获取索引为 i 节点的值 */
|
||||
fun value(i: Int): Int? {
|
||||
fun _val(i: Int): Int? {
|
||||
// 若索引越界,则返回 null ,代表空位
|
||||
if (i < 0 || i >= size()) return null
|
||||
return tree[i]
|
||||
@@ -44,8 +44,8 @@ class ArrayBinaryTree(private val tree: 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
|
||||
}
|
||||
@@ -53,19 +53,19 @@ class ArrayBinaryTree(private val tree: MutableList<Int?>) {
|
||||
/* 深度优先遍历 */
|
||||
fun dfs(i: Int, order: String, res: MutableList<Int?>) {
|
||||
// 若为空位,则返回
|
||||
if (value(i) == null)
|
||||
if (_val(i) == null)
|
||||
return
|
||||
// 前序遍历
|
||||
if ("pre" == order)
|
||||
res.add(value(i))
|
||||
res.add(_val(i))
|
||||
dfs(left(i), order, res)
|
||||
// 中序遍历
|
||||
if ("in" == order)
|
||||
res.add(value(i))
|
||||
res.add(_val(i))
|
||||
dfs(right(i), order, res)
|
||||
// 后序遍历
|
||||
if ("post" == order)
|
||||
res.add(value(i))
|
||||
res.add(_val(i))
|
||||
}
|
||||
|
||||
/* 前序遍历 */
|
||||
@@ -111,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