translation: Add Python and Java code for EN version (#1345)

* Add the intial translation of code of all the languages

* test

* revert

* Remove

* Add Python and Java code for EN version
This commit is contained in:
Yudong Jin
2024-05-06 05:21:51 +08:00
committed by GitHub
parent b5e198db7d
commit 1c0f350ad6
174 changed files with 12349 additions and 0 deletions

View File

@@ -0,0 +1,28 @@
/**
* File: ListNode.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package utils;
/* Linked list node */
public class ListNode {
public int val;
public ListNode next;
public ListNode(int x) {
val = x;
}
/* Deserialize a list into a linked list */
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;
}
}

View File

@@ -0,0 +1,116 @@
/**
* 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 matrix (Array) */
public static <T> void printMatrix(T[][] matrix) {
System.out.println("[");
for (T[] row : matrix) {
System.out.println(" " + row + ",");
}
System.out.println("]");
}
/* Print matrix (List) */
public static <T> void printMatrix(List<List<T>> matrix) {
System.out.println("[");
for (List<T> row : matrix) {
System.out.println(" " + row + ",");
}
System.out.println("]");
}
/* Print linked list */
public static void printLinkedList(ListNode head) {
List<String> list = new ArrayList<>();
while (head != null) {
list.add(String.valueOf(head.val));
head = head.next;
}
System.out.println(String.join(" -> ", list));
}
/* Print binary tree */
public static void printTree(TreeNode root) {
printTree(root, null, false);
}
/**
* Print binary tree
* This tree printer is borrowed from TECHIE DELIGHT
* https://www.techiedelight.com/c-program-print-binary-tree/
*/
public static void printTree(TreeNode root, Trunk prev, boolean isRight) {
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 (isRight) {
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);
}
public static void showTrunks(Trunk p) {
if (p == null) {
return;
}
showTrunks(p.prev);
System.out.print(p.str);
}
/* Print hash table */
public static <K, V> void printHashMap(Map<K, V> map) {
for (Map.Entry<K, V> kv : map.entrySet()) {
System.out.println(kv.getKey() + " -> " + kv.getValue());
}
}
/* Print heap (Priority queue) */
public static void printHeap(Queue<Integer> queue) {
List<Integer> list = new ArrayList<>(queue);
System.out.print("Array representation of the heap:");
System.out.println(list);
System.out.println("Tree representation of the heap:");
TreeNode root = TreeNode.listToTree(list);
printTree(root);
}
}

View File

@@ -0,0 +1,73 @@
/**
* File: TreeNode.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package utils;
import java.util.*;
/* Binary tree node class */
public class TreeNode {
public int val; // Node value
public int height; // Node height
public TreeNode left; // Reference to the left child node
public TreeNode right; // Reference to the right child node
/* Constructor */
public TreeNode(int x) {
val = x;
}
// For serialization encoding rules, refer to:
// https://www.hello-algo.com/chapter_tree/array_representation_of_tree/
// Array representation of the binary tree:
// [1, 2, 3, 4, None, 6, 7, 8, 9, None, None, 12, None, None, 15]
// Linked list representation of the binary tree:
// /——— 15
// /——— 7
// /——— 3
// | \——— 6
// | \——— 12
// ——— 1
// \——— 2
// | /——— 9
// \——— 4
// \——— 8
/* Deserialize a list into a binary tree: Recursively */
private static TreeNode listToTreeDFS(List<Integer> arr, int i) {
if (i < 0 || i >= arr.size() || arr.get(i) == null) {
return null;
}
TreeNode root = new TreeNode(arr.get(i));
root.left = listToTreeDFS(arr, 2 * i + 1);
root.right = listToTreeDFS(arr, 2 * i + 2);
return root;
}
/* Deserialize a list into a binary tree */
public static TreeNode listToTree(List<Integer> arr) {
return listToTreeDFS(arr, 0);
}
/* Serialize a binary tree into a list: Recursively */
private static void treeToListDFS(TreeNode root, int i, List<Integer> res) {
if (root == null)
return;
while (i >= res.size()) {
res.add(null);
}
res.set(i, root.val);
treeToListDFS(root.left, 2 * i + 1, res);
treeToListDFS(root.right, 2 * i + 2, res);
}
/* Serialize a binary tree into a list */
public static List<Integer> treeToList(TreeNode root) {
List<Integer> res = new ArrayList<>();
treeToListDFS(root, 0, res);
return res;
}
}

View File

@@ -0,0 +1,36 @@
/**
* File: Vertex.java
* Created Time: 2023-02-15
* Author: krahets (krahets@163.com)
*/
package utils;
import java.util.*;
/* Vertex class */
public class Vertex {
public int val;
public Vertex(int val) {
this.val = val;
}
/* Input a list of values vals, return a list of vertices 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;
}
/* Input a list of vertices vets, return a list of values vals */
public static List<Integer> vetsToVals(List<Vertex> vets) {
List<Integer> vals = new ArrayList<>();
for (Vertex vet : vets) {
vals.add(vet.val);
}
return vals;
}
}