From cfe8281aee4040ea7772e128ed2c766ebdacff95 Mon Sep 17 00:00:00 2001 From: curtishd <131777542+curtishd@users.noreply.github.com> Date: Sat, 30 Mar 2024 13:16:39 +0800 Subject: [PATCH] Add kotlin code for chapter_stack_and_queue and chapter_tree (#1197) * Add kotlin code block for chapter_hashing * Add kotlin code block for chapter_heap. * Add kotlin code block for chapter_stack_and_queue and chapter_tree * fix indentation * Update binary_tree.md --------- Co-authored-by: Yudong Jin --- docs/chapter_stack_and_queue/deque.md | 24 +++++++++++++++++- docs/chapter_stack_and_queue/queue.md | 22 +++++++++++++++- docs/chapter_stack_and_queue/stack.md | 22 +++++++++++++++- .../array_representation_of_tree.md | 4 ++- docs/chapter_tree/avl_tree.md | 7 +++++- docs/chapter_tree/binary_tree.md | 25 ++++++++++++++++--- 6 files changed, 96 insertions(+), 8 deletions(-) diff --git a/docs/chapter_stack_and_queue/deque.md b/docs/chapter_stack_and_queue/deque.md index 34e46a124..f9d35a799 100644 --- a/docs/chapter_stack_and_queue/deque.md +++ b/docs/chapter_stack_and_queue/deque.md @@ -327,7 +327,29 @@ === "Kotlin" ```kotlin title="deque.kt" - + /* 初始化双向队列 */ + val deque = LinkedList() + + /* 元素入队 */ + deque.offerLast(2) // 添加至队尾 + deque.offerLast(5) + deque.offerLast(4) + deque.offerFirst(3) // 添加至队首 + deque.offerFirst(1) + + /* 访问元素 */ + val peekFirst = deque.peekFirst() // 队首元素 + val peekLast = deque.peekLast() // 队尾元素 + + /* 元素出队 */ + val popFirst = deque.pollFirst() // 队首元素出队 + val popLast = deque.pollLast() // 队尾元素出队 + + /* 获取双向队列的长度 */ + val size = deque.size + + /* 判断双向队列是否为空 */ + val isEmpty = deque.isEmpty() ``` === "Zig" diff --git a/docs/chapter_stack_and_queue/queue.md b/docs/chapter_stack_and_queue/queue.md index 2a4413e65..cf4aa5c01 100755 --- a/docs/chapter_stack_and_queue/queue.md +++ b/docs/chapter_stack_and_queue/queue.md @@ -305,7 +305,27 @@ === "Kotlin" ```kotlin title="queue.kt" - + /* 初始化队列 */ + val queue = LinkedList() + + /* 元素入队 */ + queue.offer(1) + queue.offer(3) + queue.offer(2) + queue.offer(5) + queue.offer(4) + + /* 访问队首元素 */ + val peek = queue.peek() + + /* 元素出队 */ + val pop = queue.poll() + + /* 获取队列的长度 */ + val size = queue.size + + /* 判断队列是否为空 */ + val isEmpty = queue.isEmpty() ``` === "Zig" diff --git a/docs/chapter_stack_and_queue/stack.md b/docs/chapter_stack_and_queue/stack.md index 1a50b1fda..739158ceb 100755 --- a/docs/chapter_stack_and_queue/stack.md +++ b/docs/chapter_stack_and_queue/stack.md @@ -299,7 +299,27 @@ === "Kotlin" ```kotlin title="stack.kt" - + /* 初始化栈 */ + val stack = Stack() + + /* 元素入栈 */ + stack.push(1) + stack.push(3) + stack.push(2) + stack.push(5) + stack.push(4) + + /* 访问栈顶元素 */ + val peek = stack.peek() + + /* 元素出栈 */ + val pop = stack.pop() + + /* 获取栈的长度 */ + val size = stack.size + + /* 判断是否为空 */ + val isEmpty = stack.isEmpty() ``` === "Zig" diff --git a/docs/chapter_tree/array_representation_of_tree.md b/docs/chapter_tree/array_representation_of_tree.md index d796967b2..6da64cb40 100644 --- a/docs/chapter_tree/array_representation_of_tree.md +++ b/docs/chapter_tree/array_representation_of_tree.md @@ -115,7 +115,9 @@ === "Kotlin" ```kotlin title="" - + /* 二叉树的数组表示 */ + // 使用 null 来表示空位 + val tree = mutableListOf( 1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15 ) ``` === "Zig" diff --git a/docs/chapter_tree/avl_tree.md b/docs/chapter_tree/avl_tree.md index 8d03fdd06..6c2ada4f7 100644 --- a/docs/chapter_tree/avl_tree.md +++ b/docs/chapter_tree/avl_tree.md @@ -203,7 +203,12 @@ AVL 树既是二叉搜索树,也是平衡二叉树,同时满足这两类二 === "Kotlin" ```kotlin title="" - + /* AVL 树节点类 */ + class TreeNode(val _val: Int) { // 节点值 + val height: Int = 0 // 节点高度 + val left: TreeNode? = null // 左子节点 + val right: TreeNode? = null // 右子节点 + } ``` === "Zig" diff --git a/docs/chapter_tree/binary_tree.md b/docs/chapter_tree/binary_tree.md index 44bc8c872..40a3b3eec 100644 --- a/docs/chapter_tree/binary_tree.md +++ b/docs/chapter_tree/binary_tree.md @@ -179,7 +179,11 @@ === "Kotlin" ```kotlin title="" - + /* 二叉树节点类 */ + class TreeNode(val _val: Int) { // 节点值 + val left: TreeNode? = null // 左子节点引用 + val right: TreeNode? = null // 右子节点引用 + } ``` === "Zig" @@ -406,7 +410,17 @@ === "Kotlin" ```kotlin title="binary_tree.kt" - + // 初始化节点 + val n1 = TreeNode(1) + val n2 = TreeNode(2) + val n3 = TreeNode(3) + val n4 = TreeNode(4) + val n5 = TreeNode(5) + // 构建节点之间的引用(指针) + n1.left = n2 + n1.right = n3 + n2.left = n4 + n2.right = n5 ``` === "Zig" @@ -557,7 +571,12 @@ === "Kotlin" ```kotlin title="binary_tree.kt" - + val P = TreeNode(0) + // 在 n1 -> n2 中间插入节点 P + n1.left = P + P.left = n2 + // 删除节点 P + n1.left = n2 ``` === "Zig"