From 5ec5ef9af03c89ad4af53508b19d663d3dc4d7eb Mon Sep 17 00:00:00 2001 From: curtishd <131777542+curtishd@users.noreply.github.com> Date: Mon, 25 Mar 2024 19:15:40 +0800 Subject: [PATCH] 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 --- codes/kotlin/utils/ListNode.kt | 34 ++++++++++ codes/kotlin/utils/PrintUtil.kt | 106 ++++++++++++++++++++++++++++++++ codes/kotlin/utils/TreeNode.kt | 68 ++++++++++++++++++++ codes/kotlin/utils/Vertex.kt | 30 +++++++++ 4 files changed, 238 insertions(+) create mode 100644 codes/kotlin/utils/ListNode.kt create mode 100644 codes/kotlin/utils/PrintUtil.kt create mode 100644 codes/kotlin/utils/TreeNode.kt create mode 100644 codes/kotlin/utils/Vertex.kt diff --git a/codes/kotlin/utils/ListNode.kt b/codes/kotlin/utils/ListNode.kt new file mode 100644 index 000000000..82c1bf296 --- /dev/null +++ b/codes/kotlin/utils/ListNode.kt @@ -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 + } + } +} diff --git a/codes/kotlin/utils/PrintUtil.kt b/codes/kotlin/utils/PrintUtil.kt new file mode 100644 index 000000000..f81984e27 --- /dev/null +++ b/codes/kotlin/utils/PrintUtil.kt @@ -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 printMatrix(matrix: Array>) { + println("[") + for (row in matrix) { + println(" $row,") + } + println("]") +} + +/* 打印矩阵(List) */ +fun printMatrix(matrix: List>) { + println("[") + for (row in matrix) { + println(" $row,") + } + println("]") +} + +/* 打印链表 */ +fun printLinkedList(h: ListNode?) { + var head = h + val list = ArrayList() + 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 printHashMap(map: Map) { + for ((key, value) in map) { + println(key.toString() + " -> " + value) + } +} + +/* 打印堆 */ +fun printHeap(queue: Queue?) { + val list = queue?.let { ArrayList(it) } + print("堆的数组表示:") + println(list) + println("堆的树状表示:") + val root = list?.let { TreeNode.listToTree(it) } + printTree(root) +} diff --git a/codes/kotlin/utils/TreeNode.kt b/codes/kotlin/utils/TreeNode.kt new file mode 100644 index 000000000..a9b6e1777 --- /dev/null +++ b/codes/kotlin/utils/TreeNode.kt @@ -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, 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): TreeNode? { + return listToTreeDFS(arr, 0) + } + + /* 将二叉树序列化为列表:递归 */ + private fun treeToListDFS(root: TreeNode?, i: Int, res: MutableList) { + 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 { + val res = ArrayList() + treeToListDFS(root, 0, res) + return res + } + } +} \ No newline at end of file diff --git a/codes/kotlin/utils/Vertex.kt b/codes/kotlin/utils/Vertex.kt new file mode 100644 index 000000000..2abe51fb2 --- /dev/null +++ b/codes/kotlin/utils/Vertex.kt @@ -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 { + val vets = arrayOfNulls(vals.size) + for (i in vals.indices) { + vets[i] = Vertex(vals[i]) + } + return vets + } + + /* 输入顶点列表 vets ,返回值列表 vals */ + fun vetsToVals(vets: List): List { + val vals = ArrayList() + for (vet in vets) { + vals.add(vet!!.value) + } + return vals + } + } +} \ No newline at end of file