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>pull/1176/head
parent
5e77a64341
commit
5ec5ef9af0
@ -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
|
||||
}
|
||||
}
|
||||
}
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue