mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-25 03:08:54 +08:00
Add Java codes, and license.
This commit is contained in:
41
codes/java/include/ListNode.java
Executable file
41
codes/java/include/ListNode.java
Executable file
@ -0,0 +1,41 @@
|
||||
package include;
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @param arr
|
||||
* @return
|
||||
*/
|
||||
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
|
||||
* @param head
|
||||
* @param val
|
||||
* @return
|
||||
*/
|
||||
public static ListNode getListNode(ListNode head, int val) {
|
||||
while (head != null && head.val != val) {
|
||||
head = head.next;
|
||||
}
|
||||
return head;
|
||||
}
|
||||
}
|
88
codes/java/include/PrintUtil.java
Executable file
88
codes/java/include/PrintUtil.java
Executable file
@ -0,0 +1,88 @@
|
||||
package include;
|
||||
|
||||
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 linked list
|
||||
* @param head
|
||||
*/
|
||||
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));
|
||||
}
|
||||
|
||||
/**
|
||||
* The interface of the tree printer
|
||||
* This tree printer is borrowed from TECHIE DELIGHT
|
||||
* https://www.techiedelight.com/c-program-print-binary-tree/
|
||||
* @param root
|
||||
*/
|
||||
public static void printTree(TreeNode root) {
|
||||
printTree(root, null, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Print a binary tree
|
||||
* @param root
|
||||
* @param prev
|
||||
* @param isLeft
|
||||
*/
|
||||
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
|
||||
* @param p
|
||||
*/
|
||||
public static void showTrunks(Trunk p) {
|
||||
if (p == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
showTrunks(p.prev);
|
||||
System.out.print(p.str);
|
||||
}
|
||||
}
|
80
codes/java/include/TreeNode.java
Executable file
80
codes/java/include/TreeNode.java
Executable file
@ -0,0 +1,80 @@
|
||||
package include;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
*/
|
||||
public class TreeNode {
|
||||
public int val;
|
||||
public TreeNode left;
|
||||
public TreeNode right;
|
||||
|
||||
public TreeNode(int x) {
|
||||
val = x;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a binary tree with an array
|
||||
* @param arr
|
||||
* @return
|
||||
*/
|
||||
public static TreeNode arrToTree(Integer[] arr) {
|
||||
TreeNode root = new TreeNode(arr[0]);
|
||||
Queue<TreeNode> queue = new LinkedList<>() {{ add(root); }};
|
||||
int i = 1;
|
||||
while(!queue.isEmpty()) {
|
||||
TreeNode node = queue.poll();
|
||||
if(arr[i] != null) {
|
||||
node.left = new TreeNode(arr[i]);
|
||||
queue.add(node.left);
|
||||
}
|
||||
i++;
|
||||
if(arr[i] != null) {
|
||||
node.right = new TreeNode(arr[i]);
|
||||
queue.add(node.right);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return root;
|
||||
}
|
||||
|
||||
/**
|
||||
* Serialize a binary tree to a list
|
||||
* @param root
|
||||
* @return
|
||||
*/
|
||||
public static List<Integer> treeToList(TreeNode root) {
|
||||
List<Integer> list = new ArrayList<>();
|
||||
if(root == null) return list;
|
||||
Queue<TreeNode> 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a tree node with specific value in a binary tree
|
||||
* @param root
|
||||
* @param val
|
||||
* @return
|
||||
*/
|
||||
public static TreeNode getTreeNode(TreeNode root, int val) {
|
||||
if (root == null)
|
||||
return null;
|
||||
if (root.val == val)
|
||||
return root;
|
||||
TreeNode left = getTreeNode(root.left, val);
|
||||
TreeNode right = getTreeNode(root.right, val);
|
||||
return left != null ? left : right;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user