diff --git a/codes/kotlin/chapter_sorting/bubble_sort.kt b/codes/kotlin/chapter_sorting/bubble_sort.kt index 4fb482550..12608d160 100644 --- a/codes/kotlin/chapter_sorting/bubble_sort.kt +++ b/codes/kotlin/chapter_sorting/bubble_sort.kt @@ -14,7 +14,7 @@ fun bubbleSort(nums: IntArray) { for (j in 0.. nums[j + 1]) { // 交换 nums[j] 与 nums[j + 1] - nums[j] = nums[j+1].also { nums[j+1] = nums[j] } + nums[j] = nums[j + 1].also { nums[j + 1] = nums[j] } } } } diff --git a/codes/kotlin/chapter_sorting/bucket_sort.kt b/codes/kotlin/chapter_sorting/bucket_sort.kt index e4b1d9ff2..ccfcffc75 100644 --- a/codes/kotlin/chapter_sorting/bucket_sort.kt +++ b/codes/kotlin/chapter_sorting/bucket_sort.kt @@ -6,15 +6,13 @@ package chapter_sorting -import kotlin.collections.ArrayList - /* 桶排序 */ fun bucketSort(nums: FloatArray) { // 初始化 k = n/2 个桶,预期向每个桶分配 2 个元素 val k = nums.size / 2 - val buckets = ArrayList>() + val buckets = mutableListOf>() for (i in 0.. nums[ma]) ma = l - if (r < n && nums[r] > nums[ma]) ma = r + if (l < n && nums[l] > nums[ma]) + ma = l + if (r < n && nums[r] > nums[ma]) + ma = r // 若节点 i 最大或索引 l, r 越界,则无须继续堆化,跳出 - if (ma == i) break + if (ma == i) + break // 交换两节点 nums[i] = nums[ma].also { nums[ma] = nums[i] } // 循环向下堆化 @@ -45,4 +48,4 @@ fun main() { val nums = intArrayOf(4, 1, 3, 1, 5, 2) heapSort(nums) println("堆排序完成后 nums = ${nums.contentToString()}") -} \ No newline at end of file +} diff --git a/codes/kotlin/chapter_sorting/insertion_sort.kt b/codes/kotlin/chapter_sorting/insertion_sort.kt index c16947307..015127d97 100644 --- a/codes/kotlin/chapter_sorting/insertion_sort.kt +++ b/codes/kotlin/chapter_sorting/insertion_sort.kt @@ -12,7 +12,7 @@ fun insertionSort(nums: IntArray) { for (i in nums.indices) { val base = nums[i] var j = i - 1 - // 内循环: 将 base 插入到已排序部分的正确位置 + // 内循环:将 base 插入到已排序区间 [0, i-1] 中的正确位置 while (j >= 0 && nums[j] > base) { nums[j + 1] = nums[j] // 将 nums[j] 向右移动一位 j-- @@ -26,4 +26,4 @@ fun main() { val nums = intArrayOf(4, 1, 3, 1, 5, 2) insertionSort(nums) println("插入排序完成后 nums = ${nums.contentToString()}") -} \ No newline at end of file +} diff --git a/codes/kotlin/chapter_sorting/merge_sort.kt b/codes/kotlin/chapter_sorting/merge_sort.kt index 291ddde1f..113875422 100644 --- a/codes/kotlin/chapter_sorting/merge_sort.kt +++ b/codes/kotlin/chapter_sorting/merge_sort.kt @@ -17,8 +17,10 @@ fun merge(nums: IntArray, left: Int, mid: Int, right: Int) { var k = 0 // 当左右子数组都还有元素时,进行比较并将较小的元素复制到临时数组中 while (i <= mid && j <= right) { - if (nums[i] <= nums[j]) tmp[k++] = nums[i++] - else tmp[k++] = nums[j++] + if (nums[i] <= nums[j]) + tmp[k++] = nums[i++] + else + tmp[k++] = nums[j++] } // 将左子数组和右子数组的剩余元素复制到临时数组中 while (i <= mid) { @@ -51,4 +53,4 @@ fun main() { val nums = intArrayOf(7, 3, 2, 6, 0, 1, 5, 4) mergeSort(nums, 0, nums.size - 1) println("归并排序完成后 nums = ${nums.contentToString()}") -} \ No newline at end of file +} diff --git a/codes/kotlin/chapter_sorting/radix_sort.kt b/codes/kotlin/chapter_sorting/radix_sort.kt index 16aa13c98..1fefdccf1 100644 --- a/codes/kotlin/chapter_sorting/radix_sort.kt +++ b/codes/kotlin/chapter_sorting/radix_sort.kt @@ -20,7 +20,7 @@ fun countingSortDigit(nums: IntArray, exp: Int) { // 统计 0~9 各数字的出现次数 for (i in 0..() + private val stack = mutableListOf() /* 获取栈的长度 */ fun size(): Int { @@ -40,7 +40,7 @@ class ArrayStack { /* 将 List 转化为 Array 并返回 */ fun toArray(): Array { - return stack.toArray() + return stack.toTypedArray() } } @@ -63,7 +63,7 @@ fun main() { /* 元素出栈 */ val pop = stack.pop() - println("出栈元素 pop = ${pop},出栈后 stack = ${stack.toArray().contentToString()}") + println("出栈元素 pop = $pop,出栈后 stack = ${stack.toArray().contentToString()}") /* 获取栈的长度 */ val size = stack.size() diff --git a/codes/kotlin/chapter_stack_and_queue/linkedlist_deque.kt b/codes/kotlin/chapter_stack_and_queue/linkedlist_deque.kt index cc2a5c232..0f2789957 100644 --- a/codes/kotlin/chapter_stack_and_queue/linkedlist_deque.kt +++ b/codes/kotlin/chapter_stack_and_queue/linkedlist_deque.kt @@ -7,7 +7,7 @@ package chapter_stack_and_queue /* 双向链表节点 */ -class ListNode(var value: Int) { +class ListNode(var _val: Int) { // 节点值 var next: ListNode? = null // 后继节点引用 var prev: ListNode? = null // 前驱节点引用 @@ -15,9 +15,9 @@ class ListNode(var value: Int) { /* 基于双向链表实现的双向队列 */ class LinkedListDeque { - private var front: ListNode? = null // 头节点 front ,尾节点 rear - private var rear: ListNode? = null - private var queSize = 0 // 双向队列的长度 + private var front: ListNode? = null // 头节点 front + private var rear: ListNode? = null // 尾节点 rear + private var queSize: Int = 0 // 双向队列的长度 /* 获取双向队列的长度 */ fun size(): Int { @@ -64,12 +64,12 @@ class LinkedListDeque { /* 出队操作 */ fun pop(isFront: Boolean): Int { - if (isEmpty()) throw IndexOutOfBoundsException() - + if (isEmpty()) + throw IndexOutOfBoundsException() val value: Int // 队首出队操作 if (isFront) { - value = front!!.value // 暂存头节点值 + value = front!!._val // 暂存头节点值 // 删除头节点 val fNext = front!!.next if (fNext != null) { @@ -79,7 +79,7 @@ class LinkedListDeque { front = fNext // 更新头节点 // 队尾出队操作 } else { - value = rear!!.value // 暂存尾节点值 + value = rear!!._val // 暂存尾节点值 // 删除尾节点 val rPrev = rear!!.prev if (rPrev != null) { @@ -104,17 +104,14 @@ class LinkedListDeque { /* 访问队首元素 */ fun peekFirst(): Int { - if (isEmpty()) { - throw IndexOutOfBoundsException() - - } - return front!!.value + if (isEmpty()) throw IndexOutOfBoundsException() + return front!!._val } /* 访问队尾元素 */ fun peekLast(): Int { if (isEmpty()) throw IndexOutOfBoundsException() - return rear!!.value + return rear!!._val } /* 返回数组用于打印 */ @@ -122,7 +119,7 @@ class LinkedListDeque { var node = front val res = IntArray(size()) for (i in res.indices) { - res[i] = node!!.value + res[i] = node!!._val node = node.next } return res @@ -163,4 +160,4 @@ fun main() { /* 判断双向队列是否为空 */ val isEmpty = deque.isEmpty() println("双向队列是否为空 = $isEmpty") -} \ No newline at end of file +} diff --git a/codes/kotlin/chapter_stack_and_queue/linkedlist_queue.kt b/codes/kotlin/chapter_stack_and_queue/linkedlist_queue.kt index 33f48d560..64720de8a 100644 --- a/codes/kotlin/chapter_stack_and_queue/linkedlist_queue.kt +++ b/codes/kotlin/chapter_stack_and_queue/linkedlist_queue.kt @@ -52,7 +52,7 @@ class LinkedListQueue( /* 访问队首元素 */ fun peek(): Int { if (isEmpty()) throw IndexOutOfBoundsException() - return front!!.value + return front!!._val } /* 将链表转化为 Array 并返回 */ @@ -60,7 +60,7 @@ class LinkedListQueue( var node = front val res = IntArray(size()) for (i in res.indices) { - res[i] = node!!.value + res[i] = node!!._val node = node.next } return res diff --git a/codes/kotlin/chapter_stack_and_queue/linkedlist_stack.kt b/codes/kotlin/chapter_stack_and_queue/linkedlist_stack.kt index f51b0b9eb..b63f3639a 100644 --- a/codes/kotlin/chapter_stack_and_queue/linkedlist_stack.kt +++ b/codes/kotlin/chapter_stack_and_queue/linkedlist_stack.kt @@ -41,7 +41,7 @@ class LinkedListStack( /* 访问栈顶元素 */ fun peek(): Int? { if (isEmpty()) throw IndexOutOfBoundsException() - return stackPeek?.value + return stackPeek?._val } /* 将 List 转化为 Array 并返回 */ @@ -49,7 +49,7 @@ class LinkedListStack( var node = stackPeek val res = IntArray(size()) for (i in res.size - 1 downTo 0) { - res[i] = node?.value!! + res[i] = node?._val!! node = node.next } return res diff --git a/codes/kotlin/chapter_tree/array_binary_tree.kt b/codes/kotlin/chapter_tree/array_binary_tree.kt index 46c09cef0..f40c30758 100644 --- a/codes/kotlin/chapter_tree/array_binary_tree.kt +++ b/codes/kotlin/chapter_tree/array_binary_tree.kt @@ -10,7 +10,8 @@ import utils.TreeNode import utils.printTree /* 数组表示下的二叉树类 */ -class ArrayBinaryTree(private val tree: List) { +/* 构造方法 */ +class ArrayBinaryTree(private val tree: MutableList) { /* 列表容量 */ fun size(): Int { return tree.size @@ -39,11 +40,12 @@ class ArrayBinaryTree(private val tree: List) { } /* 层序遍历 */ - fun levelOrder(): List { - val res = ArrayList() + fun levelOrder(): MutableList { + val res = mutableListOf() // 直接遍历数组 for (i in 0..) { /* 深度优先遍历 */ fun dfs(i: Int, order: String, res: MutableList) { // 若为空位,则返回 - if (value(i) == null) return + if (value(i) == null) + return // 前序遍历 - if ("pre" == order) res.add(value(i)) + if ("pre" == order) + res.add(value(i)) dfs(left(i), order, res) // 中序遍历 - if ("in" == order) res.add(value(i)) + if ("in" == order) + res.add(value(i)) dfs(right(i), order, res) // 后序遍历 - if ("post" == order) res.add(value(i)) + if ("post" == order) + res.add(value(i)) } /* 前序遍历 */ - fun preOrder(): List { - val res = ArrayList() + fun preOrder(): MutableList { + val res = mutableListOf() dfs(0, "pre", res) return res } /* 中序遍历 */ - fun inOrder(): List { - val res = ArrayList() + fun inOrder(): MutableList { + val res = mutableListOf() dfs(0, "in", res) return res } /* 后序遍历 */ - fun postOrder(): List { - val res = ArrayList() + fun postOrder(): MutableList { + val res = mutableListOf() dfs(0, "post", res) return res } diff --git a/codes/kotlin/chapter_tree/avl_tree.kt b/codes/kotlin/chapter_tree/avl_tree.kt index 8f2d18b24..634b75081 100644 --- a/codes/kotlin/chapter_tree/avl_tree.kt +++ b/codes/kotlin/chapter_tree/avl_tree.kt @@ -23,7 +23,7 @@ class AVLTree { /* 更新节点高度 */ private fun updateHeight(node: TreeNode?) { // 节点高度等于最高子树高度 + 1 - node?.height = (max(height(node?.left).toDouble(), height(node?.right).toDouble()) + 1).toInt() + node?.height = max(height(node?.left), height(node?.right)) + 1 } /* 获取平衡因子 */ @@ -103,10 +103,12 @@ class AVLTree { return TreeNode(value) 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) - else return node // 重复节点不插入,直接返回 - + if (value < node.value) + node.left = insertHelper(node.left, value) + else if (value > node.value) + node.right = insertHelper(node.right, value) + else + return node // 重复节点不插入,直接返回 updateHeight(node) // 更新节点高度 /* 2. 执行旋转操作,使该子树重新恢复平衡 */ node = rotate(node) @@ -123,14 +125,22 @@ class AVLTree { private fun removeHelper(n: TreeNode?, value: 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 (value < node.value) + node.left = removeHelper(node.left, value) + else if (value > node.value) + node.right = removeHelper(node.right, value) else { if (node.left == null || node.right == null) { - val child = if (node.left != null) node.left else node.right + val child = if (node.left != null) + node.left + else + node.right // 子节点数量 = 0 ,直接删除 node 并返回 - if (child == null) return null - else node = child + if (child == null) + return null + // 子节点数量 = 1 ,直接删除 node + else + node = child } else { // 子节点数量 = 2 ,则将中序遍历的下个节点删除,并用该节点替换当前节点 var temp = node.right @@ -154,9 +164,14 @@ class AVLTree { // 循环查找,越过叶节点后跳出 while (cur != null) { // 目标节点在 cur 的右子树中 - cur = if (cur.value < value) cur.right!! - else (if (cur.value > value) cur.left - else break)!! + cur = if (cur.value < value) + cur.right!! + // 目标节点在 cur 的左子树中 + else if (cur.value > value) + cur.left + // 找到目标节点,跳出循环 + else + break } // 返回目标节点 return cur diff --git a/codes/kotlin/chapter_tree/binary_search_tree.kt b/codes/kotlin/chapter_tree/binary_search_tree.kt index 0d4513e0c..5dd01fe69 100644 --- a/codes/kotlin/chapter_tree/binary_search_tree.kt +++ b/codes/kotlin/chapter_tree/binary_search_tree.kt @@ -11,6 +11,7 @@ import utils.printTree /* 二叉搜索树 */ class BinarySearchTree { + // 初始化空树 private var root: TreeNode? = null /* 获取二叉树根节点 */ @@ -24,11 +25,14 @@ class BinarySearchTree { // 循环查找,越过叶节点后跳出 while (cur != null) { // 目标节点在 cur 的右子树中 - cur = if (cur.value < num) cur.right + cur = if (cur.value < num) + cur.right // 目标节点在 cur 的左子树中 - else if (cur.value > num) cur.left + else if (cur.value > num) + cur.left // 找到目标节点,跳出循环 - else break + else + break } // 返回目标节点 return cur @@ -46,45 +50,60 @@ class BinarySearchTree { // 循环查找,越过叶节点后跳出 while (cur != null) { // 找到重复节点,直接返回 - if (cur.value == num) return + if (cur.value == num) + return pre = cur // 插入位置在 cur 的右子树中 - cur = if (cur.value < num) cur.right + cur = if (cur.value < num) + cur.right // 插入位置在 cur 的左子树中 - else cur.left + else + cur.left } // 插入节点 val node = TreeNode(num) - if (pre?.value!! < num) pre.right = node - else pre.left = node + if (pre?.value!! < num) + pre.right = node + else + pre.left = node } /* 删除节点 */ fun remove(num: Int) { // 若树为空,直接提前返回 - if (root == null) return + if (root == null) + return var cur = root var pre: TreeNode? = null // 循环查找,越过叶节点后跳出 while (cur != null) { // 找到待删除节点,跳出循环 - if (cur.value == num) break + if (cur.value == num) + break pre = cur // 待删除节点在 cur 的右子树中 - cur = if (cur.value < num) cur.right + cur = if (cur.value < num) + cur.right // 待删除节点在 cur 的左子树中 - else cur.left + else + cur.left } // 若无待删除节点,则直接返回 - if (cur == null) return + if (cur == null) + return // 子节点数量 = 0 or 1 if (cur.left == null || cur.right == null) { // 当子节点数量 = 0 / 1 时, child = null / 该子节点 - val child = if (cur.left != null) cur.left else cur.right + val child = if (cur.left != null) + cur.left + else + cur.right // 删除节点 cur if (cur != root) { - if (pre!!.left == cur) pre.left = child - else pre.right = child + if (pre!!.left == cur) + pre.left = child + else + pre.right = child } else { // 若删除节点为根节点,则重新指定根节点 root = child diff --git a/codes/kotlin/chapter_tree/binary_tree_bfs.kt b/codes/kotlin/chapter_tree/binary_tree_bfs.kt index 3bd6b850d..9316e5186 100644 --- a/codes/kotlin/chapter_tree/binary_tree_bfs.kt +++ b/codes/kotlin/chapter_tree/binary_tree_bfs.kt @@ -16,13 +16,14 @@ fun levelOrder(root: TreeNode?): MutableList { val queue = LinkedList() queue.add(root) // 初始化一个列表,用于保存遍历序列 - val list = ArrayList() - while (!queue.isEmpty()) { - val node = queue.poll() // 队列出队 - list.add(node?.value!!) // 保存节点值 - if (node.left != null) queue.offer(node.left) // 左子节点入队 - - if (node.right != null) queue.offer(node.right) // 右子节点入队 + val list = mutableListOf() + while (queue.isNotEmpty()) { + val node = queue.poll() // 队列出队 + list.add(node?.value!!) // 保存节点值 + if (node.left != null) + queue.offer(node.left) // 左子节点入队 + if (node.right != null) + queue.offer(node.right) // 右子节点入队 } return list } @@ -38,4 +39,4 @@ fun main() { /* 层序遍历 */ val list = levelOrder(root) println("\n层序遍历的节点打印序列 = $list") -} \ No newline at end of file +} diff --git a/codes/kotlin/chapter_tree/binary_tree_dfs.kt b/codes/kotlin/chapter_tree/binary_tree_dfs.kt index cefae4e27..aa879c435 100644 --- a/codes/kotlin/chapter_tree/binary_tree_dfs.kt +++ b/codes/kotlin/chapter_tree/binary_tree_dfs.kt @@ -10,7 +10,7 @@ import utils.TreeNode import utils.printTree // 初始化列表,用于存储遍历序列 -var list = ArrayList() +var list = mutableListOf() /* 前序遍历 */ fun preOrder(root: TreeNode?) { diff --git a/codes/kotlin/utils/PrintUtil.kt b/codes/kotlin/utils/PrintUtil.kt index f81984e27..9954a4621 100644 --- a/codes/kotlin/utils/PrintUtil.kt +++ b/codes/kotlin/utils/PrintUtil.kt @@ -20,7 +20,7 @@ fun printMatrix(matrix: Array>) { } /* 打印矩阵(List) */ -fun printMatrix(matrix: List>) { +fun printMatrix(matrix: MutableList>) { println("[") for (row in matrix) { println(" $row,") @@ -31,7 +31,7 @@ fun printMatrix(matrix: List>) { /* 打印链表 */ fun printLinkedList(h: ListNode?) { var head = h - val list = ArrayList() + val list = mutableListOf() while (head != null) { list.add(head.value.toString()) head = head.next @@ -91,16 +91,17 @@ fun showTrunks(p: Trunk?) { /* 打印哈希表 */ fun printHashMap(map: Map) { for ((key, value) in map) { - println(key.toString() + " -> " + value) + println("${key.toString()} -> $value") } } /* 打印堆 */ fun printHeap(queue: Queue?) { - val list = queue?.let { ArrayList(it) } + val list = mutableListOf() + queue?.let { list.addAll(it) } print("堆的数组表示:") println(list) println("堆的树状表示:") - val root = list?.let { TreeNode.listToTree(it) } + val root = TreeNode.listToTree(list) printTree(root) } diff --git a/codes/kotlin/utils/TreeNode.kt b/codes/kotlin/utils/TreeNode.kt index a9b6e1777..e3a21c241 100644 --- a/codes/kotlin/utils/TreeNode.kt +++ b/codes/kotlin/utils/TreeNode.kt @@ -7,6 +7,7 @@ package utils /* 二叉树节点类 */ +/* 构造方法 */ class TreeNode( var value: Int // 节点值 ) { @@ -59,8 +60,8 @@ class TreeNode( } /* 将二叉树序列化为列表 */ - fun treeToList(root: TreeNode?): List { - val res = ArrayList() + fun treeToList(root: TreeNode?): MutableList { + val res = mutableListOf() treeToListDFS(root, 0, res) return res } diff --git a/codes/kotlin/utils/Vertex.kt b/codes/kotlin/utils/Vertex.kt index 2abe51fb2..fb1f9de9e 100644 --- a/codes/kotlin/utils/Vertex.kt +++ b/codes/kotlin/utils/Vertex.kt @@ -19,8 +19,8 @@ class Vertex(val value: Int) { } /* 输入顶点列表 vets ,返回值列表 vals */ - fun vetsToVals(vets: List): List { - val vals = ArrayList() + fun vetsToVals(vets: MutableList): MutableList { + val vals = mutableListOf() for (vet in vets) { vals.add(vet!!.value) }