mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-20 15:30:58 +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()
|
||||
|
||||
@@ -93,20 +93,20 @@ 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)
|
||||
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) // 更新节点高度
|
||||
@@ -117,18 +117,18 @@ 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)
|
||||
@@ -147,8 +147,8 @@ class AVLTree {
|
||||
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) // 更新节点高度
|
||||
@@ -159,15 +159,15 @@ 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 = if (cur._val < _val)
|
||||
cur.right!!
|
||||
// 目标节点在 cur 的左子树中
|
||||
else if (cur.value > value)
|
||||
else if (cur._val > _val)
|
||||
cur.left
|
||||
// 找到目标节点,跳出循环
|
||||
else
|
||||
@@ -178,15 +178,15 @@ class AVLTree {
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -219,5 +219,5 @@ fun main() {
|
||||
|
||||
/* 查询节点 */
|
||||
val node = avlTree.search(7)
|
||||
println("\n 查找到的节点对象为 $node,节点值 = ${node?.value}")
|
||||
println("\n 查找到的节点对象为 $node,节点值 = ${node?._val}")
|
||||
}
|
||||
@@ -25,10 +25,10 @@ class BinarySearchTree {
|
||||
// 循环查找,越过叶节点后跳出
|
||||
while (cur != null) {
|
||||
// 目标节点在 cur 的右子树中
|
||||
cur = if (cur.value < num)
|
||||
cur = if (cur._val < num)
|
||||
cur.right
|
||||
// 目标节点在 cur 的左子树中
|
||||
else if (cur.value > num)
|
||||
else if (cur._val > num)
|
||||
cur.left
|
||||
// 找到目标节点,跳出循环
|
||||
else
|
||||
@@ -50,11 +50,11 @@ class BinarySearchTree {
|
||||
// 循环查找,越过叶节点后跳出
|
||||
while (cur != null) {
|
||||
// 找到重复节点,直接返回
|
||||
if (cur.value == num)
|
||||
if (cur._val == num)
|
||||
return
|
||||
pre = cur
|
||||
// 插入位置在 cur 的右子树中
|
||||
cur = if (cur.value < num)
|
||||
cur = if (cur._val < num)
|
||||
cur.right
|
||||
// 插入位置在 cur 的左子树中
|
||||
else
|
||||
@@ -62,7 +62,7 @@ class BinarySearchTree {
|
||||
}
|
||||
// 插入节点
|
||||
val node = TreeNode(num)
|
||||
if (pre?.value!! < num)
|
||||
if (pre?._val!! < num)
|
||||
pre.right = node
|
||||
else
|
||||
pre.left = node
|
||||
@@ -78,11 +78,11 @@ class BinarySearchTree {
|
||||
// 循环查找,越过叶节点后跳出
|
||||
while (cur != null) {
|
||||
// 找到待删除节点,跳出循环
|
||||
if (cur.value == num)
|
||||
if (cur._val == num)
|
||||
break
|
||||
pre = cur
|
||||
// 待删除节点在 cur 的右子树中
|
||||
cur = if (cur.value < num)
|
||||
cur = if (cur._val < num)
|
||||
cur.right
|
||||
// 待删除节点在 cur 的左子树中
|
||||
else
|
||||
@@ -116,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
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -137,7 +137,7 @@ fun main() {
|
||||
|
||||
/* 查找节点 */
|
||||
val node = bst.search(7)
|
||||
println("查找到的节点对象为 $node,节点值 = ${node?.value}")
|
||||
println("查找到的节点对象为 $node,节点值 = ${node?._val}")
|
||||
|
||||
/* 插入节点 */
|
||||
bst.insert(16)
|
||||
|
||||
@@ -19,7 +19,7 @@ fun levelOrder(root: TreeNode?): MutableList<Int> {
|
||||
val list = mutableListOf<Int>()
|
||||
while (queue.isNotEmpty()) {
|
||||
val node = queue.poll() // 队列出队
|
||||
list.add(node?.value!!) // 保存节点值
|
||||
list.add(node?._val!!) // 保存节点值
|
||||
if (node.left != null)
|
||||
queue.offer(node.left) // 左子节点入队
|
||||
if (node.right != null)
|
||||
|
||||
@@ -16,7 +16,7 @@ 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