Add kotlin code for the utils file (#1175)

* feat(kotlin): add kotlin code for utils file.

* Update ListNode.kt

* Update PrintUtil.kt

---------

Co-authored-by: Yudong Jin <krahets@163.com>
This commit is contained in:
curtishd 2024-03-25 19:15:40 +08:00 committed by GitHub
parent 5e77a64341
commit 5ec5ef9af0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 238 additions and 0 deletions

View file

@ -0,0 +1,34 @@
/**
* File: ListNode.kt
* Created Time: 2024-01-25
* Author: curtishd (1023632660@qq.com)
*/
package utils
/* 链表节点 */
class ListNode(var value: Int) {
var next: ListNode? = null
companion object {
/* 将列表序列化为链表 */
fun arrToLinkedList(arr: IntArray): ListNode? {
val dum = ListNode(0)
var head = dum
for (value in arr) {
head.next = ListNode(value)
head = head.next!!
}
return dum.next
}
/* 获取链表中值为 value 的节点 */
fun getListNode(h: ListNode, value: Int): ListNode {
var head = h
while (head.value != value) {
head = head.next!!
}
return head
}
}
}

View file

@ -0,0 +1,106 @@
/**
* File: PrintUtil.kt
* Created Time: 2024-01-25
* Author: curtishd (1023632660@qq.com)
*/
package utils
import java.util.*
class Trunk(var prev: Trunk?, var str: String)
/* 打印矩阵Array */
fun <T> printMatrix(matrix: Array<Array<T>>) {
println("[")
for (row in matrix) {
println(" $row,")
}
println("]")
}
/* 打印矩阵List */
fun <T> printMatrix(matrix: List<List<T>>) {
println("[")
for (row in matrix) {
println(" $row,")
}
println("]")
}
/* 打印链表 */
fun printLinkedList(h: ListNode?) {
var head = h
val list = ArrayList<String>()
while (head != null) {
list.add(head.value.toString())
head = head.next
}
println(list.joinToString(separator = " -> "))
}
/* 打印二叉树 */
fun printTree(root: TreeNode?) {
printTree(root, null, false)
}
/**
* 打印二叉树
* This tree printer is borrowed from TECHIE DELIGHT
* https://www.techiedelight.com/c-program-print-binary-tree/
*/
fun printTree(root: TreeNode?, prev: Trunk?, isRight: Boolean) {
if (root == null) {
return
}
var prevStr = " "
val trunk = Trunk(prev, prevStr)
printTree(root.right, trunk, true)
if (prev == null) {
trunk.str = "———"
} else if (isRight) {
trunk.str = "/———"
prevStr = " |"
} else {
trunk.str = "\\———"
prev.str = prevStr
}
showTrunks(trunk)
println(" ${root.value}")
if (prev != null) {
prev.str = prevStr
}
trunk.str = " |"
printTree(root.left, trunk, false)
}
fun showTrunks(p: Trunk?) {
if (p == null) {
return
}
showTrunks(p.prev)
print(p.str)
}
/* 打印哈希表 */
fun <K, V> printHashMap(map: Map<K, V>) {
for ((key, value) in map) {
println(key.toString() + " -> " + value)
}
}
/* 打印堆 */
fun printHeap(queue: Queue<Int>?) {
val list = queue?.let { ArrayList(it) }
print("堆的数组表示:")
println(list)
println("堆的树状表示:")
val root = list?.let { TreeNode.listToTree(it) }
printTree(root)
}

View file

@ -0,0 +1,68 @@
/**
* File: TreeNode.kt
* Created Time: 2024-01-25
* Author: curtishd (1023632660@qq.com)
*/
package utils
/* 二叉树节点类 */
class TreeNode(
var value: Int // 节点值
) {
var height: Int = 0 // 节点高度
var left: TreeNode? = null // 左子节点引用
var right: TreeNode? = null // 右子节点引用
// 序列化编码规则请参考:
// https://www.hello-algo.com/chapter_tree/array_representation_of_tree/
// 二叉树的数组表示:
// [1, 2, 3, 4, None, 6, 7, 8, 9, None, None, 12, None, None, 15]
// 二叉树的链表表示:
// /——— 15
// /——— 7
// /——— 3
// | \——— 6
// | \——— 12
// ——— 1
// \——— 2
// | /——— 9
// \——— 4
// \——— 8
/* 将列表反序列化为二叉树:递归 */
companion object {
private fun listToTreeDFS(arr: MutableList<Int?>, i: Int): TreeNode? {
if (i < 0 || i >= arr.size || arr[i] == null) {
return null
}
val root = TreeNode(arr[i]!!)
root.left = listToTreeDFS(arr, 2 * i + 1)
root.right = listToTreeDFS(arr, 2 * i + 2)
return root
}
/* 将列表反序列化为二叉树 */
fun listToTree(arr: MutableList<Int?>): TreeNode? {
return listToTreeDFS(arr, 0)
}
/* 将二叉树序列化为列表:递归 */
private fun treeToListDFS(root: TreeNode?, i: Int, res: MutableList<Int?>) {
if (root == null) return
while (i >= res.size) {
res.add(null)
}
res[i] = root.value
treeToListDFS(root.left, 2 * i + 1, res)
treeToListDFS(root.right, 2 * i + 2, res)
}
/* 将二叉树序列化为列表 */
fun treeToList(root: TreeNode?): List<Int?> {
val res = ArrayList<Int?>()
treeToListDFS(root, 0, res)
return res
}
}
}

View file

@ -0,0 +1,30 @@
/**
* File: Vertex.kt
* Created Time: 2024-01-25
* Author: curtishd (1023632660@qq.com)
*/
package utils
/* 顶点类 */
class Vertex(val value: Int) {
companion object {
/* 输入值列表 vals ,返回顶点列表 vets */
fun valsToVets(vals: IntArray): Array<Vertex?> {
val vets = arrayOfNulls<Vertex>(vals.size)
for (i in vals.indices) {
vets[i] = Vertex(vals[i])
}
return vets
}
/* 输入顶点列表 vets ,返回值列表 vals */
fun vetsToVals(vets: List<Vertex?>): List<Int> {
val vals = ArrayList<Int>()
for (vet in vets) {
vals.add(vet!!.value)
}
return vals
}
}
}