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:
curtishd
2024-04-10 18:09:43 +08:00
committed by GitHub
parent 39e2e1a5c1
commit 1623e3c6a8
28 changed files with 152 additions and 152 deletions

View File

@@ -41,7 +41,7 @@ fun find(head: ListNode?, target: Int): Int {
var index = 0
var h = head
while (h != null) {
if (h.value == target)
if (h._val == target)
return index
h = h.next
index++
@@ -79,7 +79,7 @@ fun main() {
/* 访问节点 */
val node: ListNode = access(n0, 3)!!
println("链表中索引 3 处的节点的值 = ${node.value}")
println("链表中索引 3 处的节点的值 = ${node._val}")
/* 查找节点 */
val index: Int = find(n0, 2)

View File

@@ -16,7 +16,7 @@ fun preOrder(root: TreeNode?) {
if (root == null) {
return
}
if (root.value == 7) {
if (root._val == 7) {
// 记录解
res!!.add(root)
}
@@ -37,7 +37,7 @@ fun main() {
println("\n输出所有值为 7 的节点")
val vals = mutableListOf<Int>()
for (node in res!!) {
vals.add(node.value)
vals.add(node._val)
}
println(vals)
}

View File

@@ -19,7 +19,7 @@ fun preOrder(root: TreeNode?) {
}
// 尝试
path!!.add(root)
if (root.value == 7) {
if (root._val == 7) {
// 记录解
res!!.add(path!!.toMutableList())
}
@@ -42,10 +42,10 @@ fun main() {
println("\n输出所有根节点到节点 7 的路径")
for (path in res!!) {
val values = mutableListOf<Int>()
val _vals = mutableListOf<Int>()
for (node in path) {
values.add(node.value)
_vals.add(node._val)
}
println(values)
println(_vals)
}
}

View File

@@ -15,12 +15,12 @@ var res: MutableList<MutableList<TreeNode>>? = null
/* 前序遍历:例题三 */
fun preOrder(root: TreeNode?) {
// 剪枝
if (root == null || root.value == 3) {
if (root == null || root._val == 3) {
return
}
// 尝试
path!!.add(root)
if (root.value == 7) {
if (root._val == 7) {
// 记录解
res!!.add(path!!.toMutableList())
}
@@ -43,10 +43,10 @@ fun main() {
println("\n输出所有根节点到节点 7 的路径,路径中不包含值为 3 的节点")
for (path in res!!) {
val values = mutableListOf<Int>()
val _vals = mutableListOf<Int>()
for (node in path) {
values.add(node.value)
_vals.add(node._val)
}
println(values)
println(_vals)
}
}

View File

@@ -11,7 +11,7 @@ import utils.printTree
/* 判断当前状态是否为解 */
fun isSolution(state: MutableList<TreeNode?>): Boolean {
return state.isNotEmpty() && state[state.size - 1]?.value == 7
return state.isNotEmpty() && state[state.size - 1]?._val == 7
}
/* 记录解 */
@@ -21,7 +21,7 @@ fun recordSolution(state: MutableList<TreeNode?>?, res: MutableList<MutableList<
/* 判断在当前状态下,该选择是否合法 */
fun isValid(state: MutableList<TreeNode?>?, choice: TreeNode?): Boolean {
return choice != null && choice.value != 3
return choice != null && choice._val != 3
}
/* 更新状态 */
@@ -74,7 +74,7 @@ fun main() {
val vals = mutableListOf<Int>()
for (node in path!!) {
if (node != null) {
vals.add(node.value)
vals.add(node._val)
}
}
println(vals)

View File

@@ -11,7 +11,7 @@ import kotlin.math.max
/* 0-1 背包:暴力搜索 */
fun knapsackDFS(
wgt: IntArray,
value: IntArray,
_val: IntArray,
i: Int,
c: Int
): Int {
@@ -21,11 +21,11 @@ fun knapsackDFS(
}
// 若超过背包容量,则只能选择不放入背包
if (wgt[i - 1] > c) {
return knapsackDFS(wgt, value, i - 1, c)
return knapsackDFS(wgt, _val, i - 1, c)
}
// 计算不放入和放入物品 i 的最大价值
val no = knapsackDFS(wgt, value, i - 1, c)
val yes = knapsackDFS(wgt, value, i - 1, c - wgt[i - 1]) + value[i - 1]
val no = knapsackDFS(wgt, _val, i - 1, c)
val yes = knapsackDFS(wgt, _val, i - 1, c - wgt[i - 1]) + _val[i - 1]
// 返回两种方案中价值更大的那一个
return max(no, yes)
}
@@ -33,7 +33,7 @@ fun knapsackDFS(
/* 0-1 背包:记忆化搜索 */
fun knapsackDFSMem(
wgt: IntArray,
value: IntArray,
_val: IntArray,
mem: Array<IntArray>,
i: Int,
c: Int
@@ -48,11 +48,11 @@ fun knapsackDFSMem(
}
// 若超过背包容量,则只能选择不放入背包
if (wgt[i - 1] > c) {
return knapsackDFSMem(wgt, value, mem, i - 1, c)
return knapsackDFSMem(wgt, _val, mem, i - 1, c)
}
// 计算不放入和放入物品 i 的最大价值
val no = knapsackDFSMem(wgt, value, mem, i - 1, c)
val yes = knapsackDFSMem(wgt, value, mem, i - 1, c - wgt[i - 1]) + value[i - 1]
val no = knapsackDFSMem(wgt, _val, mem, i - 1, c)
val yes = knapsackDFSMem(wgt, _val, mem, i - 1, c - wgt[i - 1]) + _val[i - 1]
// 记录并返回两种方案中价值更大的那一个
mem[i][c] = max(no, yes)
return mem[i][c]
@@ -61,7 +61,7 @@ fun knapsackDFSMem(
/* 0-1 背包:动态规划 */
fun knapsackDP(
wgt: IntArray,
value: IntArray,
_val: IntArray,
cap: Int
): Int {
val n = wgt.size
@@ -75,7 +75,7 @@ fun knapsackDP(
dp[i][c] = dp[i - 1][c]
} else {
// 不选和选物品 i 这两种方案的较大值
dp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + value[i - 1])
dp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + _val[i - 1])
}
}
}
@@ -85,7 +85,7 @@ fun knapsackDP(
/* 0-1 背包:空间优化后的动态规划 */
fun knapsackDPComp(
wgt: IntArray,
value: IntArray,
_val: IntArray,
cap: Int
): Int {
val n = wgt.size
@@ -98,7 +98,7 @@ fun knapsackDPComp(
if (wgt[i - 1] <= c) {
// 不选和选物品 i 这两种方案的较大值
dp[c] =
max(dp[c], dp[c - wgt[i - 1]] + value[i - 1])
max(dp[c], dp[c - wgt[i - 1]] + _val[i - 1])
}
}
}
@@ -108,12 +108,12 @@ fun knapsackDPComp(
/* Driver Code */
fun main() {
val wgt = intArrayOf(10, 20, 30, 40, 50)
val value = intArrayOf(50, 120, 150, 210, 240)
val _val = intArrayOf(50, 120, 150, 210, 240)
val cap = 50
val n = wgt.size
// 暴力搜索
var res = knapsackDFS(wgt, value, n, cap)
var res = knapsackDFS(wgt, _val, n, cap)
println("不超过背包容量的最大物品价值为 $res")
// 记忆化搜索
@@ -121,14 +121,14 @@ fun main() {
for (row in mem) {
row.fill(-1)
}
res = knapsackDFSMem(wgt, value, mem, n, cap)
res = knapsackDFSMem(wgt, _val, mem, n, cap)
println("不超过背包容量的最大物品价值为 $res")
// 动态规划
res = knapsackDP(wgt, value, cap)
res = knapsackDP(wgt, _val, cap)
println("不超过背包容量的最大物品价值为 $res")
// 空间优化后的动态规划
res = knapsackDPComp(wgt, value, cap)
res = knapsackDPComp(wgt, _val, cap)
println("不超过背包容量的最大物品价值为 $res")
}

View File

@@ -9,7 +9,7 @@ package chapter_dynamic_programming
import kotlin.math.max
/* 完全背包:动态规划 */
fun unboundedKnapsackDP(wgt: IntArray, value: IntArray, cap: Int): Int {
fun unboundedKnapsackDP(wgt: IntArray, _val: IntArray, cap: Int): Int {
val n = wgt.size
// 初始化 dp 表
val dp = Array(n + 1) { IntArray(cap + 1) }
@@ -21,7 +21,7 @@ fun unboundedKnapsackDP(wgt: IntArray, value: IntArray, cap: Int): Int {
dp[i][c] = dp[i - 1][c]
} else {
// 不选和选物品 i 这两种方案的较大值
dp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + value[i - 1])
dp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + _val[i - 1])
}
}
}
@@ -31,7 +31,7 @@ fun unboundedKnapsackDP(wgt: IntArray, value: IntArray, cap: Int): Int {
/* 完全背包:空间优化后的动态规划 */
fun unboundedKnapsackDPComp(
wgt: IntArray,
value: IntArray,
_val: IntArray,
cap: Int
): Int {
val n = wgt.size
@@ -45,7 +45,7 @@ fun unboundedKnapsackDPComp(
dp[c] = dp[c]
} else {
// 不选和选物品 i 这两种方案的较大值
dp[c] = max(dp[c], dp[c - wgt[i - 1]] + value[i - 1])
dp[c] = max(dp[c], dp[c - wgt[i - 1]] + _val[i - 1])
}
}
}
@@ -55,14 +55,14 @@ fun unboundedKnapsackDPComp(
/* Driver Code */
fun main() {
val wgt = intArrayOf(1, 2, 3)
val value = intArrayOf(5, 11, 15)
val _val = intArrayOf(5, 11, 15)
val cap = 4
// 动态规划
var res = unboundedKnapsackDP(wgt, value, cap)
var res = unboundedKnapsackDP(wgt, _val, cap)
println("不超过背包容量的最大物品价值为 $res")
// 空间优化后的动态规划
res = unboundedKnapsackDPComp(wgt, value, cap)
res = unboundedKnapsackDPComp(wgt, _val, cap)
println("不超过背包容量的最大物品价值为 $res")
}

View File

@@ -72,9 +72,9 @@ class GraphAdjList(edges: Array<Array<Vertex?>>) {
for (pair in adjList.entries) {
val tmp = mutableListOf<Int>()
for (vertex in pair.value) {
tmp.add(vertex.value)
tmp.add(vertex._val)
}
println("${pair.key.value}: $tmp,")
println("${pair.key._val}: $tmp,")
}
}
}

View File

@@ -32,10 +32,10 @@ class GraphAdjMat(vertices: IntArray, edges: Array<IntArray>) {
}
/* 添加顶点 */
fun addVertex(value: Int) {
fun addVertex(_val: Int) {
val n = size()
// 向顶点列表中添加新顶点的值
vertices.add(value)
vertices.add(_val)
// 在邻接矩阵中添加一行
val newRow = mutableListOf<Int>()
for (j in 0..<n) {

View File

@@ -42,10 +42,10 @@ fun fractionalKnapsack(wgt: IntArray, _val: IntArray, c: Int): Double {
/* Driver Code */
fun main() {
val wgt = intArrayOf(10, 20, 30, 40, 50)
val values = intArrayOf(50, 120, 150, 210, 240)
val _val = intArrayOf(50, 120, 150, 210, 240)
val cap = 50
// 贪心算法
val res = fractionalKnapsack(wgt, values, cap)
val res = fractionalKnapsack(wgt, _val, cap)
println("不超过背包容量的最大物品价值为 $res")
}

View File

@@ -9,7 +9,7 @@ package chapter_hashing
/* 键值对 */
class Pair(
var key: Int,
var value: String
var _val: String
)
/* 基于数组实现的哈希表 */
@@ -27,12 +27,12 @@ class ArrayHashMap {
fun get(key: Int): String? {
val index = hashFunc(key)
val pair = buckets[index] ?: return null
return pair.value
return pair._val
}
/* 添加操作 */
fun put(key: Int, value: String) {
val pair = Pair(key, value)
fun put(key: Int, _val: String) {
val pair = Pair(key, _val)
val index = hashFunc(key)
buckets[index] = pair
}
@@ -68,7 +68,7 @@ class ArrayHashMap {
fun valueSet(): MutableList<String> {
val valueSet = mutableListOf<String>()
for (pair in buckets) {
pair?.let { valueSet.add(it.value) }
pair?.let { valueSet.add(it._val) }
}
return valueSet
}
@@ -77,8 +77,8 @@ class ArrayHashMap {
fun print() {
for (kv in pairSet()) {
val key = kv.key
val value = kv.value
println("${key} -> ${value}")
val _val = kv._val
println("${key} -> ${_val}")
}
}
}
@@ -112,14 +112,14 @@ fun main() {
/* 遍历哈希表 */
println("\n遍历键值对 Key -> Value")
for (kv in map.pairSet()) {
println("${kv.key} -> ${kv.value}")
println("${kv.key} -> ${kv._val}")
}
println("\n单独遍历键 Key")
for (key in map.keySet()) {
println(key)
}
println("\n单独遍历值 Value")
for (value in map.valueSet()) {
println(value)
for (_val in map.valueSet()) {
println(_val)
}
}

View File

@@ -44,7 +44,7 @@ fun main() {
println(key)
}
println("\n单独遍历值 Value")
for (value in map.values) {
println(value)
for (_val in map.values) {
println(_val)
}
}

View File

@@ -42,14 +42,14 @@ class HashMapChaining() {
val bucket = buckets[index]
// 遍历桶,若找到 key ,则返回对应 val
for (pair in bucket) {
if (pair.key == key) return pair.value
if (pair.key == key) return pair._val
}
// 若未找到 key ,则返回 null
return null
}
/* 添加操作 */
fun put(key: Int, value: String) {
fun put(key: Int, _val: String) {
// 当负载因子超过阈值时,执行扩容
if (loadFactor() > loadThres) {
extend()
@@ -59,12 +59,12 @@ class HashMapChaining() {
// 遍历桶,若遇到指定 key ,则更新对应 val 并返回
for (pair in bucket) {
if (pair.key == key) {
pair.value = value
pair._val = _val
return
}
}
// 若无该 key ,则将键值对添加至尾部
val pair = Pair(key, value)
val pair = Pair(key, _val)
bucket.add(pair)
size++
}
@@ -98,7 +98,7 @@ class HashMapChaining() {
// 将键值对从原哈希表搬运至新哈希表
for (bucket in bucketsTmp) {
for (pair in bucket) {
put(pair.key, pair.value)
put(pair.key, pair._val)
}
}
}
@@ -109,7 +109,7 @@ class HashMapChaining() {
val res = mutableListOf<String>()
for (pair in bucket) {
val k = pair.key
val v = pair.value
val v = pair._val
res.add("$k -> $v")
}
println(res)

View File

@@ -68,14 +68,14 @@ class HashMapOpenAddressing {
val index = findBucket(key)
// 若找到键值对,则返回对应 val
if (buckets[index] != null && buckets[index] != TOMBSTONE) {
return buckets[index]?.value
return buckets[index]?._val
}
// 若键值对不存在,则返回 null
return null
}
/* 添加操作 */
fun put(key: Int, value: String) {
fun put(key: Int, _val: String) {
// 当负载因子超过阈值时,执行扩容
if (loadFactor() > loadThres) {
extend()
@@ -84,11 +84,11 @@ class HashMapOpenAddressing {
val index = findBucket(key)
// 若找到键值对,则覆盖 val 并返回
if (buckets[index] != null && buckets[index] != TOMBSTONE) {
buckets[index]!!.value = value
buckets[index]!!._val = _val
return
}
// 若键值对不存在,则添加该键值对
buckets[index] = Pair(key, value)
buckets[index] = Pair(key, _val)
size++
}
@@ -114,7 +114,7 @@ class HashMapOpenAddressing {
// 将键值对从原哈希表搬运至新哈希表
for (pair in bucketsTmp) {
if (pair != null && pair != TOMBSTONE) {
put(pair.key, pair.value)
put(pair.key, pair._val)
}
}
}
@@ -127,7 +127,7 @@ class HashMapOpenAddressing {
} else if (pair == TOMBSTONE) {
println("TOMESTOME")
} else {
println("${pair.key} -> ${pair.value}")
println("${pair.key} -> ${pair._val}")
}
}
}

View File

@@ -9,15 +9,15 @@ package chapter_heap
import utils.printHeap
import java.util.*
fun testPush(heap: Queue<Int>, value: Int) {
heap.offer(value) // 元素入堆
print("\n元素 $value 入堆后\n")
fun testPush(heap: Queue<Int>, _val: Int) {
heap.offer(_val) // 元素入堆
print("\n元素 $_val 入堆后\n")
printHeap(heap)
}
fun testPop(heap: Queue<Int>) {
val value = heap.poll() // 堆顶元素出堆
print("\n堆顶元素 $value 出堆后\n")
val _val = heap.poll() // 堆顶元素出堆
print("\n堆顶元素 $_val 出堆后\n")
printHeap(heap)
}

View File

@@ -61,9 +61,9 @@ class MaxHeap(nums: MutableList<Int>?) {
}
/* 元素入堆 */
fun push(value: Int) {
fun push(_val: Int) {
// 添加节点
maxHeap.add(value)
maxHeap.add(_val)
// 从底至顶堆化
siftUp(size() - 1)
}
@@ -91,11 +91,11 @@ class MaxHeap(nums: MutableList<Int>?) {
// 交换根节点与最右叶节点(交换首元素与尾元素)
swap(0, size() - 1)
// 删除节点
val value = maxHeap.removeAt(size() - 1)
val _val = maxHeap.removeAt(size() - 1)
// 从顶至底堆化
siftDown(0)
// 返回堆顶元素
return value
return _val
}
/* 从节点 i 开始,从顶至底堆化 */
@@ -138,9 +138,9 @@ fun main() {
print("\n堆顶元素为 $peek\n")
/* 元素入堆 */
val value = 7
maxHeap.push(value)
print("\n元素 $value 入堆后\n")
val _val = 7
maxHeap.push(_val)
print("\n元素 $_val 入堆后\n")
maxHeap.print()
/* 堆顶元素出堆 */

View File

@@ -10,14 +10,14 @@ import utils.ListNode
/* 哈希查找(数组) */
fun hashingSearchArray(map: Map<Int?, Int>, target: Int): Int {
// 哈希表的 key: 目标元素value: 索引
// 哈希表的 key: 目标元素,_val: 索引
// 若哈希表中无此 key ,返回 -1
return map.getOrDefault(target, -1)
}
/* 哈希查找(链表) */
fun hashingSearchLinkedList(map: Map<Int?, ListNode?>, target: Int): ListNode? {
// 哈希表的 key: 目标节点值value: 节点对象
// 哈希表的 key: 目标节点值,_val: 节点对象
// 若哈希表中无此 key ,返回 null
return map.getOrDefault(target, null)
}
@@ -31,7 +31,7 @@ fun main() {
// 初始化哈希表
val map = HashMap<Int?, Int>()
for (i in nums.indices) {
map[nums[i]] = i // key: 元素value: 索引
map[nums[i]] = i // key: 元素,_val: 索引
}
val index = hashingSearchArray(map, target)
println("目标元素 3 的索引 = $index")
@@ -41,7 +41,7 @@ fun main() {
// 初始化哈希表
val map1 = HashMap<Int?, ListNode?>()
while (head != null) {
map1[head.value] = head // key: 节点值value: 节点
map1[head._val] = head // key: 节点值,_val: 节点
head = head.next
}
val node = hashingSearchLinkedList(map1, target)

View File

@@ -26,7 +26,7 @@ fun linearSearchLinkedList(h: ListNode?, target: Int): ListNode? {
var head = h
while (head != null) {
// 找到目标节点,返回之
if (head.value == target)
if (head._val == target)
return head
head = head.next
}

View File

@@ -66,10 +66,10 @@ class LinkedListDeque {
fun pop(isFront: Boolean): Int {
if (isEmpty())
throw IndexOutOfBoundsException()
val value: Int
val _val: Int
// 队首出队操作
if (isFront) {
value = front!!._val // 暂存头节点值
_val = front!!._val // 暂存头节点值
// 删除头节点
val fNext = front!!.next
if (fNext != null) {
@@ -79,7 +79,7 @@ class LinkedListDeque {
front = fNext // 更新头节点
// 队尾出队操作
} else {
value = rear!!._val // 暂存尾节点值
_val = rear!!._val // 暂存尾节点值
// 删除尾节点
val rPrev = rear!!.prev
if (rPrev != null) {
@@ -89,7 +89,7 @@ class LinkedListDeque {
rear = rPrev // 更新尾节点
}
queSize-- // 更新队列长度
return value
return _val
}
/* 队首出队 */

View File

@@ -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()

View File

@@ -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插入节点 $valueAVL 树为")
fun testInsert(tree: AVLTree, _val: Int) {
tree.insert(_val)
println("\n插入节点 $_valAVL 树为")
printTree(tree.root)
}
fun testRemove(tree: AVLTree, value: Int) {
tree.remove(value)
println("\n删除节点 $valueAVL 树为")
fun testRemove(tree: AVLTree, _val: Int) {
tree.remove(_val)
println("\n删除节点 $_valAVL 树为")
printTree(tree.root)
}
@@ -219,5 +219,5 @@ fun main() {
/* 查询节点 */
val node = avlTree.search(7)
println("\n 查找到的节点对象为 $node,节点值 = ${node?.value}")
println("\n 查找到的节点对象为 $node,节点值 = ${node?._val}")
}

View File

@@ -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)

View File

@@ -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)

View File

@@ -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 */

View File

@@ -7,7 +7,7 @@
package utils
/* 链表节点 */
class ListNode(var value: Int) {
class ListNode(var _val: Int) {
var next: ListNode? = null
companion object {
@@ -15,8 +15,8 @@ class ListNode(var value: Int) {
fun arrToLinkedList(arr: IntArray): ListNode? {
val dum = ListNode(0)
var head = dum
for (value in arr) {
head.next = ListNode(value)
for (_val in arr) {
head.next = ListNode(_val)
head = head.next!!
}
return dum.next

View File

@@ -33,7 +33,7 @@ fun printLinkedList(h: ListNode?) {
var head = h
val list = mutableListOf<String>()
while (head != null) {
list.add(head.value.toString())
list.add(head._val.toString())
head = head.next
}
println(list.joinToString(separator = " -> "))
@@ -70,7 +70,7 @@ fun printTree(root: TreeNode?, prev: Trunk?, isRight: Boolean) {
}
showTrunks(trunk)
println(" ${root.value}")
println(" ${root._val}")
if (prev != null) {
prev.str = prevStr

View File

@@ -9,7 +9,7 @@ package utils
/* 二叉树节点类 */
/* 构造方法 */
class TreeNode(
var value: Int // 节点值
var _val: Int // 节点值
) {
var height: Int = 0 // 节点高度
var left: TreeNode? = null // 左子节点引用
@@ -54,7 +54,7 @@ class TreeNode(
while (i >= res.size) {
res.add(null)
}
res[i] = root.value
res[i] = root._val
treeToListDFS(root.left, 2 * i + 1, res)
treeToListDFS(root.right, 2 * i + 2, res)
}

View File

@@ -7,7 +7,7 @@
package utils
/* 顶点类 */
class Vertex(val value: Int) {
class Vertex(val _val: Int) {
companion object {
/* 输入值列表 vals ,返回顶点列表 vets */
fun valsToVets(vals: IntArray): Array<Vertex?> {
@@ -22,7 +22,7 @@ class Vertex(val value: Int) {
fun vetsToVals(vets: MutableList<Vertex?>): MutableList<Int> {
val vals = mutableListOf<Int>()
for (vet in vets) {
vals.add(vet!!.value)
vals.add(vet!!._val)
}
return vals
}