From 40e6d2b415552edbac2cfb5680bdea09fc93be5e Mon Sep 17 00:00:00 2001 From: krahets Date: Mon, 24 Apr 2023 04:20:51 +0800 Subject: [PATCH] Rename the common modules. --- .gitignore | 2 +- .../linked_list.cs | 2 +- .../preorder_traversal_i_compact.cs | 2 +- .../preorder_traversal_ii_compact.cs | 2 +- .../preorder_traversal_iii_compact.cs | 2 +- .../preorder_traversal_iii_template.cs | 2 +- .../space_complexity.cs | 2 +- .../chapter_graph/graph_adjacency_list.cs | 2 +- .../chapter_graph/graph_adjacency_matrix.cs | 2 +- codes/csharp/chapter_graph/graph_bfs.cs | 2 +- codes/csharp/chapter_graph/graph_dfs.cs | 2 +- codes/csharp/chapter_hashing/hash_map.cs | 2 +- codes/csharp/chapter_heap/heap.cs | 2 +- codes/csharp/chapter_heap/my_heap.cs | 2 +- .../chapter_searching/hashing_search.cs | 2 +- .../csharp/chapter_searching/linear_search.cs | 2 +- .../linkedlist_queue.cs | 2 +- .../linkedlist_stack.cs | 2 +- codes/csharp/chapter_tree/avl_tree.cs | 2 +- .../csharp/chapter_tree/binary_search_tree.cs | 2 +- codes/csharp/chapter_tree/binary_tree.cs | 2 +- codes/csharp/chapter_tree/binary_tree_bfs.cs | 2 +- codes/csharp/chapter_tree/binary_tree_dfs.cs | 2 +- codes/csharp/{include => utils}/ListNode.cs | 2 +- codes/csharp/{include => utils}/PrintUtil.cs | 2 +- codes/csharp/{include => utils}/TreeNode.cs | 2 +- codes/csharp/{include => utils}/Vertex.cs | 2 +- codes/java/utils/ListNode.java | 36 ++++++ codes/java/utils/PrintUtil.java | 118 ++++++++++++++++++ codes/java/utils/TreeNode.java | 67 ++++++++++ codes/java/utils/Vertex.java | 36 ++++++ utils | 1 + 32 files changed, 285 insertions(+), 27 deletions(-) rename codes/csharp/{include => utils}/ListNode.cs (97%) rename codes/csharp/{include => utils}/PrintUtil.cs (99%) rename codes/csharp/{include => utils}/TreeNode.cs (98%) rename codes/csharp/{include => utils}/Vertex.cs (96%) create mode 100755 codes/java/utils/ListNode.java create mode 100755 codes/java/utils/PrintUtil.java create mode 100644 codes/java/utils/TreeNode.java create mode 100644 codes/java/utils/Vertex.java create mode 160000 utils diff --git a/.gitignore b/.gitignore index 5839793c3..69fd781b9 100644 --- a/.gitignore +++ b/.gitignore @@ -12,7 +12,7 @@ docs/overrides/ # build build/ site/ -utils/ +./utils/ # test script test.sh diff --git a/codes/csharp/chapter_array_and_linkedlist/linked_list.cs b/codes/csharp/chapter_array_and_linkedlist/linked_list.cs index 8ac986355..fdaf5ea1b 100644 --- a/codes/csharp/chapter_array_and_linkedlist/linked_list.cs +++ b/codes/csharp/chapter_array_and_linkedlist/linked_list.cs @@ -2,7 +2,7 @@ // Created Time: 2022-12-16 // Author: mingXta (1195669834@qq.com) -using hello_algo.include; +using hello_algo.utils; using NUnit.Framework; namespace hello_algo.chapter_array_and_linkedlist; diff --git a/codes/csharp/chapter_backtracking/preorder_traversal_i_compact.cs b/codes/csharp/chapter_backtracking/preorder_traversal_i_compact.cs index 096b6bd6f..6c35072a9 100644 --- a/codes/csharp/chapter_backtracking/preorder_traversal_i_compact.cs +++ b/codes/csharp/chapter_backtracking/preorder_traversal_i_compact.cs @@ -4,7 +4,7 @@ * Author: hpstory (hpstory1024@163.com) */ -using hello_algo.include; +using hello_algo.utils; using NUnit.Framework; using System.IO; diff --git a/codes/csharp/chapter_backtracking/preorder_traversal_ii_compact.cs b/codes/csharp/chapter_backtracking/preorder_traversal_ii_compact.cs index 647cd3781..0512a1442 100644 --- a/codes/csharp/chapter_backtracking/preorder_traversal_ii_compact.cs +++ b/codes/csharp/chapter_backtracking/preorder_traversal_ii_compact.cs @@ -4,7 +4,7 @@ * Author: hpstory (hpstory1024@163.com) */ -using hello_algo.include; +using hello_algo.utils; using NUnit.Framework; namespace hello_algo.chapter_backtracking; diff --git a/codes/csharp/chapter_backtracking/preorder_traversal_iii_compact.cs b/codes/csharp/chapter_backtracking/preorder_traversal_iii_compact.cs index af202ffd3..606dd97a7 100644 --- a/codes/csharp/chapter_backtracking/preorder_traversal_iii_compact.cs +++ b/codes/csharp/chapter_backtracking/preorder_traversal_iii_compact.cs @@ -4,7 +4,7 @@ * Author: hpstory (hpstory1024@163.com) */ -using hello_algo.include; +using hello_algo.utils; using NUnit.Framework; namespace hello_algo.chapter_backtracking; diff --git a/codes/csharp/chapter_backtracking/preorder_traversal_iii_template.cs b/codes/csharp/chapter_backtracking/preorder_traversal_iii_template.cs index 74da92a83..92a44c80e 100644 --- a/codes/csharp/chapter_backtracking/preorder_traversal_iii_template.cs +++ b/codes/csharp/chapter_backtracking/preorder_traversal_iii_template.cs @@ -4,7 +4,7 @@ * Author: hpstory (hpstory1024@163.com) */ -using hello_algo.include; +using hello_algo.utils; using NUnit.Framework; namespace hello_algo.chapter_backtracking; diff --git a/codes/csharp/chapter_computational_complexity/space_complexity.cs b/codes/csharp/chapter_computational_complexity/space_complexity.cs index 49d272c89..976b3d288 100644 --- a/codes/csharp/chapter_computational_complexity/space_complexity.cs +++ b/codes/csharp/chapter_computational_complexity/space_complexity.cs @@ -4,7 +4,7 @@ * Author: haptear (haptear@hotmail.com) */ -using hello_algo.include; +using hello_algo.utils; using NUnit.Framework; namespace hello_algo.chapter_computational_complexity; diff --git a/codes/csharp/chapter_graph/graph_adjacency_list.cs b/codes/csharp/chapter_graph/graph_adjacency_list.cs index 1a5a4b2f1..44bec3b90 100644 --- a/codes/csharp/chapter_graph/graph_adjacency_list.cs +++ b/codes/csharp/chapter_graph/graph_adjacency_list.cs @@ -4,7 +4,7 @@ * Author: zjkung1123 (zjkung1123@gmail.com) */ -using hello_algo.include; +using hello_algo.utils; using NUnit.Framework; namespace hello_algo.chapter_graph; diff --git a/codes/csharp/chapter_graph/graph_adjacency_matrix.cs b/codes/csharp/chapter_graph/graph_adjacency_matrix.cs index 50fe3d456..7ccee0948 100644 --- a/codes/csharp/chapter_graph/graph_adjacency_matrix.cs +++ b/codes/csharp/chapter_graph/graph_adjacency_matrix.cs @@ -4,7 +4,7 @@ * Author: zjkung1123 (zjkung1123@gmail.com) */ -using hello_algo.include; +using hello_algo.utils; using NUnit.Framework; namespace hello_algo.chapter_graph; diff --git a/codes/csharp/chapter_graph/graph_bfs.cs b/codes/csharp/chapter_graph/graph_bfs.cs index 9d11d95bc..286c13f9f 100644 --- a/codes/csharp/chapter_graph/graph_bfs.cs +++ b/codes/csharp/chapter_graph/graph_bfs.cs @@ -4,7 +4,7 @@ * Author: hpstory (hpstory1024@163.com) */ -using hello_algo.include; +using hello_algo.utils; using NUnit.Framework; namespace hello_algo.chapter_graph; diff --git a/codes/csharp/chapter_graph/graph_dfs.cs b/codes/csharp/chapter_graph/graph_dfs.cs index 63eba6555..f1c3d9d26 100644 --- a/codes/csharp/chapter_graph/graph_dfs.cs +++ b/codes/csharp/chapter_graph/graph_dfs.cs @@ -4,7 +4,7 @@ * Author: hpstory (hpstory1024@163.com) */ -using hello_algo.include; +using hello_algo.utils; using NUnit.Framework; namespace hello_algo.chapter_graph; diff --git a/codes/csharp/chapter_hashing/hash_map.cs b/codes/csharp/chapter_hashing/hash_map.cs index eef618bd5..6837deb0d 100644 --- a/codes/csharp/chapter_hashing/hash_map.cs +++ b/codes/csharp/chapter_hashing/hash_map.cs @@ -5,7 +5,7 @@ * Author: haptear (haptear@hotmail.com) */ -using hello_algo.include; +using hello_algo.utils; using NUnit.Framework; namespace hello_algo.chapter_hashing; diff --git a/codes/csharp/chapter_heap/heap.cs b/codes/csharp/chapter_heap/heap.cs index b21e10387..f005ff478 100644 --- a/codes/csharp/chapter_heap/heap.cs +++ b/codes/csharp/chapter_heap/heap.cs @@ -4,7 +4,7 @@ * Author: zjkung1123 (zjkung1123@gmail.com) */ -using hello_algo.include; +using hello_algo.utils; using NUnit.Framework; namespace hello_algo.chapter_heap; diff --git a/codes/csharp/chapter_heap/my_heap.cs b/codes/csharp/chapter_heap/my_heap.cs index 25690d0eb..0c5421ad3 100644 --- a/codes/csharp/chapter_heap/my_heap.cs +++ b/codes/csharp/chapter_heap/my_heap.cs @@ -4,7 +4,7 @@ * Author: zjkung1123 (zjkung1123@gmail.com) */ -using hello_algo.include; +using hello_algo.utils; using NUnit.Framework; namespace hello_algo.chapter_heap; diff --git a/codes/csharp/chapter_searching/hashing_search.cs b/codes/csharp/chapter_searching/hashing_search.cs index 3e4712373..4f15b4eba 100644 --- a/codes/csharp/chapter_searching/hashing_search.cs +++ b/codes/csharp/chapter_searching/hashing_search.cs @@ -4,7 +4,7 @@ * Author: haptear (haptear@hotmail.com) */ -using hello_algo.include; +using hello_algo.utils; using NUnit.Framework; namespace hello_algo.chapter_searching; diff --git a/codes/csharp/chapter_searching/linear_search.cs b/codes/csharp/chapter_searching/linear_search.cs index bd7f3eac9..b30b38292 100644 --- a/codes/csharp/chapter_searching/linear_search.cs +++ b/codes/csharp/chapter_searching/linear_search.cs @@ -4,7 +4,7 @@ * Author: haptear (haptear@hotmail.com) */ -using hello_algo.include; +using hello_algo.utils; using NUnit.Framework; namespace hello_algo.chapter_searching; diff --git a/codes/csharp/chapter_stack_and_queue/linkedlist_queue.cs b/codes/csharp/chapter_stack_and_queue/linkedlist_queue.cs index d2a6c70a9..12c8f80b5 100644 --- a/codes/csharp/chapter_stack_and_queue/linkedlist_queue.cs +++ b/codes/csharp/chapter_stack_and_queue/linkedlist_queue.cs @@ -4,7 +4,7 @@ * Author: haptear (haptear@hotmail.com) */ -using hello_algo.include; +using hello_algo.utils; using NUnit.Framework; namespace hello_algo.chapter_stack_and_queue; diff --git a/codes/csharp/chapter_stack_and_queue/linkedlist_stack.cs b/codes/csharp/chapter_stack_and_queue/linkedlist_stack.cs index 4f5341815..f4dfc2e00 100644 --- a/codes/csharp/chapter_stack_and_queue/linkedlist_stack.cs +++ b/codes/csharp/chapter_stack_and_queue/linkedlist_stack.cs @@ -4,7 +4,7 @@ * Author: haptear (haptear@hotmail.com) */ -using hello_algo.include; +using hello_algo.utils; using NUnit.Framework; namespace hello_algo.chapter_stack_and_queue; diff --git a/codes/csharp/chapter_tree/avl_tree.cs b/codes/csharp/chapter_tree/avl_tree.cs index ccf6a38a5..60485d3bf 100644 --- a/codes/csharp/chapter_tree/avl_tree.cs +++ b/codes/csharp/chapter_tree/avl_tree.cs @@ -4,7 +4,7 @@ * Author: haptear (haptear@hotmail.com) */ -using hello_algo.include; +using hello_algo.utils; using NUnit.Framework; namespace hello_algo.chapter_tree; diff --git a/codes/csharp/chapter_tree/binary_search_tree.cs b/codes/csharp/chapter_tree/binary_search_tree.cs index 43830a1a5..391369984 100644 --- a/codes/csharp/chapter_tree/binary_search_tree.cs +++ b/codes/csharp/chapter_tree/binary_search_tree.cs @@ -4,7 +4,7 @@ * Author: haptear (haptear@hotmail.com) */ -using hello_algo.include; +using hello_algo.utils; using NUnit.Framework; namespace hello_algo.chapter_tree; diff --git a/codes/csharp/chapter_tree/binary_tree.cs b/codes/csharp/chapter_tree/binary_tree.cs index d94b87891..4dc0e80b2 100644 --- a/codes/csharp/chapter_tree/binary_tree.cs +++ b/codes/csharp/chapter_tree/binary_tree.cs @@ -4,7 +4,7 @@ * Author: haptear (haptear@hotmail.com) */ -using hello_algo.include; +using hello_algo.utils; using NUnit.Framework; namespace hello_algo.chapter_tree; diff --git a/codes/csharp/chapter_tree/binary_tree_bfs.cs b/codes/csharp/chapter_tree/binary_tree_bfs.cs index 18b2810b1..f0548198d 100644 --- a/codes/csharp/chapter_tree/binary_tree_bfs.cs +++ b/codes/csharp/chapter_tree/binary_tree_bfs.cs @@ -4,7 +4,7 @@ * Author: haptear (haptear@hotmail.com) */ -using hello_algo.include; +using hello_algo.utils; using NUnit.Framework; namespace hello_algo.chapter_tree; diff --git a/codes/csharp/chapter_tree/binary_tree_dfs.cs b/codes/csharp/chapter_tree/binary_tree_dfs.cs index 6b0d3481a..b0935a557 100644 --- a/codes/csharp/chapter_tree/binary_tree_dfs.cs +++ b/codes/csharp/chapter_tree/binary_tree_dfs.cs @@ -4,7 +4,7 @@ * Author: haptear (haptear@hotmail.com) */ -using hello_algo.include; +using hello_algo.utils; using NUnit.Framework; namespace hello_algo.chapter_tree; diff --git a/codes/csharp/include/ListNode.cs b/codes/csharp/utils/ListNode.cs similarity index 97% rename from codes/csharp/include/ListNode.cs rename to codes/csharp/utils/ListNode.cs index 8bc8acf29..dab5a6617 100644 --- a/codes/csharp/include/ListNode.cs +++ b/codes/csharp/utils/ListNode.cs @@ -2,7 +2,7 @@ // Created Time: 2022-12-16 // Author: mingXta (1195669834@qq.com) -namespace hello_algo.include; +namespace hello_algo.utils; /* Definition for a singly-linked list node */ public class ListNode { diff --git a/codes/csharp/include/PrintUtil.cs b/codes/csharp/utils/PrintUtil.cs similarity index 99% rename from codes/csharp/include/PrintUtil.cs rename to codes/csharp/utils/PrintUtil.cs index e64b555a5..c40d3ec3a 100644 --- a/codes/csharp/include/PrintUtil.cs +++ b/codes/csharp/utils/PrintUtil.cs @@ -4,7 +4,7 @@ * Author: haptear (haptear@hotmail.com), krahets (krahets@163.com) */ -namespace hello_algo.include; +namespace hello_algo.utils; public class Trunk { public Trunk? prev; diff --git a/codes/csharp/include/TreeNode.cs b/codes/csharp/utils/TreeNode.cs similarity index 98% rename from codes/csharp/include/TreeNode.cs rename to codes/csharp/utils/TreeNode.cs index bd810a8cc..5bf3f3c38 100644 --- a/codes/csharp/include/TreeNode.cs +++ b/codes/csharp/utils/TreeNode.cs @@ -4,7 +4,7 @@ * Author: haptear (haptear@hotmail.com) */ -namespace hello_algo.include; +namespace hello_algo.utils; /* 二叉树节点类 */ public class TreeNode { diff --git a/codes/csharp/include/Vertex.cs b/codes/csharp/utils/Vertex.cs similarity index 96% rename from codes/csharp/include/Vertex.cs rename to codes/csharp/utils/Vertex.cs index 45085817c..2c85ead74 100644 --- a/codes/csharp/include/Vertex.cs +++ b/codes/csharp/utils/Vertex.cs @@ -4,7 +4,7 @@ * Author: zjkung1123 (zjkung1123@gmail.com), krahets (krahets@163.com) */ -namespace hello_algo.include; +namespace hello_algo.utils; /* 顶点类 */ public class Vertex { diff --git a/codes/java/utils/ListNode.java b/codes/java/utils/ListNode.java new file mode 100755 index 000000000..0645128ce --- /dev/null +++ b/codes/java/utils/ListNode.java @@ -0,0 +1,36 @@ +/** + * File: ListNode.java + * Created Time: 2022-11-25 + * Author: Krahets (krahets@163.com) + */ + +package utils; + +/* Definition for a singly-linked list node */ +public class ListNode { + public int val; + public ListNode next; + + public ListNode(int x) { + val = x; + } + + /* Generate a linked list with an array */ + public static ListNode arrToLinkedList(int[] arr) { + ListNode dum = new ListNode(0); + ListNode head = dum; + for (int val : arr) { + head.next = new ListNode(val); + head = head.next; + } + return dum.next; + } + + /* Get a list node with specific value from a linked list */ + public static ListNode getListNode(ListNode head, int val) { + while (head != null && head.val != val) { + head = head.next; + } + return head; + } +} diff --git a/codes/java/utils/PrintUtil.java b/codes/java/utils/PrintUtil.java new file mode 100755 index 000000000..a9cd69a89 --- /dev/null +++ b/codes/java/utils/PrintUtil.java @@ -0,0 +1,118 @@ +/** + * File: PrintUtil.java + * Created Time: 2022-11-25 + * Author: Krahets (krahets@163.com) + */ + +package utils; + +import java.util.*; + +class Trunk { + Trunk prev; + String str; + + Trunk(Trunk prev, String str) { + this.prev = prev; + this.str = str; + } +}; + +public class PrintUtil { + + /* Print a matrix (Array) */ + public static void printMatrix(T[][] matrix) { + System.out.println("["); + for (T[] row : matrix) { + System.out.println(" " + row + ","); + } + System.out.println("]"); + } + + /* Print a matrix (List) */ + public static void printMatrix(List> matrix) { + System.out.println("["); + for (List row : matrix) { + System.out.println(" " + row + ","); + } + System.out.println("]"); + } + + /* Print a linked list */ + public static void printLinkedList(ListNode head) { + List list = new ArrayList<>(); + while (head != null) { + list.add(String.valueOf(head.val)); + head = head.next; + } + System.out.println(String.join(" -> ", list)); + } + + /** + * The interface of the tree printer + * This tree printer is borrowed from TECHIE DELIGHT + * https://www.techiedelight.com/c-program-print-binary-tree/ + */ + public static void printTree(TreeNode root) { + printTree(root, null, false); + } + + /* Print a binary tree */ + public static void printTree(TreeNode root, Trunk prev, boolean isLeft) { + if (root == null) { + return; + } + + String prev_str = " "; + Trunk trunk = new Trunk(prev, prev_str); + + printTree(root.right, trunk, true); + + if (prev == null) { + trunk.str = "———"; + } else if (isLeft) { + trunk.str = "/———"; + prev_str = " |"; + } else { + trunk.str = "\\———"; + prev.str = prev_str; + } + + showTrunks(trunk); + System.out.println(" " + root.val); + + if (prev != null) { + prev.str = prev_str; + } + trunk.str = " |"; + + printTree(root.left, trunk, false); + } + + /* Helper function to print branches of the binary tree */ + public static void showTrunks(Trunk p) { + if (p == null) { + return; + } + + showTrunks(p.prev); + System.out.print(p.str); + } + + /* Print a hash map */ + public static void printHashMap(Map map) { + for (Map.Entry kv : map.entrySet()) { + System.out.println(kv.getKey() + " -> " + kv.getValue()); + } + } + + /* Print a heap (PriorityQueue) */ + public static void printHeap(Queue queue) { + List list = new ArrayList<>(queue); + System.out.print("堆的数组表示:"); + System.out.println(list); + System.out.println("堆的树状表示:"); + TreeNode root = TreeNode.listToTree(list); + printTree(root); + } +} diff --git a/codes/java/utils/TreeNode.java b/codes/java/utils/TreeNode.java new file mode 100644 index 000000000..954494ba9 --- /dev/null +++ b/codes/java/utils/TreeNode.java @@ -0,0 +1,67 @@ +/** + * File: TreeNode.java + * Created Time: 2022-11-25 + * Author: Krahets (krahets@163.com) + */ + +package utils; + +import java.util.*; + +/* Definition for a binary tree node. */ +public class TreeNode { + public int val; // 节点值 + public int height; // 节点高度 + public TreeNode left; // 左子节点引用 + public TreeNode right; // 右子节点引用 + + public TreeNode(int x) { + val = x; + } + + /* Generate a binary tree given an array */ + public static TreeNode listToTree(List list) { + int size = list.size(); + if (size == 0) + return null; + + TreeNode root = new TreeNode(list.get(0)); + Queue queue = new LinkedList<>() {{ add(root); }}; + int i = 0; + while (!queue.isEmpty()) { + TreeNode node = queue.poll(); + if (++i >= size) + break; + if (list.get(i) != null) { + node.left = new TreeNode(list.get(i)); + queue.add(node.left); + } + if (++i >= size) + break; + if (list.get(i) != null) { + node.right = new TreeNode(list.get(i)); + queue.add(node.right); + } + } + return root; + } + + /* Serialize a binary tree to a list */ + public static List treeToList(TreeNode root) { + List list = new ArrayList<>(); + if (root == null) + return list; + Queue queue = new LinkedList<>() {{ add(root); }}; + while (!queue.isEmpty()) { + TreeNode node = queue.poll(); + if (node != null) { + list.add(node.val); + queue.add(node.left); + queue.add(node.right); + } else { + list.add(null); + } + } + return list; + } +} diff --git a/codes/java/utils/Vertex.java b/codes/java/utils/Vertex.java new file mode 100644 index 000000000..061745336 --- /dev/null +++ b/codes/java/utils/Vertex.java @@ -0,0 +1,36 @@ +/** + * File: Vertex.java + * Created Time: 2023-02-15 + * Author: Krahets (krahets@163.com) + */ + +package utils; + +import java.util.*; + +/* 顶点类 */ +public class Vertex { + public int val; + + public Vertex(int val) { + this.val = val; + } + + /* 输入值列表 vals ,返回顶点列表 vets */ + public static Vertex[] valsToVets(int[] vals) { + Vertex[] vets = new Vertex[vals.length]; + for (int i = 0; i < vals.length; i++) { + vets[i] = new Vertex(vals[i]); + } + return vets; + } + + /* 输入顶点列表 vets ,返回值列表 vals */ + public static List vetsToVals(List vets) { + List vals = new ArrayList<>(); + for (Vertex vet : vets) { + vals.add(vet.val); + } + return vals; + } +} diff --git a/utils b/utils new file mode 160000 index 000000000..67ba5be2b --- /dev/null +++ b/utils @@ -0,0 +1 @@ +Subproject commit 67ba5be2bf7160fcf96f7293473d3be5bc6d152d