mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-13 16:19:46 +08:00
Translate all code to English (#1836)
* Review the EN heading format. * Fix pythontutor headings. * Fix pythontutor headings. * bug fixes * Fix headings in **/summary.md * Revisit the CN-to-EN translation for Python code using Claude-4.5 * Revisit the CN-to-EN translation for Java code using Claude-4.5 * Revisit the CN-to-EN translation for Cpp code using Claude-4.5. * Fix the dictionary. * Fix cpp code translation for the multipart strings. * Translate Go code to English. * Update workflows to test EN code. * Add EN translation for C. * Add EN translation for CSharp. * Add EN translation for Swift. * Trigger the CI check. * Revert. * Update en/hash_map.md * Add the EN version of Dart code. * Add the EN version of Kotlin code. * Add missing code files. * Add the EN version of JavaScript code. * Add the EN version of TypeScript code. * Fix the workflows. * Add the EN version of Ruby code. * Add the EN version of Rust code. * Update the CI check for the English version code. * Update Python CI check. * Fix cmakelists for en/C code. * Fix Ruby comments
This commit is contained in:
@@ -10,40 +10,40 @@ import java.util.*;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
public class array {
|
||||
/* Random access to elements */
|
||||
/* Random access to element */
|
||||
static int randomAccess(int[] nums) {
|
||||
// Randomly select a number in the interval [0, nums.length)
|
||||
int randomIndex = ThreadLocalRandom.current().nextInt(0, nums.length);
|
||||
// Retrieve and return a random element
|
||||
// Retrieve and return the random element
|
||||
int randomNum = nums[randomIndex];
|
||||
return randomNum;
|
||||
}
|
||||
|
||||
/* Extend array length */
|
||||
static int[] extend(int[] nums, int enlarge) {
|
||||
// Initialize an extended length array
|
||||
// Initialize an array with extended length
|
||||
int[] res = new int[nums.length + enlarge];
|
||||
// Copy all elements from the original array to the new array
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
res[i] = nums[i];
|
||||
}
|
||||
// Return the new array after expansion
|
||||
// Return the extended new array
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Insert element num at `index` */
|
||||
/* Insert element num at index index in the array */
|
||||
static void insert(int[] nums, int num, int index) {
|
||||
// Move all elements after `index` one position backward
|
||||
// Move all elements at and after index index backward by one position
|
||||
for (int i = nums.length - 1; i > index; i--) {
|
||||
nums[i] = nums[i - 1];
|
||||
}
|
||||
// Assign num to the element at index
|
||||
// Assign num to the element at index index
|
||||
nums[index] = num;
|
||||
}
|
||||
|
||||
/* Remove the element at `index` */
|
||||
/* Remove the element at index index */
|
||||
static void remove(int[] nums, int index) {
|
||||
// Move all elements after `index` one position forward
|
||||
// Move all elements after index index forward by one position
|
||||
for (int i = index; i < nums.length - 1; i++) {
|
||||
nums[i] = nums[i + 1];
|
||||
}
|
||||
@@ -56,13 +56,13 @@ public class array {
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
count += nums[i];
|
||||
}
|
||||
// Traverse array elements
|
||||
// Direct traversal of array elements
|
||||
for (int num : nums) {
|
||||
count += num;
|
||||
}
|
||||
}
|
||||
|
||||
/* Search for a specified element in the array */
|
||||
/* Find the specified element in the array */
|
||||
static int find(int[] nums, int target) {
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
if (nums[i] == target)
|
||||
@@ -73,33 +73,33 @@ public class array {
|
||||
|
||||
/* Driver Code */
|
||||
public static void main(String[] args) {
|
||||
/* Initialize an array */
|
||||
/* Initialize array */
|
||||
int[] arr = new int[5];
|
||||
System.out.println("Array arr = " + Arrays.toString(arr));
|
||||
int[] nums = { 1, 3, 2, 5, 4 };
|
||||
System.out.println("Array nums = " + Arrays.toString(nums));
|
||||
|
||||
/* Random access */
|
||||
/* Insert element */
|
||||
int randomNum = randomAccess(nums);
|
||||
System.out.println("Get a random element from nums = " + randomNum);
|
||||
System.out.println("Get random element in nums " + randomNum);
|
||||
|
||||
/* Length extension */
|
||||
/* Traverse array */
|
||||
nums = extend(nums, 3);
|
||||
System.out.println("Extend the array length to 8, resulting in nums = " + Arrays.toString(nums));
|
||||
System.out.println("Extend array length to 8, resulting in nums = " + Arrays.toString(nums));
|
||||
|
||||
/* Insert element */
|
||||
insert(nums, 6, 3);
|
||||
System.out.println("Insert the number 6 at index 3, resulting in nums = " + Arrays.toString(nums));
|
||||
System.out.println("Insert number 6 at index 3, resulting in nums = " + Arrays.toString(nums));
|
||||
|
||||
/* Remove element */
|
||||
remove(nums, 2);
|
||||
System.out.println("Remove the element at index 2, resulting in nums = " + Arrays.toString(nums));
|
||||
System.out.println("Remove element at index 2, resulting in nums = " + Arrays.toString(nums));
|
||||
|
||||
/* Traverse array */
|
||||
traverse(nums);
|
||||
|
||||
/* Search for elements */
|
||||
/* Find element */
|
||||
int index = find(nums, 3);
|
||||
System.out.println("Find element 3 in nums, index = " + index);
|
||||
System.out.println("Find element 3 in nums, get index = " + index);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ public class linked_list {
|
||||
n0.next = n1;
|
||||
}
|
||||
|
||||
/* Access the node at `index` in the linked list */
|
||||
/* Access the node at index index in the linked list */
|
||||
static ListNode access(ListNode head, int index) {
|
||||
for (int i = 0; i < index; i++) {
|
||||
if (head == null)
|
||||
@@ -36,7 +36,7 @@ public class linked_list {
|
||||
return head;
|
||||
}
|
||||
|
||||
/* Search for the first node with value target in the linked list */
|
||||
/* Find the first node with value target in the linked list */
|
||||
static int find(ListNode head, int target) {
|
||||
int index = 0;
|
||||
while (head != null) {
|
||||
@@ -62,25 +62,25 @@ public class linked_list {
|
||||
n1.next = n2;
|
||||
n2.next = n3;
|
||||
n3.next = n4;
|
||||
System.out.println("The initialized linked list is");
|
||||
System.out.println("Initialized linked list is");
|
||||
PrintUtil.printLinkedList(n0);
|
||||
|
||||
/* Insert node */
|
||||
insert(n0, new ListNode(0));
|
||||
System.out.println("Linked list after inserting the node is");
|
||||
System.out.println("Linked list after inserting node is");
|
||||
PrintUtil.printLinkedList(n0);
|
||||
|
||||
/* Remove node */
|
||||
remove(n0);
|
||||
System.out.println("Linked list after removing the node is");
|
||||
System.out.println("Linked list after removing node is");
|
||||
PrintUtil.printLinkedList(n0);
|
||||
|
||||
/* Access node */
|
||||
ListNode node = access(n0, 3);
|
||||
System.out.println("The value of the node at index 3 in the linked list = " + node.val);
|
||||
System.out.println("Value of node at index 3 in linked list = " + node.val);
|
||||
|
||||
/* Search node */
|
||||
int index = find(n0, 2);
|
||||
System.out.println("The index of the node with value 2 in the linked list = " + index);
|
||||
System.out.println("Index of node with value 2 in linked list = " + index);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,24 +11,24 @@ import java.util.*;
|
||||
public class list {
|
||||
public static void main(String[] args) {
|
||||
/* Initialize list */
|
||||
// The array's element type is Integer[], a wrapper class for int
|
||||
// Note that the array element type is Integer[], the wrapper class of int[]
|
||||
Integer[] numbers = new Integer[] { 1, 3, 2, 5, 4 };
|
||||
List<Integer> nums = new ArrayList<>(Arrays.asList(numbers));
|
||||
System.out.println("List nums = " + nums);
|
||||
|
||||
/* Access element */
|
||||
int num = nums.get(1);
|
||||
System.out.println("Access the element at index 1, obtained num = " + num);
|
||||
|
||||
/* Update element */
|
||||
int num = nums.get(1);
|
||||
System.out.println("Access element at index 1, get num = " + num);
|
||||
|
||||
/* Add elements at the end */
|
||||
nums.set(1, 0);
|
||||
System.out.println("Update the element at index 1 to 0, resulting in nums = " + nums);
|
||||
System.out.println("Update element at index 1 to 0, resulting in nums = " + nums);
|
||||
|
||||
/* Clear list */
|
||||
/* Remove element */
|
||||
nums.clear();
|
||||
System.out.println("After clearing the list, nums = " + nums);
|
||||
System.out.println("After clearing list, nums = " + nums);
|
||||
|
||||
/* Add element at the end */
|
||||
/* Direct traversal of list elements */
|
||||
nums.add(1);
|
||||
nums.add(3);
|
||||
nums.add(2);
|
||||
@@ -36,20 +36,20 @@ public class list {
|
||||
nums.add(4);
|
||||
System.out.println("After adding elements, nums = " + nums);
|
||||
|
||||
/* Insert element in the middle */
|
||||
/* Sort list */
|
||||
nums.add(3, 6);
|
||||
System.out.println("Insert the number 6 at index 3, resulting in nums = " + nums);
|
||||
System.out.println("Insert number 6 at index 3, resulting in nums = " + nums);
|
||||
|
||||
/* Remove element */
|
||||
nums.remove(3);
|
||||
System.out.println("Remove the element at index 3, resulting in nums = " + nums);
|
||||
System.out.println("Remove element at index 3, resulting in nums = " + nums);
|
||||
|
||||
/* Traverse the list by index */
|
||||
/* Traverse list by index */
|
||||
int count = 0;
|
||||
for (int i = 0; i < nums.size(); i++) {
|
||||
count += nums.get(i);
|
||||
}
|
||||
/* Traverse the list elements */
|
||||
/* Directly traverse list elements */
|
||||
for (int x : nums) {
|
||||
count += x;
|
||||
}
|
||||
@@ -61,6 +61,6 @@ public class list {
|
||||
|
||||
/* Sort list */
|
||||
Collections.sort(nums);
|
||||
System.out.println("After sorting the list, nums = " + nums);
|
||||
System.out.println("After sorting list, nums = " + nums);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ class MyList {
|
||||
private int[] arr; // Array (stores list elements)
|
||||
private int capacity = 10; // List capacity
|
||||
private int size = 0; // List length (current number of elements)
|
||||
private int extendRatio = 2; // Multiple for each list expansion
|
||||
private int extendRatio = 2; // Multiple by which the list capacity is extended each time
|
||||
|
||||
/* Constructor */
|
||||
public MyList() {
|
||||
@@ -30,7 +30,7 @@ class MyList {
|
||||
return capacity;
|
||||
}
|
||||
|
||||
/* Access element */
|
||||
/* Update element */
|
||||
public int get(int index) {
|
||||
// If the index is out of bounds, throw an exception, as below
|
||||
if (index < 0 || index >= size)
|
||||
@@ -38,16 +38,16 @@ class MyList {
|
||||
return arr[index];
|
||||
}
|
||||
|
||||
/* Update element */
|
||||
/* Add elements at the end */
|
||||
public void set(int index, int num) {
|
||||
if (index < 0 || index >= size)
|
||||
throw new IndexOutOfBoundsException("Index out of bounds");
|
||||
arr[index] = num;
|
||||
}
|
||||
|
||||
/* Add element at the end */
|
||||
/* Direct traversal of list elements */
|
||||
public void add(int num) {
|
||||
// When the number of elements exceeds capacity, trigger the expansion mechanism
|
||||
// When the number of elements exceeds capacity, trigger the extension mechanism
|
||||
if (size == capacity())
|
||||
extendCapacity();
|
||||
arr[size] = num;
|
||||
@@ -55,14 +55,14 @@ class MyList {
|
||||
size++;
|
||||
}
|
||||
|
||||
/* Insert element in the middle */
|
||||
/* Sort list */
|
||||
public void insert(int index, int num) {
|
||||
if (index < 0 || index >= size)
|
||||
throw new IndexOutOfBoundsException("Index out of bounds");
|
||||
// When the number of elements exceeds capacity, trigger the expansion mechanism
|
||||
// When the number of elements exceeds capacity, trigger the extension mechanism
|
||||
if (size == capacity())
|
||||
extendCapacity();
|
||||
// Move all elements after `index` one position backward
|
||||
// Move all elements after index index forward by one position
|
||||
for (int j = size - 1; j >= index; j--) {
|
||||
arr[j + 1] = arr[j];
|
||||
}
|
||||
@@ -76,7 +76,7 @@ class MyList {
|
||||
if (index < 0 || index >= size)
|
||||
throw new IndexOutOfBoundsException("Index out of bounds");
|
||||
int num = arr[index];
|
||||
// Move all elements after `index` one position forward
|
||||
// Move all elements after index forward by one position
|
||||
for (int j = index; j < size - 1; j++) {
|
||||
arr[j] = arr[j + 1];
|
||||
}
|
||||
@@ -86,18 +86,18 @@ class MyList {
|
||||
return num;
|
||||
}
|
||||
|
||||
/* Extend list */
|
||||
/* Driver Code */
|
||||
public void extendCapacity() {
|
||||
// Create a new array with a length multiple of the original array by extendRatio, and copy the original array to the new array
|
||||
// Create a new array with length extendRatio times the original array and copy the original array to the new array
|
||||
arr = Arrays.copyOf(arr, capacity() * extendRatio);
|
||||
// Update list capacity
|
||||
// Add elements at the end
|
||||
capacity = arr.length;
|
||||
}
|
||||
|
||||
/* Convert the list to an array */
|
||||
/* Convert list to array */
|
||||
public int[] toArray() {
|
||||
int size = size();
|
||||
// Only convert elements within valid length range
|
||||
// Elements enqueue
|
||||
int[] arr = new int[size];
|
||||
for (int i = 0; i < size; i++) {
|
||||
arr[i] = get(i);
|
||||
@@ -111,7 +111,7 @@ public class my_list {
|
||||
public static void main(String[] args) {
|
||||
/* Initialize list */
|
||||
MyList nums = new MyList();
|
||||
/* Add element at the end */
|
||||
/* Direct traversal of list elements */
|
||||
nums.add(1);
|
||||
nums.add(3);
|
||||
nums.add(2);
|
||||
@@ -120,28 +120,28 @@ public class my_list {
|
||||
System.out.println("List nums = " + Arrays.toString(nums.toArray()) +
|
||||
", capacity = " + nums.capacity() + ", length = " + nums.size());
|
||||
|
||||
/* Insert element in the middle */
|
||||
/* Sort list */
|
||||
nums.insert(3, 6);
|
||||
System.out.println("Insert the number 6 at index 3, resulting in nums = " + Arrays.toString(nums.toArray()));
|
||||
System.out.println("Insert number 6 at index 3, resulting in nums = " + Arrays.toString(nums.toArray()));
|
||||
|
||||
/* Remove element */
|
||||
nums.remove(3);
|
||||
System.out.println("Remove the element at index 3, resulting in nums = " + Arrays.toString(nums.toArray()));
|
||||
|
||||
/* Access element */
|
||||
int num = nums.get(1);
|
||||
System.out.println("Access the element at index 1, obtained num = " + num);
|
||||
System.out.println("Remove element at index 3, resulting in nums = " + Arrays.toString(nums.toArray()));
|
||||
|
||||
/* Update element */
|
||||
nums.set(1, 0);
|
||||
System.out.println("Update the element at index 1 to 0, resulting in nums = " + Arrays.toString(nums.toArray()));
|
||||
int num = nums.get(1);
|
||||
System.out.println("Access element at index 1, get num = " + num);
|
||||
|
||||
/* Test expansion mechanism */
|
||||
/* Add elements at the end */
|
||||
nums.set(1, 0);
|
||||
System.out.println("Update element at index 1 to 0, resulting in nums = " + Arrays.toString(nums.toArray()));
|
||||
|
||||
/* Test capacity expansion mechanism */
|
||||
for (int i = 0; i < 10; i++) {
|
||||
// At i = 5, the list length will exceed the list capacity, triggering the expansion mechanism at this time
|
||||
// At i = 5, the list length will exceed the list capacity, triggering the expansion mechanism
|
||||
nums.add(i);
|
||||
}
|
||||
System.out.println("After extending, list nums = " + Arrays.toString(nums.toArray()) +
|
||||
System.out.println("List nums after expansion = " + Arrays.toString(nums.toArray()) +
|
||||
", capacity = " + nums.capacity() + ", length = " + nums.size());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ package chapter_backtracking;
|
||||
import java.util.*;
|
||||
|
||||
public class n_queens {
|
||||
/* Backtracking algorithm: n queens */
|
||||
/* Backtracking algorithm: N queens */
|
||||
public static void backtrack(int row, int n, List<List<String>> state, List<List<List<String>>> res,
|
||||
boolean[] cols, boolean[] diags1, boolean[] diags2) {
|
||||
// When all rows are placed, record the solution
|
||||
@@ -23,26 +23,26 @@ public class n_queens {
|
||||
}
|
||||
// Traverse all columns
|
||||
for (int col = 0; col < n; col++) {
|
||||
// Calculate the main and minor diagonals corresponding to the cell
|
||||
// Calculate the main diagonal and anti-diagonal corresponding to this cell
|
||||
int diag1 = row - col + n - 1;
|
||||
int diag2 = row + col;
|
||||
// Pruning: do not allow queens on the column, main diagonal, or minor diagonal of the cell
|
||||
// Pruning: do not allow queens to exist in the column, main diagonal, and anti-diagonal of this cell
|
||||
if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {
|
||||
// Attempt: place the queen in the cell
|
||||
// Attempt: place the queen in this cell
|
||||
state.get(row).set(col, "Q");
|
||||
cols[col] = diags1[diag1] = diags2[diag2] = true;
|
||||
// Place the next row
|
||||
backtrack(row + 1, n, state, res, cols, diags1, diags2);
|
||||
// Retract: restore the cell to an empty spot
|
||||
// Backtrack: restore this cell to an empty cell
|
||||
state.get(row).set(col, "#");
|
||||
cols[col] = diags1[diag1] = diags2[diag2] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Solve n queens */
|
||||
/* Solve N queens */
|
||||
public static List<List<List<String>>> nQueens(int n) {
|
||||
// Initialize an n*n size chessboard, where 'Q' represents the queen and '#' represents an empty spot
|
||||
// Initialize an n*n chessboard, where 'Q' represents a queen and '#' represents an empty cell
|
||||
List<List<String>> state = new ArrayList<>();
|
||||
for (int i = 0; i < n; i++) {
|
||||
List<String> row = new ArrayList<>();
|
||||
@@ -51,9 +51,9 @@ public class n_queens {
|
||||
}
|
||||
state.add(row);
|
||||
}
|
||||
boolean[] cols = new boolean[n]; // Record columns with queens
|
||||
boolean[] diags1 = new boolean[2 * n - 1]; // Record main diagonals with queens
|
||||
boolean[] diags2 = new boolean[2 * n - 1]; // Record minor diagonals with queens
|
||||
boolean[] cols = new boolean[n]; // Record whether there is a queen in the column
|
||||
boolean[] diags1 = new boolean[2 * n - 1]; // Record whether there is a queen on the main diagonal
|
||||
boolean[] diags2 = new boolean[2 * n - 1]; // Record whether there is a queen on the anti-diagonal
|
||||
List<List<List<String>>> res = new ArrayList<>();
|
||||
|
||||
backtrack(0, n, state, res, cols, diags1, diags2);
|
||||
@@ -65,8 +65,8 @@ public class n_queens {
|
||||
int n = 4;
|
||||
List<List<List<String>>> res = nQueens(n);
|
||||
|
||||
System.out.println("Input the dimensions of the chessboard as " + n);
|
||||
System.out.println("Total number of queen placement solutions = " + res.size());
|
||||
System.out.println("Input board size is " + n);
|
||||
System.out.println("Total queen placement solutions: " + res.size() + "");
|
||||
for (List<List<String>> state : res) {
|
||||
System.out.println("--------------------");
|
||||
for (List<String> row : state) {
|
||||
|
||||
@@ -9,7 +9,7 @@ package chapter_backtracking;
|
||||
import java.util.*;
|
||||
|
||||
public class permutations_i {
|
||||
/* Backtracking algorithm: Permutation I */
|
||||
/* Backtracking algorithm: Permutations I */
|
||||
public static void backtrack(List<Integer> state, int[] choices, boolean[] selected, List<List<Integer>> res) {
|
||||
// When the state length equals the number of elements, record the solution
|
||||
if (state.size() == choices.length) {
|
||||
@@ -21,19 +21,19 @@ public class permutations_i {
|
||||
int choice = choices[i];
|
||||
// Pruning: do not allow repeated selection of elements
|
||||
if (!selected[i]) {
|
||||
// Attempt: make a choice, update the state
|
||||
// Attempt: make choice, update state
|
||||
selected[i] = true;
|
||||
state.add(choice);
|
||||
// Proceed to the next round of selection
|
||||
backtrack(state, choices, selected, res);
|
||||
// Retract: undo the choice, restore to the previous state
|
||||
// Backtrack: undo choice, restore to previous state
|
||||
selected[i] = false;
|
||||
state.remove(state.size() - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Permutation I */
|
||||
/* Permutations I */
|
||||
static List<List<Integer>> permutationsI(int[] nums) {
|
||||
List<List<Integer>> res = new ArrayList<List<Integer>>();
|
||||
backtrack(new ArrayList<Integer>(), nums, new boolean[nums.length], res);
|
||||
|
||||
@@ -9,7 +9,7 @@ package chapter_backtracking;
|
||||
import java.util.*;
|
||||
|
||||
public class permutations_ii {
|
||||
/* Backtracking algorithm: Permutation II */
|
||||
/* Backtracking algorithm: Permutations II */
|
||||
static void backtrack(List<Integer> state, int[] choices, boolean[] selected, List<List<Integer>> res) {
|
||||
// When the state length equals the number of elements, record the solution
|
||||
if (state.size() == choices.length) {
|
||||
@@ -22,20 +22,20 @@ public class permutations_ii {
|
||||
int choice = choices[i];
|
||||
// Pruning: do not allow repeated selection of elements and do not allow repeated selection of equal elements
|
||||
if (!selected[i] && !duplicated.contains(choice)) {
|
||||
// Attempt: make a choice, update the state
|
||||
duplicated.add(choice); // Record selected element values
|
||||
// Attempt: make choice, update state
|
||||
duplicated.add(choice); // Record the selected element value
|
||||
selected[i] = true;
|
||||
state.add(choice);
|
||||
// Proceed to the next round of selection
|
||||
backtrack(state, choices, selected, res);
|
||||
// Retract: undo the choice, restore to the previous state
|
||||
// Backtrack: undo choice, restore to previous state
|
||||
selected[i] = false;
|
||||
state.remove(state.size() - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Permutation II */
|
||||
/* Permutations II */
|
||||
static List<List<Integer>> permutationsII(int[] nums) {
|
||||
List<List<Integer>> res = new ArrayList<List<Integer>>();
|
||||
backtrack(new ArrayList<Integer>(), nums, new boolean[nums.length], res);
|
||||
|
||||
@@ -12,7 +12,7 @@ import java.util.*;
|
||||
public class preorder_traversal_i_compact {
|
||||
static List<TreeNode> res;
|
||||
|
||||
/* Pre-order traversal: Example one */
|
||||
/* Preorder traversal: Example 1 */
|
||||
static void preOrder(TreeNode root) {
|
||||
if (root == null) {
|
||||
return;
|
||||
@@ -30,7 +30,7 @@ public class preorder_traversal_i_compact {
|
||||
System.out.println("\nInitialize binary tree");
|
||||
PrintUtil.printTree(root);
|
||||
|
||||
// Pre-order traversal
|
||||
// Preorder traversal
|
||||
res = new ArrayList<>();
|
||||
preOrder(root);
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ public class preorder_traversal_ii_compact {
|
||||
static List<TreeNode> path;
|
||||
static List<List<TreeNode>> res;
|
||||
|
||||
/* Pre-order traversal: Example two */
|
||||
/* Preorder traversal: Example 2 */
|
||||
static void preOrder(TreeNode root) {
|
||||
if (root == null) {
|
||||
return;
|
||||
@@ -26,7 +26,7 @@ public class preorder_traversal_ii_compact {
|
||||
}
|
||||
preOrder(root.left);
|
||||
preOrder(root.right);
|
||||
// Retract
|
||||
// Backtrack
|
||||
path.remove(path.size() - 1);
|
||||
}
|
||||
|
||||
@@ -35,12 +35,12 @@ public class preorder_traversal_ii_compact {
|
||||
System.out.println("\nInitialize binary tree");
|
||||
PrintUtil.printTree(root);
|
||||
|
||||
// Pre-order traversal
|
||||
// Preorder traversal
|
||||
path = new ArrayList<>();
|
||||
res = new ArrayList<>();
|
||||
preOrder(root);
|
||||
|
||||
System.out.println("\nOutput all root-to-node 7 paths");
|
||||
System.out.println("\nOutput all paths from root node to node 7");
|
||||
for (List<TreeNode> path : res) {
|
||||
List<Integer> vals = new ArrayList<>();
|
||||
for (TreeNode node : path) {
|
||||
|
||||
@@ -13,7 +13,7 @@ public class preorder_traversal_iii_compact {
|
||||
static List<TreeNode> path;
|
||||
static List<List<TreeNode>> res;
|
||||
|
||||
/* Pre-order traversal: Example three */
|
||||
/* Preorder traversal: Example 3 */
|
||||
static void preOrder(TreeNode root) {
|
||||
// Pruning
|
||||
if (root == null || root.val == 3) {
|
||||
@@ -27,7 +27,7 @@ public class preorder_traversal_iii_compact {
|
||||
}
|
||||
preOrder(root.left);
|
||||
preOrder(root.right);
|
||||
// Retract
|
||||
// Backtrack
|
||||
path.remove(path.size() - 1);
|
||||
}
|
||||
|
||||
@@ -36,12 +36,12 @@ public class preorder_traversal_iii_compact {
|
||||
System.out.println("\nInitialize binary tree");
|
||||
PrintUtil.printTree(root);
|
||||
|
||||
// Pre-order traversal
|
||||
// Preorder traversal
|
||||
path = new ArrayList<>();
|
||||
res = new ArrayList<>();
|
||||
preOrder(root);
|
||||
|
||||
System.out.println("\nOutput all root-to-node 7 paths, not including nodes with value 3");
|
||||
System.out.println("\nOutput all paths from root node to node 7, paths do not include nodes with value 3");
|
||||
for (List<TreeNode> path : res) {
|
||||
List<Integer> vals = new ArrayList<>();
|
||||
for (TreeNode node : path) {
|
||||
|
||||
@@ -10,7 +10,7 @@ import utils.*;
|
||||
import java.util.*;
|
||||
|
||||
public class preorder_traversal_iii_template {
|
||||
/* Determine if the current state is a solution */
|
||||
/* Check if the current state is a solution */
|
||||
static boolean isSolution(List<TreeNode> state) {
|
||||
return !state.isEmpty() && state.get(state.size() - 1).val == 7;
|
||||
}
|
||||
@@ -20,7 +20,7 @@ public class preorder_traversal_iii_template {
|
||||
res.add(new ArrayList<>(state));
|
||||
}
|
||||
|
||||
/* Determine if the choice is legal under the current state */
|
||||
/* Check if the choice is valid under the current state */
|
||||
static boolean isValid(List<TreeNode> state, TreeNode choice) {
|
||||
return choice != null && choice.val != 3;
|
||||
}
|
||||
@@ -35,22 +35,22 @@ public class preorder_traversal_iii_template {
|
||||
state.remove(state.size() - 1);
|
||||
}
|
||||
|
||||
/* Backtracking algorithm: Example three */
|
||||
/* Backtracking algorithm: Example 3 */
|
||||
static void backtrack(List<TreeNode> state, List<TreeNode> choices, List<List<TreeNode>> res) {
|
||||
// Check if it's a solution
|
||||
// Check if it is a solution
|
||||
if (isSolution(state)) {
|
||||
// Record solution
|
||||
recordSolution(state, res);
|
||||
}
|
||||
// Traverse all choices
|
||||
for (TreeNode choice : choices) {
|
||||
// Pruning: check if the choice is legal
|
||||
// Pruning: check if the choice is valid
|
||||
if (isValid(state, choice)) {
|
||||
// Attempt: make a choice, update the state
|
||||
// Attempt: make choice, update state
|
||||
makeChoice(state, choice);
|
||||
// Proceed to the next round of selection
|
||||
backtrack(state, Arrays.asList(choice.left, choice.right), res);
|
||||
// Retract: undo the choice, restore to the previous state
|
||||
// Backtrack: undo choice, restore to previous state
|
||||
undoChoice(state, choice);
|
||||
}
|
||||
}
|
||||
@@ -65,7 +65,7 @@ public class preorder_traversal_iii_template {
|
||||
List<List<TreeNode>> res = new ArrayList<>();
|
||||
backtrack(new ArrayList<>(), Arrays.asList(root), res);
|
||||
|
||||
System.out.println("\nOutput all root-to-node 7 paths, requiring paths not to include nodes with value 3");
|
||||
System.out.println("\nOutput all paths from root node to node 7, requiring paths do not include nodes with value 3");
|
||||
for (List<TreeNode> path : res) {
|
||||
List<Integer> vals = new ArrayList<>();
|
||||
for (TreeNode node : path) {
|
||||
|
||||
@@ -9,7 +9,7 @@ package chapter_backtracking;
|
||||
import java.util.*;
|
||||
|
||||
public class subset_sum_i {
|
||||
/* Backtracking algorithm: Subset Sum I */
|
||||
/* Backtracking algorithm: Subset sum I */
|
||||
static void backtrack(List<Integer> state, int target, int[] choices, int start, List<List<Integer>> res) {
|
||||
// When the subset sum equals target, record the solution
|
||||
if (target == 0) {
|
||||
@@ -17,23 +17,23 @@ public class subset_sum_i {
|
||||
return;
|
||||
}
|
||||
// Traverse all choices
|
||||
// Pruning two: start traversing from start to avoid generating duplicate subsets
|
||||
// Pruning 2: start traversing from start to avoid generating duplicate subsets
|
||||
for (int i = start; i < choices.length; i++) {
|
||||
// Pruning one: if the subset sum exceeds target, end the loop immediately
|
||||
// Pruning 1: if the subset sum exceeds target, end the loop directly
|
||||
// This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target
|
||||
if (target - choices[i] < 0) {
|
||||
break;
|
||||
}
|
||||
// Attempt: make a choice, update target, start
|
||||
// Attempt: make choice, update target, start
|
||||
state.add(choices[i]);
|
||||
// Proceed to the next round of selection
|
||||
backtrack(state, target - choices[i], choices, i, res);
|
||||
// Retract: undo the choice, restore to the previous state
|
||||
// Backtrack: undo choice, restore to previous state
|
||||
state.remove(state.size() - 1);
|
||||
}
|
||||
}
|
||||
|
||||
/* Solve Subset Sum I */
|
||||
/* Solve subset sum I */
|
||||
static List<List<Integer>> subsetSumI(int[] nums, int target) {
|
||||
List<Integer> state = new ArrayList<>(); // State (subset)
|
||||
Arrays.sort(nums); // Sort nums
|
||||
@@ -50,6 +50,6 @@ public class subset_sum_i {
|
||||
List<List<Integer>> res = subsetSumI(nums, target);
|
||||
|
||||
System.out.println("Input array nums = " + Arrays.toString(nums) + ", target = " + target);
|
||||
System.out.println("All subsets summing to " + target + " res = " + res);
|
||||
System.out.println("All subsets with sum equal to " + target + " are res = " + res);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ package chapter_backtracking;
|
||||
import java.util.*;
|
||||
|
||||
public class subset_sum_i_naive {
|
||||
/* Backtracking algorithm: Subset Sum I */
|
||||
/* Backtracking algorithm: Subset sum I */
|
||||
static void backtrack(List<Integer> state, int target, int total, int[] choices, List<List<Integer>> res) {
|
||||
// When the subset sum equals target, record the solution
|
||||
if (total == target) {
|
||||
@@ -18,20 +18,20 @@ public class subset_sum_i_naive {
|
||||
}
|
||||
// Traverse all choices
|
||||
for (int i = 0; i < choices.length; i++) {
|
||||
// Pruning: if the subset sum exceeds target, skip that choice
|
||||
// Pruning: if the subset sum exceeds target, skip this choice
|
||||
if (total + choices[i] > target) {
|
||||
continue;
|
||||
}
|
||||
// Attempt: make a choice, update elements and total
|
||||
// Attempt: make choice, update element sum total
|
||||
state.add(choices[i]);
|
||||
// Proceed to the next round of selection
|
||||
backtrack(state, target, total + choices[i], choices, res);
|
||||
// Retract: undo the choice, restore to the previous state
|
||||
// Backtrack: undo choice, restore to previous state
|
||||
state.remove(state.size() - 1);
|
||||
}
|
||||
}
|
||||
|
||||
/* Solve Subset Sum I (including duplicate subsets) */
|
||||
/* Solve subset sum I (including duplicate subsets) */
|
||||
static List<List<Integer>> subsetSumINaive(int[] nums, int target) {
|
||||
List<Integer> state = new ArrayList<>(); // State (subset)
|
||||
int total = 0; // Subset sum
|
||||
@@ -47,7 +47,7 @@ public class subset_sum_i_naive {
|
||||
List<List<Integer>> res = subsetSumINaive(nums, target);
|
||||
|
||||
System.out.println("Input array nums = " + Arrays.toString(nums) + ", target = " + target);
|
||||
System.out.println("All subsets summing to " + target + " res = " + res);
|
||||
System.out.println("The result of this method includes duplicate sets");
|
||||
System.out.println("All subsets with sum equal to " + target + " are res = " + res);
|
||||
System.out.println("Please note that this method outputs results containing duplicate sets");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ package chapter_backtracking;
|
||||
import java.util.*;
|
||||
|
||||
public class subset_sum_ii {
|
||||
/* Backtracking algorithm: Subset Sum II */
|
||||
/* Backtracking algorithm: Subset sum II */
|
||||
static void backtrack(List<Integer> state, int target, int[] choices, int start, List<List<Integer>> res) {
|
||||
// When the subset sum equals target, record the solution
|
||||
if (target == 0) {
|
||||
@@ -17,28 +17,28 @@ public class subset_sum_ii {
|
||||
return;
|
||||
}
|
||||
// Traverse all choices
|
||||
// Pruning two: start traversing from start to avoid generating duplicate subsets
|
||||
// Pruning three: start traversing from start to avoid repeatedly selecting the same element
|
||||
// Pruning 2: start traversing from start to avoid generating duplicate subsets
|
||||
// Pruning 3: start traversing from start to avoid repeatedly selecting the same element
|
||||
for (int i = start; i < choices.length; i++) {
|
||||
// Pruning one: if the subset sum exceeds target, end the loop immediately
|
||||
// Pruning 1: if the subset sum exceeds target, end the loop directly
|
||||
// This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target
|
||||
if (target - choices[i] < 0) {
|
||||
break;
|
||||
}
|
||||
// Pruning four: if the element equals the left element, it indicates that the search branch is repeated, skip it
|
||||
// Pruning 4: if this element equals the left element, it means this search branch is duplicate, skip it directly
|
||||
if (i > start && choices[i] == choices[i - 1]) {
|
||||
continue;
|
||||
}
|
||||
// Attempt: make a choice, update target, start
|
||||
// Attempt: make choice, update target, start
|
||||
state.add(choices[i]);
|
||||
// Proceed to the next round of selection
|
||||
backtrack(state, target - choices[i], choices, i + 1, res);
|
||||
// Retract: undo the choice, restore to the previous state
|
||||
// Backtrack: undo choice, restore to previous state
|
||||
state.remove(state.size() - 1);
|
||||
}
|
||||
}
|
||||
|
||||
/* Solve Subset Sum II */
|
||||
/* Solve subset sum II */
|
||||
static List<List<Integer>> subsetSumII(int[] nums, int target) {
|
||||
List<Integer> state = new ArrayList<>(); // State (subset)
|
||||
Arrays.sort(nums); // Sort nums
|
||||
@@ -55,6 +55,6 @@ public class subset_sum_ii {
|
||||
List<List<Integer>> res = subsetSumII(nums, target);
|
||||
|
||||
System.out.println("Input array nums = " + Arrays.toString(nums) + ", target = " + target);
|
||||
System.out.println("All subsets summing to " + target + " res = " + res);
|
||||
System.out.println("All subsets with sum equal to " + target + " are res = " + res);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ public class iteration {
|
||||
/* for loop */
|
||||
static int forLoop(int n) {
|
||||
int res = 0;
|
||||
// Loop sum 1, 2, ..., n-1, n
|
||||
// Sum 1, 2, ..., n-1, n
|
||||
for (int i = 1; i <= n; i++) {
|
||||
res += i;
|
||||
}
|
||||
@@ -21,7 +21,7 @@ public class iteration {
|
||||
static int whileLoop(int n) {
|
||||
int res = 0;
|
||||
int i = 1; // Initialize condition variable
|
||||
// Loop sum 1, 2, ..., n-1, n
|
||||
// Sum 1, 2, ..., n-1, n
|
||||
while (i <= n) {
|
||||
res += i;
|
||||
i++; // Update condition variable
|
||||
@@ -33,7 +33,7 @@ public class iteration {
|
||||
static int whileLoopII(int n) {
|
||||
int res = 0;
|
||||
int i = 1; // Initialize condition variable
|
||||
// Loop sum 1, 4, 10, ...
|
||||
// Sum 1, 4, 10, ...
|
||||
while (i <= n) {
|
||||
res += i;
|
||||
// Update condition variable
|
||||
@@ -43,7 +43,7 @@ public class iteration {
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Double for loop */
|
||||
/* Nested for loop */
|
||||
static String nestedForLoop(int n) {
|
||||
StringBuilder res = new StringBuilder();
|
||||
// Loop i = 1, 2, ..., n-1, n
|
||||
@@ -62,15 +62,15 @@ public class iteration {
|
||||
int res;
|
||||
|
||||
res = forLoop(n);
|
||||
System.out.println("\nSum result of the for loop res = " + res);
|
||||
System.out.println("\nfor loop sum result res = " + res);
|
||||
|
||||
res = whileLoop(n);
|
||||
System.out.println("\nSum result of the while loop res = " + res);
|
||||
System.out.println("\nwhile loop sum result res = " + res);
|
||||
|
||||
res = whileLoopII(n);
|
||||
System.out.println("\nSum result of the while loop (with two updates) res = " + res);
|
||||
System.out.println("\nwhile loop (two updates) sum result res = " + res);
|
||||
|
||||
String resStr = nestedForLoop(n);
|
||||
System.out.println("\nResult of the double for loop traversal = " + resStr);
|
||||
System.out.println("\nDouble for loop traversal result " + resStr);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,25 +14,25 @@ public class recursion {
|
||||
// Termination condition
|
||||
if (n == 1)
|
||||
return 1;
|
||||
// Recursive: recursive call
|
||||
// Recurse: recursive call
|
||||
int res = recur(n - 1);
|
||||
// Return: return result
|
||||
return n + res;
|
||||
}
|
||||
|
||||
/* Simulate recursion with iteration */
|
||||
/* Simulate recursion using iteration */
|
||||
static int forLoopRecur(int n) {
|
||||
// Use an explicit stack to simulate the system call stack
|
||||
Stack<Integer> stack = new Stack<>();
|
||||
int res = 0;
|
||||
// Recursive: recursive call
|
||||
// Recurse: recursive call
|
||||
for (int i = n; i > 0; i--) {
|
||||
// Simulate "recursive" by "pushing onto the stack"
|
||||
// Simulate "recurse" with "push"
|
||||
stack.push(i);
|
||||
}
|
||||
// Return: return result
|
||||
while (!stack.isEmpty()) {
|
||||
// Simulate "return" by "popping from the stack"
|
||||
// Simulate "return" with "pop"
|
||||
res += stack.pop();
|
||||
}
|
||||
// res = 1+2+3+...+n
|
||||
@@ -48,7 +48,7 @@ public class recursion {
|
||||
return tailRecur(n - 1, res + n);
|
||||
}
|
||||
|
||||
/* Fibonacci sequence: Recursion */
|
||||
/* Fibonacci sequence: recursion */
|
||||
static int fib(int n) {
|
||||
// Termination condition f(1) = 0, f(2) = 1
|
||||
if (n == 1 || n == 2)
|
||||
@@ -65,15 +65,15 @@ public class recursion {
|
||||
int res;
|
||||
|
||||
res = recur(n);
|
||||
System.out.println("\nSum result of the recursive function res = " + res);
|
||||
System.out.println("\nRecursive function sum result res = " + res);
|
||||
|
||||
res = forLoopRecur(n);
|
||||
System.out.println("\nSum result using iteration to simulate recursion res = " + res);
|
||||
System.out.println("\nUsing iteration to simulate recursive sum result res = " + res);
|
||||
|
||||
res = tailRecur(n, 0);
|
||||
System.out.println("\nSum result of the tail-recursive function res = " + res);
|
||||
System.out.println("\nTail recursive function sum result res = " + res);
|
||||
|
||||
res = fib(n);
|
||||
System.out.println("\nThe " + n + "th number in the Fibonacci sequence is " + res);
|
||||
System.out.println("\nThe " + n + "th term of the Fibonacci sequence is " + res);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,26 +16,26 @@ public class space_complexity {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Constant complexity */
|
||||
/* Constant order */
|
||||
static void constant(int n) {
|
||||
// Constants, variables, objects occupy O(1) space
|
||||
final int a = 0;
|
||||
int b = 0;
|
||||
int[] nums = new int[10000];
|
||||
ListNode node = new ListNode(0);
|
||||
// Variables in a loop occupy O(1) space
|
||||
// Variables in the loop occupy O(1) space
|
||||
for (int i = 0; i < n; i++) {
|
||||
int c = 0;
|
||||
}
|
||||
// Functions in a loop occupy O(1) space
|
||||
// Functions in the loop occupy O(1) space
|
||||
for (int i = 0; i < n; i++) {
|
||||
function();
|
||||
}
|
||||
}
|
||||
|
||||
/* Linear complexity */
|
||||
/* Linear order */
|
||||
static void linear(int n) {
|
||||
// Array of length n occupies O(n) space
|
||||
// Array of length n uses O(n) space
|
||||
int[] nums = new int[n];
|
||||
// A list of length n occupies O(n) space
|
||||
List<ListNode> nodes = new ArrayList<>();
|
||||
@@ -49,7 +49,7 @@ public class space_complexity {
|
||||
}
|
||||
}
|
||||
|
||||
/* Linear complexity (recursive implementation) */
|
||||
/* Linear order (recursive implementation) */
|
||||
static void linearRecur(int n) {
|
||||
System.out.println("Recursion n = " + n);
|
||||
if (n == 1)
|
||||
@@ -57,11 +57,11 @@ public class space_complexity {
|
||||
linearRecur(n - 1);
|
||||
}
|
||||
|
||||
/* Quadratic complexity */
|
||||
/* Exponential order */
|
||||
static void quadratic(int n) {
|
||||
// Matrix occupies O(n^2) space
|
||||
// Matrix uses O(n^2) space
|
||||
int[][] numMatrix = new int[n][n];
|
||||
// A two-dimensional list occupies O(n^2) space
|
||||
// 2D list uses O(n^2) space
|
||||
List<List<Integer>> numList = new ArrayList<>();
|
||||
for (int i = 0; i < n; i++) {
|
||||
List<Integer> tmp = new ArrayList<>();
|
||||
@@ -72,17 +72,17 @@ public class space_complexity {
|
||||
}
|
||||
}
|
||||
|
||||
/* Quadratic complexity (recursive implementation) */
|
||||
/* Quadratic order (recursive implementation) */
|
||||
static int quadraticRecur(int n) {
|
||||
if (n <= 0)
|
||||
return 0;
|
||||
// Array nums length = n, n-1, ..., 2, 1
|
||||
// Array nums has length n, n-1, ..., 2, 1
|
||||
int[] nums = new int[n];
|
||||
System.out.println("Recursion n = " + n + " in the length of nums = " + nums.length);
|
||||
System.out.println("In recursion n = " + n + ", nums length = " + nums.length);
|
||||
return quadraticRecur(n - 1);
|
||||
}
|
||||
|
||||
/* Exponential complexity (building a full binary tree) */
|
||||
/* Driver Code */
|
||||
static TreeNode buildTree(int n) {
|
||||
if (n == 0)
|
||||
return null;
|
||||
@@ -95,15 +95,15 @@ public class space_complexity {
|
||||
/* Driver Code */
|
||||
public static void main(String[] args) {
|
||||
int n = 5;
|
||||
// Constant complexity
|
||||
// Constant order
|
||||
constant(n);
|
||||
// Linear complexity
|
||||
// Linear order
|
||||
linear(n);
|
||||
linearRecur(n);
|
||||
// Quadratic complexity
|
||||
// Exponential order
|
||||
quadratic(n);
|
||||
quadraticRecur(n);
|
||||
// Exponential complexity
|
||||
// Exponential order
|
||||
TreeNode root = buildTree(n);
|
||||
PrintUtil.printTree(root);
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
package chapter_computational_complexity;
|
||||
|
||||
public class time_complexity {
|
||||
/* Constant complexity */
|
||||
/* Constant order */
|
||||
static int constant(int n) {
|
||||
int count = 0;
|
||||
int size = 100000;
|
||||
@@ -16,7 +16,7 @@ public class time_complexity {
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Linear complexity */
|
||||
/* Linear order */
|
||||
static int linear(int n) {
|
||||
int count = 0;
|
||||
for (int i = 0; i < n; i++)
|
||||
@@ -24,20 +24,20 @@ public class time_complexity {
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Linear complexity (traversing an array) */
|
||||
/* Linear order (traversing array) */
|
||||
static int arrayTraversal(int[] nums) {
|
||||
int count = 0;
|
||||
// Loop count is proportional to the length of the array
|
||||
// Number of iterations is proportional to the array length
|
||||
for (int num : nums) {
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Quadratic complexity */
|
||||
/* Exponential order */
|
||||
static int quadratic(int n) {
|
||||
int count = 0;
|
||||
// Loop count is squared in relation to the data size n
|
||||
// Number of iterations is quadratically related to the data size n
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
count++;
|
||||
@@ -46,29 +46,29 @@ public class time_complexity {
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Quadratic complexity (bubble sort) */
|
||||
/* Quadratic order (bubble sort) */
|
||||
static int bubbleSort(int[] nums) {
|
||||
int count = 0; // Counter
|
||||
// Outer loop: unsorted range is [0, i]
|
||||
for (int i = nums.length - 1; i > 0; i--) {
|
||||
// Inner loop: swap the largest element in the unsorted range [0, i] to the right end of the range
|
||||
// Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range
|
||||
for (int j = 0; j < i; j++) {
|
||||
if (nums[j] > nums[j + 1]) {
|
||||
// Swap nums[j] and nums[j + 1]
|
||||
int tmp = nums[j];
|
||||
nums[j] = nums[j + 1];
|
||||
nums[j + 1] = tmp;
|
||||
count += 3; // Element swap includes 3 individual operations
|
||||
count += 3; // Element swap includes 3 unit operations
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Exponential complexity (loop implementation) */
|
||||
/* Exponential order (loop implementation) */
|
||||
static int exponential(int n) {
|
||||
int count = 0, base = 1;
|
||||
// Cells split into two every round, forming the sequence 1, 2, 4, 8, ..., 2^(n-1)
|
||||
// Cells divide into two every round, forming sequence 1, 2, 4, 8, ..., 2^(n-1)
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < base; j++) {
|
||||
count++;
|
||||
@@ -79,14 +79,14 @@ public class time_complexity {
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Exponential complexity (recursive implementation) */
|
||||
/* Exponential order (recursive implementation) */
|
||||
static int expRecur(int n) {
|
||||
if (n == 1)
|
||||
return 1;
|
||||
return expRecur(n - 1) + expRecur(n - 1) + 1;
|
||||
}
|
||||
|
||||
/* Logarithmic complexity (loop implementation) */
|
||||
/* Logarithmic order (loop implementation) */
|
||||
static int logarithmic(int n) {
|
||||
int count = 0;
|
||||
while (n > 1) {
|
||||
@@ -96,14 +96,14 @@ public class time_complexity {
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Logarithmic complexity (recursive implementation) */
|
||||
/* Logarithmic order (recursive implementation) */
|
||||
static int logRecur(int n) {
|
||||
if (n <= 1)
|
||||
return 0;
|
||||
return logRecur(n / 2) + 1;
|
||||
}
|
||||
|
||||
/* Linear logarithmic complexity */
|
||||
/* Linearithmic order */
|
||||
static int linearLogRecur(int n) {
|
||||
if (n <= 1)
|
||||
return 1;
|
||||
@@ -114,12 +114,12 @@ public class time_complexity {
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Factorial complexity (recursive implementation) */
|
||||
/* Factorial order (recursive implementation) */
|
||||
static int factorialRecur(int n) {
|
||||
if (n == 0)
|
||||
return 1;
|
||||
int count = 0;
|
||||
// From 1 split into n
|
||||
// Split from 1 into n
|
||||
for (int i = 0; i < n; i++) {
|
||||
count += factorialRecur(n - 1);
|
||||
}
|
||||
@@ -128,40 +128,40 @@ public class time_complexity {
|
||||
|
||||
/* Driver Code */
|
||||
public static void main(String[] args) {
|
||||
// Can modify n to experience the trend of operation count changes under various complexities
|
||||
// You can modify n to run and observe the trend of the number of operations for various complexities
|
||||
int n = 8;
|
||||
System.out.println("Input data size n = " + n);
|
||||
|
||||
int count = constant(n);
|
||||
System.out.println("Number of constant complexity operations = " + count);
|
||||
System.out.println("Constant order operation count = " + count);
|
||||
|
||||
count = linear(n);
|
||||
System.out.println("Number of linear complexity operations = " + count);
|
||||
System.out.println("Linear order operation count = " + count);
|
||||
count = arrayTraversal(new int[n]);
|
||||
System.out.println("Number of linear complexity operations (traversing the array) = " + count);
|
||||
System.out.println("Linear order (array traversal) operation count = " + count);
|
||||
|
||||
count = quadratic(n);
|
||||
System.out.println("Number of quadratic order operations = " + count);
|
||||
System.out.println("Quadratic order operation count = " + count);
|
||||
int[] nums = new int[n];
|
||||
for (int i = 0; i < n; i++)
|
||||
nums[i] = n - i; // [n,n-1,...,2,1]
|
||||
count = bubbleSort(nums);
|
||||
System.out.println("Number of quadratic order operations (bubble sort) = " + count);
|
||||
System.out.println("Quadratic order (bubble sort) operation count = " + count);
|
||||
|
||||
count = exponential(n);
|
||||
System.out.println("Number of exponential complexity operations (implemented by loop) = " + count);
|
||||
System.out.println("Exponential order (loop implementation) operation count = " + count);
|
||||
count = expRecur(n);
|
||||
System.out.println("Number of exponential complexity operations (implemented by recursion) = " + count);
|
||||
System.out.println("Exponential order (recursive implementation) operation count = " + count);
|
||||
|
||||
count = logarithmic(n);
|
||||
System.out.println("Number of logarithmic complexity operations (implemented by loop) = " + count);
|
||||
System.out.println("Logarithmic order (loop implementation) operation count = " + count);
|
||||
count = logRecur(n);
|
||||
System.out.println("Number of logarithmic complexity operations (implemented by recursion) = " + count);
|
||||
System.out.println("Logarithmic order (recursive implementation) operation count = " + count);
|
||||
|
||||
count = linearLogRecur(n);
|
||||
System.out.println("Number of linear logarithmic complexity operations (implemented by recursion) = " + count);
|
||||
System.out.println("Linearithmic order (recursive implementation) operation count = " + count);
|
||||
|
||||
count = factorialRecur(n);
|
||||
System.out.println("Number of factorial complexity operations (implemented by recursion) = " + count);
|
||||
System.out.println("Factorial order (recursive implementation) operation count = " + count);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ package chapter_computational_complexity;
|
||||
import java.util.*;
|
||||
|
||||
public class worst_best_time_complexity {
|
||||
/* Generate an array with elements {1, 2, ..., n} in a randomly shuffled order */
|
||||
/* Generate an array with elements { 1, 2, ..., n }, order shuffled */
|
||||
static int[] randomNumbers(int n) {
|
||||
Integer[] nums = new Integer[n];
|
||||
// Generate array nums = { 1, 2, 3, ..., n }
|
||||
@@ -29,8 +29,8 @@ public class worst_best_time_complexity {
|
||||
/* Find the index of number 1 in array nums */
|
||||
static int findOne(int[] nums) {
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
// When element 1 is at the start of the array, achieve best time complexity O(1)
|
||||
// When element 1 is at the end of the array, achieve worst time complexity O(n)
|
||||
// When element 1 is at the head of the array, best time complexity O(1) is achieved
|
||||
// When element 1 is at the tail of the array, worst time complexity O(n) is achieved
|
||||
if (nums[i] == 1)
|
||||
return i;
|
||||
}
|
||||
@@ -43,8 +43,8 @@ public class worst_best_time_complexity {
|
||||
int n = 100;
|
||||
int[] nums = randomNumbers(n);
|
||||
int index = findOne(nums);
|
||||
System.out.println("\nThe array [ 1, 2, ..., n ] after being shuffled = " + Arrays.toString(nums));
|
||||
System.out.println("The index of number 1 is " + index);
|
||||
System.out.println("\nArray [ 1, 2, ..., n ] after shuffling = " + Arrays.toString(nums));
|
||||
System.out.println("Index of number 1 is " + index);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,20 +9,20 @@ package chapter_divide_and_conquer;
|
||||
public class binary_search_recur {
|
||||
/* Binary search: problem f(i, j) */
|
||||
static int dfs(int[] nums, int target, int i, int j) {
|
||||
// If the interval is empty, indicating no target element, return -1
|
||||
// If the interval is empty, it means there is no target element, return -1
|
||||
if (i > j) {
|
||||
return -1;
|
||||
}
|
||||
// Calculate midpoint index m
|
||||
int m = i + (j - i) / 2;
|
||||
// Calculate the midpoint index m
|
||||
int m = (i + j) / 2;
|
||||
if (nums[m] < target) {
|
||||
// Recursive subproblem f(m+1, j)
|
||||
// Recursion subproblem f(m+1, j)
|
||||
return dfs(nums, target, m + 1, j);
|
||||
} else if (nums[m] > target) {
|
||||
// Recursive subproblem f(i, m-1)
|
||||
// Recursion subproblem f(i, m-1)
|
||||
return dfs(nums, target, i, m - 1);
|
||||
} else {
|
||||
// Found the target element, thus return its index
|
||||
// Found the target element, return its index
|
||||
return m;
|
||||
}
|
||||
}
|
||||
@@ -30,7 +30,7 @@ public class binary_search_recur {
|
||||
/* Binary search */
|
||||
static int binarySearch(int[] nums, int target) {
|
||||
int n = nums.length;
|
||||
// Solve problem f(0, n-1)
|
||||
// Solve the problem f(0, n-1)
|
||||
return dfs(nums, target, 0, n - 1);
|
||||
}
|
||||
|
||||
@@ -38,8 +38,8 @@ public class binary_search_recur {
|
||||
int target = 6;
|
||||
int[] nums = { 1, 3, 6, 8, 12, 15, 23, 26, 31, 35 };
|
||||
|
||||
// Binary search (double closed interval)
|
||||
// Binary search (closed interval on both sides)
|
||||
int index = binarySearch(nums, target);
|
||||
System.out.println("Index of target element 6 =" + index);
|
||||
System.out.println("Index of target element 6 = " + index);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,26 +10,26 @@ import utils.*;
|
||||
import java.util.*;
|
||||
|
||||
public class build_tree {
|
||||
/* Build binary tree: Divide and conquer */
|
||||
/* Build binary tree: divide and conquer */
|
||||
static TreeNode dfs(int[] preorder, Map<Integer, Integer> inorderMap, int i, int l, int r) {
|
||||
// Terminate when subtree interval is empty
|
||||
// Terminate when the subtree interval is empty
|
||||
if (r - l < 0)
|
||||
return null;
|
||||
// Initialize root node
|
||||
// Initialize the root node
|
||||
TreeNode root = new TreeNode(preorder[i]);
|
||||
// Query m to divide left and right subtrees
|
||||
// Query m to divide the left and right subtrees
|
||||
int m = inorderMap.get(preorder[i]);
|
||||
// Subproblem: build left subtree
|
||||
// Subproblem: build the left subtree
|
||||
root.left = dfs(preorder, inorderMap, i + 1, l, m - 1);
|
||||
// Subproblem: build right subtree
|
||||
// Subproblem: build the right subtree
|
||||
root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);
|
||||
// Return root node
|
||||
// Return the root node
|
||||
return root;
|
||||
}
|
||||
|
||||
/* Build binary tree */
|
||||
static TreeNode buildTree(int[] preorder, int[] inorder) {
|
||||
// Initialize hash table, storing in-order elements to indices mapping
|
||||
// Initialize hash map, storing the mapping from inorder elements to indices
|
||||
Map<Integer, Integer> inorderMap = new HashMap<>();
|
||||
for (int i = 0; i < inorder.length; i++) {
|
||||
inorderMap.put(inorder[i], i);
|
||||
@@ -41,11 +41,11 @@ public class build_tree {
|
||||
public static void main(String[] args) {
|
||||
int[] preorder = { 3, 9, 2, 1, 7 };
|
||||
int[] inorder = { 9, 3, 1, 2, 7 };
|
||||
System.out.println("Pre-order traversal = " + Arrays.toString(preorder));
|
||||
System.out.println("In-order traversal = " + Arrays.toString(inorder));
|
||||
System.out.println("Preorder traversal = " + Arrays.toString(preorder));
|
||||
System.out.println("Inorder traversal = " + Arrays.toString(inorder));
|
||||
|
||||
TreeNode root = buildTree(preorder, inorder);
|
||||
System.out.println("The built binary tree is:");
|
||||
System.out.println("The constructed binary tree is:");
|
||||
PrintUtil.printTree(root);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,49 +9,49 @@ package chapter_divide_and_conquer;
|
||||
import java.util.*;
|
||||
|
||||
public class hanota {
|
||||
/* Move a disc */
|
||||
/* Move a disk */
|
||||
static void move(List<Integer> src, List<Integer> tar) {
|
||||
// Take out a disc from the top of src
|
||||
// Take out a disk from the top of src
|
||||
Integer pan = src.remove(src.size() - 1);
|
||||
// Place the disc on top of tar
|
||||
// Place the disk on top of tar
|
||||
tar.add(pan);
|
||||
}
|
||||
|
||||
/* Solve the Tower of Hanoi problem f(i) */
|
||||
static void dfs(int i, List<Integer> src, List<Integer> buf, List<Integer> tar) {
|
||||
// If only one disc remains on src, move it to tar
|
||||
// If there is only one disk left in src, move it directly to tar
|
||||
if (i == 1) {
|
||||
move(src, tar);
|
||||
return;
|
||||
}
|
||||
// Subproblem f(i-1): move the top i-1 discs from src with the help of tar to buf
|
||||
// Subproblem f(i-1): move the top i-1 disks from src to buf using tar
|
||||
dfs(i - 1, src, tar, buf);
|
||||
// Subproblem f(1): move the remaining one disc from src to tar
|
||||
// Subproblem f(1): move the remaining disk from src to tar
|
||||
move(src, tar);
|
||||
// Subproblem f(i-1): move the top i-1 discs from buf with the help of src to tar
|
||||
// Subproblem f(i-1): move the top i-1 disks from buf to tar using src
|
||||
dfs(i - 1, buf, src, tar);
|
||||
}
|
||||
|
||||
/* Solve the Tower of Hanoi problem */
|
||||
static void solveHanota(List<Integer> A, List<Integer> B, List<Integer> C) {
|
||||
int n = A.size();
|
||||
// Move the top n discs from A with the help of B to C
|
||||
// Move the top n disks from A to C using B
|
||||
dfs(n, A, B, C);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
// The tail of the list is the top of the pillar
|
||||
// The tail of the list is the top of the rod
|
||||
List<Integer> A = new ArrayList<>(Arrays.asList(5, 4, 3, 2, 1));
|
||||
List<Integer> B = new ArrayList<>();
|
||||
List<Integer> C = new ArrayList<>();
|
||||
System.out.println("Initial state:");
|
||||
System.out.println("In initial state:");
|
||||
System.out.println("A = " + A);
|
||||
System.out.println("B = " + B);
|
||||
System.out.println("C = " + C);
|
||||
|
||||
solveHanota(A, B, C);
|
||||
|
||||
System.out.println("After the discs are moved:");
|
||||
System.out.println("After disk movement is complete:");
|
||||
System.out.println("A = " + A);
|
||||
System.out.println("B = " + B);
|
||||
System.out.println("C = " + C);
|
||||
|
||||
@@ -11,26 +11,26 @@ import java.util.*;
|
||||
public class climbing_stairs_backtrack {
|
||||
/* Backtracking */
|
||||
public static void backtrack(List<Integer> choices, int state, int n, List<Integer> res) {
|
||||
// When climbing to the nth step, add 1 to the number of solutions
|
||||
// When climbing to the n-th stair, add 1 to the solution count
|
||||
if (state == n)
|
||||
res.set(0, res.get(0) + 1);
|
||||
// Traverse all choices
|
||||
for (Integer choice : choices) {
|
||||
// Pruning: do not allow climbing beyond the nth step
|
||||
// Pruning: not allowed to go beyond the n-th stair
|
||||
if (state + choice > n)
|
||||
continue;
|
||||
// Attempt: make a choice, update the state
|
||||
// Attempt: make choice, update state
|
||||
backtrack(choices, state + choice, n, res);
|
||||
// Retract
|
||||
// Backtrack
|
||||
}
|
||||
}
|
||||
|
||||
/* Climbing stairs: Backtracking */
|
||||
public static int climbingStairsBacktrack(int n) {
|
||||
List<Integer> choices = Arrays.asList(1, 2); // Can choose to climb up 1 step or 2 steps
|
||||
int state = 0; // Start climbing from the 0th step
|
||||
List<Integer> choices = Arrays.asList(1, 2); // Can choose to climb up 1 or 2 stairs
|
||||
int state = 0; // Start climbing from the 0-th stair
|
||||
List<Integer> res = new ArrayList<>();
|
||||
res.add(0); // Use res[0] to record the number of solutions
|
||||
res.add(0); // Use res[0] to record the solution count
|
||||
backtrack(choices, state, n, res);
|
||||
return res.get(0);
|
||||
}
|
||||
@@ -39,6 +39,6 @@ public class climbing_stairs_backtrack {
|
||||
int n = 9;
|
||||
|
||||
int res = climbingStairsBacktrack(n);
|
||||
System.out.println(String.format("There are %d solutions to climb %d stairs", res, n));
|
||||
System.out.println(String.format("Climbing %d stairs has %d solutions", n, res));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,14 +7,14 @@
|
||||
package chapter_dynamic_programming;
|
||||
|
||||
public class climbing_stairs_constraint_dp {
|
||||
/* Constrained climbing stairs: Dynamic programming */
|
||||
/* Climbing stairs with constraint: Dynamic programming */
|
||||
static int climbingStairsConstraintDP(int n) {
|
||||
if (n == 1 || n == 2) {
|
||||
return 1;
|
||||
}
|
||||
// Initialize dp table, used to store subproblem solutions
|
||||
// Initialize dp table, used to store solutions to subproblems
|
||||
int[][] dp = new int[n + 1][3];
|
||||
// Initial state: preset the smallest subproblem solution
|
||||
// Initial state: preset the solution to the smallest subproblem
|
||||
dp[1][1] = 1;
|
||||
dp[1][2] = 0;
|
||||
dp[2][1] = 0;
|
||||
@@ -31,6 +31,6 @@ public class climbing_stairs_constraint_dp {
|
||||
int n = 9;
|
||||
|
||||
int res = climbingStairsConstraintDP(n);
|
||||
System.out.println(String.format("There are %d solutions to climb %d stairs", res, n));
|
||||
System.out.println(String.format("Climbing %d stairs has %d solutions", n, res));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,6 +26,6 @@ public class climbing_stairs_dfs {
|
||||
int n = 9;
|
||||
|
||||
int res = climbingStairsDFS(n);
|
||||
System.out.println(String.format("There are %d solutions to climb %d stairs", res, n));
|
||||
System.out.println(String.format("Climbing %d stairs has %d solutions", n, res));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,12 +9,12 @@ package chapter_dynamic_programming;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class climbing_stairs_dfs_mem {
|
||||
/* Memoized search */
|
||||
/* Memoization search */
|
||||
public static int dfs(int i, int[] mem) {
|
||||
// Known dp[1] and dp[2], return them
|
||||
if (i == 1 || i == 2)
|
||||
return i;
|
||||
// If there is a record for dp[i], return it
|
||||
// If record dp[i] exists, return it directly
|
||||
if (mem[i] != -1)
|
||||
return mem[i];
|
||||
// dp[i] = dp[i-1] + dp[i-2]
|
||||
@@ -24,9 +24,9 @@ public class climbing_stairs_dfs_mem {
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Climbing stairs: Memoized search */
|
||||
/* Climbing stairs: Memoization search */
|
||||
public static int climbingStairsDFSMem(int n) {
|
||||
// mem[i] records the total number of solutions for climbing to the ith step, -1 means no record
|
||||
// mem[i] records the total number of solutions to climb to the i-th stair, -1 means no record
|
||||
int[] mem = new int[n + 1];
|
||||
Arrays.fill(mem, -1);
|
||||
return dfs(n, mem);
|
||||
@@ -36,6 +36,6 @@ public class climbing_stairs_dfs_mem {
|
||||
int n = 9;
|
||||
|
||||
int res = climbingStairsDFSMem(n);
|
||||
System.out.println(String.format("There are %d solutions to climb %d stairs", res, n));
|
||||
System.out.println(String.format("Climbing %d stairs has %d solutions", n, res));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,9 +11,9 @@ public class climbing_stairs_dp {
|
||||
public static int climbingStairsDP(int n) {
|
||||
if (n == 1 || n == 2)
|
||||
return n;
|
||||
// Initialize dp table, used to store subproblem solutions
|
||||
// Initialize dp table, used to store solutions to subproblems
|
||||
int[] dp = new int[n + 1];
|
||||
// Initial state: preset the smallest subproblem solution
|
||||
// Initial state: preset the solution to the smallest subproblem
|
||||
dp[1] = 1;
|
||||
dp[2] = 2;
|
||||
// State transition: gradually solve larger subproblems from smaller ones
|
||||
@@ -40,9 +40,9 @@ public class climbing_stairs_dp {
|
||||
int n = 9;
|
||||
|
||||
int res = climbingStairsDP(n);
|
||||
System.out.println(String.format("There are %d solutions to climb %d stairs", res, n));
|
||||
System.out.println(String.format("Climbing %d stairs has %d solutions", n, res));
|
||||
|
||||
res = climbingStairsDPComp(n);
|
||||
System.out.println(String.format("There are %d solutions to climb %d stairs", res, n));
|
||||
System.out.println(String.format("Climbing %d stairs has %d solutions", n, res));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,14 +19,14 @@ public class coin_change {
|
||||
for (int a = 1; a <= amt; a++) {
|
||||
dp[0][a] = MAX;
|
||||
}
|
||||
// State transition: the rest of the rows and columns
|
||||
// State transition: rest of the rows and columns
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int a = 1; a <= amt; a++) {
|
||||
if (coins[i - 1] > a) {
|
||||
// If exceeding the target amount, do not choose coin i
|
||||
// If exceeds target amount, don't select coin i
|
||||
dp[i][a] = dp[i - 1][a];
|
||||
} else {
|
||||
// The smaller value between not choosing and choosing coin i
|
||||
// The smaller value between not selecting and selecting coin i
|
||||
dp[i][a] = Math.min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);
|
||||
}
|
||||
}
|
||||
@@ -46,10 +46,10 @@ public class coin_change {
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int a = 1; a <= amt; a++) {
|
||||
if (coins[i - 1] > a) {
|
||||
// If exceeding the target amount, do not choose coin i
|
||||
// If exceeds target amount, don't select coin i
|
||||
dp[a] = dp[a];
|
||||
} else {
|
||||
// The smaller value between not choosing and choosing coin i
|
||||
// The smaller value between not selecting and selecting coin i
|
||||
dp[a] = Math.min(dp[a], dp[a - coins[i - 1]] + 1);
|
||||
}
|
||||
}
|
||||
@@ -63,10 +63,10 @@ public class coin_change {
|
||||
|
||||
// Dynamic programming
|
||||
int res = coinChangeDP(coins, amt);
|
||||
System.out.println("The minimum number of coins required to make up the target amount is " + res);
|
||||
System.out.println("Minimum number of coins needed to make target amount is " + res);
|
||||
|
||||
// Space-optimized dynamic programming
|
||||
res = coinChangeDPComp(coins, amt);
|
||||
System.out.println("The minimum number of coins required to make up the target amount is " + res);
|
||||
System.out.println("Minimum number of coins needed to make target amount is " + res);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,10 +20,10 @@ public class coin_change_ii {
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int a = 1; a <= amt; a++) {
|
||||
if (coins[i - 1] > a) {
|
||||
// If exceeding the target amount, do not choose coin i
|
||||
// If exceeds target amount, don't select coin i
|
||||
dp[i][a] = dp[i - 1][a];
|
||||
} else {
|
||||
// The sum of the two options of not choosing and choosing coin i
|
||||
// Sum of the two options: not selecting and selecting coin i
|
||||
dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];
|
||||
}
|
||||
}
|
||||
@@ -41,10 +41,10 @@ public class coin_change_ii {
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int a = 1; a <= amt; a++) {
|
||||
if (coins[i - 1] > a) {
|
||||
// If exceeding the target amount, do not choose coin i
|
||||
// If exceeds target amount, don't select coin i
|
||||
dp[a] = dp[a];
|
||||
} else {
|
||||
// The sum of the two options of not choosing and choosing coin i
|
||||
// Sum of the two options: not selecting and selecting coin i
|
||||
dp[a] = dp[a] + dp[a - coins[i - 1]];
|
||||
}
|
||||
}
|
||||
@@ -58,10 +58,10 @@ public class coin_change_ii {
|
||||
|
||||
// Dynamic programming
|
||||
int res = coinChangeIIDP(coins, amt);
|
||||
System.out.println("The number of coin combinations to make up the target amount is " + res);
|
||||
System.out.println("Number of coin combinations to make target amount is " + res);
|
||||
|
||||
// Space-optimized dynamic programming
|
||||
res = coinChangeIIDPComp(coins, amt);
|
||||
System.out.println("The number of coin combinations to make up the target amount is " + res);
|
||||
System.out.println("Number of coin combinations to make target amount is " + res);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,50 +9,50 @@ package chapter_dynamic_programming;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class edit_distance {
|
||||
/* Edit distance: Brute force search */
|
||||
/* Edit distance: Brute-force search */
|
||||
static int editDistanceDFS(String s, String t, int i, int j) {
|
||||
// If both s and t are empty, return 0
|
||||
if (i == 0 && j == 0)
|
||||
return 0;
|
||||
// If s is empty, return the length of t
|
||||
// If s is empty, return length of t
|
||||
if (i == 0)
|
||||
return j;
|
||||
// If t is empty, return the length of s
|
||||
// If t is empty, return length of s
|
||||
if (j == 0)
|
||||
return i;
|
||||
// If the two characters are equal, skip these two characters
|
||||
// If two characters are equal, skip both characters
|
||||
if (s.charAt(i - 1) == t.charAt(j - 1))
|
||||
return editDistanceDFS(s, t, i - 1, j - 1);
|
||||
// The minimum number of edits = the minimum number of edits from three operations (insert, remove, replace) + 1
|
||||
// Minimum edit steps = minimum edit steps of insert, delete, replace + 1
|
||||
int insert = editDistanceDFS(s, t, i, j - 1);
|
||||
int delete = editDistanceDFS(s, t, i - 1, j);
|
||||
int replace = editDistanceDFS(s, t, i - 1, j - 1);
|
||||
// Return the minimum number of edits
|
||||
// Return minimum edit steps
|
||||
return Math.min(Math.min(insert, delete), replace) + 1;
|
||||
}
|
||||
|
||||
/* Edit distance: Memoized search */
|
||||
/* Edit distance: Memoization search */
|
||||
static int editDistanceDFSMem(String s, String t, int[][] mem, int i, int j) {
|
||||
// If both s and t are empty, return 0
|
||||
if (i == 0 && j == 0)
|
||||
return 0;
|
||||
// If s is empty, return the length of t
|
||||
// If s is empty, return length of t
|
||||
if (i == 0)
|
||||
return j;
|
||||
// If t is empty, return the length of s
|
||||
// If t is empty, return length of s
|
||||
if (j == 0)
|
||||
return i;
|
||||
// If there is a record, return it
|
||||
// If there's a record, return it directly
|
||||
if (mem[i][j] != -1)
|
||||
return mem[i][j];
|
||||
// If the two characters are equal, skip these two characters
|
||||
// If two characters are equal, skip both characters
|
||||
if (s.charAt(i - 1) == t.charAt(j - 1))
|
||||
return editDistanceDFSMem(s, t, mem, i - 1, j - 1);
|
||||
// The minimum number of edits = the minimum number of edits from three operations (insert, remove, replace) + 1
|
||||
// Minimum edit steps = minimum edit steps of insert, delete, replace + 1
|
||||
int insert = editDistanceDFSMem(s, t, mem, i, j - 1);
|
||||
int delete = editDistanceDFSMem(s, t, mem, i - 1, j);
|
||||
int replace = editDistanceDFSMem(s, t, mem, i - 1, j - 1);
|
||||
// Record and return the minimum number of edits
|
||||
// Record and return minimum edit steps
|
||||
mem[i][j] = Math.min(Math.min(insert, delete), replace) + 1;
|
||||
return mem[i][j];
|
||||
}
|
||||
@@ -68,14 +68,14 @@ public class edit_distance {
|
||||
for (int j = 1; j <= m; j++) {
|
||||
dp[0][j] = j;
|
||||
}
|
||||
// State transition: the rest of the rows and columns
|
||||
// State transition: rest of the rows and columns
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int j = 1; j <= m; j++) {
|
||||
if (s.charAt(i - 1) == t.charAt(j - 1)) {
|
||||
// If the two characters are equal, skip these two characters
|
||||
// If two characters are equal, skip both characters
|
||||
dp[i][j] = dp[i - 1][j - 1];
|
||||
} else {
|
||||
// The minimum number of edits = the minimum number of edits from three operations (insert, remove, replace) + 1
|
||||
// Minimum edit steps = minimum edit steps of insert, delete, replace + 1
|
||||
dp[i][j] = Math.min(Math.min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;
|
||||
}
|
||||
}
|
||||
@@ -91,22 +91,22 @@ public class edit_distance {
|
||||
for (int j = 1; j <= m; j++) {
|
||||
dp[j] = j;
|
||||
}
|
||||
// State transition: the rest of the rows
|
||||
// State transition: rest of the rows
|
||||
for (int i = 1; i <= n; i++) {
|
||||
// State transition: first column
|
||||
int leftup = dp[0]; // Temporarily store dp[i-1, j-1]
|
||||
dp[0] = i;
|
||||
// State transition: the rest of the columns
|
||||
// State transition: rest of the columns
|
||||
for (int j = 1; j <= m; j++) {
|
||||
int temp = dp[j];
|
||||
if (s.charAt(i - 1) == t.charAt(j - 1)) {
|
||||
// If the two characters are equal, skip these two characters
|
||||
// If two characters are equal, skip both characters
|
||||
dp[j] = leftup;
|
||||
} else {
|
||||
// The minimum number of edits = the minimum number of edits from three operations (insert, remove, replace) + 1
|
||||
// Minimum edit steps = minimum edit steps of insert, delete, replace + 1
|
||||
dp[j] = Math.min(Math.min(dp[j - 1], dp[j]), leftup) + 1;
|
||||
}
|
||||
leftup = temp; // Update for the next round of dp[i-1, j-1]
|
||||
leftup = temp; // Update for next round's dp[i-1, j-1]
|
||||
}
|
||||
}
|
||||
return dp[m];
|
||||
@@ -117,11 +117,11 @@ public class edit_distance {
|
||||
String t = "pack";
|
||||
int n = s.length(), m = t.length();
|
||||
|
||||
// Brute force search
|
||||
// Brute-force search
|
||||
int res = editDistanceDFS(s, t, n, m);
|
||||
System.out.println("Changing " + s + " to " + t + " requires a minimum of " + res + " edits");
|
||||
|
||||
// Memoized search
|
||||
// Memoization search
|
||||
int[][] mem = new int[n + 1][m + 1];
|
||||
for (int[] row : mem)
|
||||
Arrays.fill(row, -1);
|
||||
|
||||
@@ -10,46 +10,46 @@ import java.util.Arrays;
|
||||
|
||||
public class knapsack {
|
||||
|
||||
/* 0-1 Knapsack: Brute force search */
|
||||
/* 0-1 knapsack: Brute-force search */
|
||||
static int knapsackDFS(int[] wgt, int[] val, int i, int c) {
|
||||
// If all items have been chosen or the knapsack has no remaining capacity, return value 0
|
||||
// If all items have been selected or knapsack has no remaining capacity, return value 0
|
||||
if (i == 0 || c == 0) {
|
||||
return 0;
|
||||
}
|
||||
// If exceeding the knapsack capacity, can only choose not to put it in the knapsack
|
||||
// If exceeds knapsack capacity, can only choose not to put it in
|
||||
if (wgt[i - 1] > c) {
|
||||
return knapsackDFS(wgt, val, i - 1, c);
|
||||
}
|
||||
// Calculate the maximum value of not putting in and putting in item i
|
||||
int no = knapsackDFS(wgt, val, i - 1, c);
|
||||
int yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];
|
||||
// Return the greater value of the two options
|
||||
// Return the larger value of the two options
|
||||
return Math.max(no, yes);
|
||||
}
|
||||
|
||||
/* 0-1 Knapsack: Memoized search */
|
||||
/* 0-1 knapsack: Memoization search */
|
||||
static int knapsackDFSMem(int[] wgt, int[] val, int[][] mem, int i, int c) {
|
||||
// If all items have been chosen or the knapsack has no remaining capacity, return value 0
|
||||
// If all items have been selected or knapsack has no remaining capacity, return value 0
|
||||
if (i == 0 || c == 0) {
|
||||
return 0;
|
||||
}
|
||||
// If there is a record, return it
|
||||
// If there's a record, return it directly
|
||||
if (mem[i][c] != -1) {
|
||||
return mem[i][c];
|
||||
}
|
||||
// If exceeding the knapsack capacity, can only choose not to put it in the knapsack
|
||||
// If exceeds knapsack capacity, can only choose not to put it in
|
||||
if (wgt[i - 1] > c) {
|
||||
return knapsackDFSMem(wgt, val, mem, i - 1, c);
|
||||
}
|
||||
// Calculate the maximum value of not putting in and putting in item i
|
||||
int no = knapsackDFSMem(wgt, val, mem, i - 1, c);
|
||||
int yes = knapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];
|
||||
// Record and return the greater value of the two options
|
||||
// Record and return the larger value of the two options
|
||||
mem[i][c] = Math.max(no, yes);
|
||||
return mem[i][c];
|
||||
}
|
||||
|
||||
/* 0-1 Knapsack: Dynamic programming */
|
||||
/* 0-1 knapsack: Dynamic programming */
|
||||
static int knapsackDP(int[] wgt, int[] val, int cap) {
|
||||
int n = wgt.length;
|
||||
// Initialize dp table
|
||||
@@ -58,10 +58,10 @@ public class knapsack {
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int c = 1; c <= cap; c++) {
|
||||
if (wgt[i - 1] > c) {
|
||||
// If exceeding the knapsack capacity, do not choose item i
|
||||
// If exceeds knapsack capacity, don't select item i
|
||||
dp[i][c] = dp[i - 1][c];
|
||||
} else {
|
||||
// The greater value between not choosing and choosing item i
|
||||
// The larger value between not selecting and selecting item i
|
||||
dp[i][c] = Math.max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]);
|
||||
}
|
||||
}
|
||||
@@ -69,7 +69,7 @@ public class knapsack {
|
||||
return dp[n][cap];
|
||||
}
|
||||
|
||||
/* 0-1 Knapsack: Space-optimized dynamic programming */
|
||||
/* 0-1 knapsack: Space-optimized dynamic programming */
|
||||
static int knapsackDPComp(int[] wgt, int[] val, int cap) {
|
||||
int n = wgt.length;
|
||||
// Initialize dp table
|
||||
@@ -79,7 +79,7 @@ public class knapsack {
|
||||
// Traverse in reverse order
|
||||
for (int c = cap; c >= 1; c--) {
|
||||
if (wgt[i - 1] <= c) {
|
||||
// The greater value between not choosing and choosing item i
|
||||
// The larger value between not selecting and selecting item i
|
||||
dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);
|
||||
}
|
||||
}
|
||||
@@ -93,24 +93,24 @@ public class knapsack {
|
||||
int cap = 50;
|
||||
int n = wgt.length;
|
||||
|
||||
// Brute force search
|
||||
// Brute-force search
|
||||
int res = knapsackDFS(wgt, val, n, cap);
|
||||
System.out.println("The maximum value within the bag capacity is " + res);
|
||||
System.out.println("Maximum item value not exceeding knapsack capacity is " + res);
|
||||
|
||||
// Memoized search
|
||||
// Memoization search
|
||||
int[][] mem = new int[n + 1][cap + 1];
|
||||
for (int[] row : mem) {
|
||||
Arrays.fill(row, -1);
|
||||
}
|
||||
res = knapsackDFSMem(wgt, val, mem, n, cap);
|
||||
System.out.println("The maximum value within the bag capacity is " + res);
|
||||
System.out.println("Maximum item value not exceeding knapsack capacity is " + res);
|
||||
|
||||
// Dynamic programming
|
||||
res = knapsackDP(wgt, val, cap);
|
||||
System.out.println("The maximum value within the bag capacity is " + res);
|
||||
System.out.println("Maximum item value not exceeding knapsack capacity is " + res);
|
||||
|
||||
// Space-optimized dynamic programming
|
||||
res = knapsackDPComp(wgt, val, cap);
|
||||
System.out.println("The maximum value within the bag capacity is " + res);
|
||||
System.out.println("Maximum item value not exceeding knapsack capacity is " + res);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,14 +9,14 @@ package chapter_dynamic_programming;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class min_cost_climbing_stairs_dp {
|
||||
/* Climbing stairs with minimum cost: Dynamic programming */
|
||||
/* Minimum cost climbing stairs: Dynamic programming */
|
||||
public static int minCostClimbingStairsDP(int[] cost) {
|
||||
int n = cost.length - 1;
|
||||
if (n == 1 || n == 2)
|
||||
return cost[n];
|
||||
// Initialize dp table, used to store subproblem solutions
|
||||
// Initialize dp table, used to store solutions to subproblems
|
||||
int[] dp = new int[n + 1];
|
||||
// Initial state: preset the smallest subproblem solution
|
||||
// Initial state: preset the solution to the smallest subproblem
|
||||
dp[1] = cost[1];
|
||||
dp[2] = cost[2];
|
||||
// State transition: gradually solve larger subproblems from smaller ones
|
||||
@@ -26,7 +26,7 @@ public class min_cost_climbing_stairs_dp {
|
||||
return dp[n];
|
||||
}
|
||||
|
||||
/* Climbing stairs with minimum cost: Space-optimized dynamic programming */
|
||||
/* Minimum cost climbing stairs: Space-optimized dynamic programming */
|
||||
public static int minCostClimbingStairsDPComp(int[] cost) {
|
||||
int n = cost.length - 1;
|
||||
if (n == 1 || n == 2)
|
||||
@@ -42,12 +42,12 @@ public class min_cost_climbing_stairs_dp {
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[] cost = { 0, 1, 10, 1, 1, 1, 10, 1, 1, 10, 1 };
|
||||
System.out.println(String.format("Input the cost list for stairs as %s", Arrays.toString(cost)));
|
||||
System.out.println(String.format("Input staircase cost list is %s", Arrays.toString(cost)));
|
||||
|
||||
int res = minCostClimbingStairsDP(cost);
|
||||
System.out.println(String.format("Minimum cost to climb the stairs %d", res));
|
||||
System.out.println(String.format("Minimum cost to climb staircase is %d", res));
|
||||
|
||||
res = minCostClimbingStairsDPComp(cost);
|
||||
System.out.println(String.format("Minimum cost to climb the stairs %d", res));
|
||||
System.out.println(String.format("Minimum cost to climb staircase is %d", res));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,41 +9,41 @@ package chapter_dynamic_programming;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class min_path_sum {
|
||||
/* Minimum path sum: Brute force search */
|
||||
/* Minimum path sum: Brute-force search */
|
||||
static int minPathSumDFS(int[][] grid, int i, int j) {
|
||||
// If it's the top-left cell, terminate the search
|
||||
if (i == 0 && j == 0) {
|
||||
return grid[0][0];
|
||||
}
|
||||
// If the row or column index is out of bounds, return a +∞ cost
|
||||
// If row or column index is out of bounds, return +∞ cost
|
||||
if (i < 0 || j < 0) {
|
||||
return Integer.MAX_VALUE;
|
||||
}
|
||||
// Calculate the minimum path cost from the top-left to (i-1, j) and (i, j-1)
|
||||
// Calculate the minimum path cost from top-left to (i-1, j) and (i, j-1)
|
||||
int up = minPathSumDFS(grid, i - 1, j);
|
||||
int left = minPathSumDFS(grid, i, j - 1);
|
||||
// Return the minimum path cost from the top-left to (i, j)
|
||||
// Return the minimum path cost from top-left to (i, j)
|
||||
return Math.min(left, up) + grid[i][j];
|
||||
}
|
||||
|
||||
/* Minimum path sum: Memoized search */
|
||||
/* Minimum path sum: Memoization search */
|
||||
static int minPathSumDFSMem(int[][] grid, int[][] mem, int i, int j) {
|
||||
// If it's the top-left cell, terminate the search
|
||||
if (i == 0 && j == 0) {
|
||||
return grid[0][0];
|
||||
}
|
||||
// If the row or column index is out of bounds, return a +∞ cost
|
||||
// If row or column index is out of bounds, return +∞ cost
|
||||
if (i < 0 || j < 0) {
|
||||
return Integer.MAX_VALUE;
|
||||
}
|
||||
// If there is a record, return it
|
||||
// If there's a record, return it directly
|
||||
if (mem[i][j] != -1) {
|
||||
return mem[i][j];
|
||||
}
|
||||
// The minimum path cost from the left and top cells
|
||||
// Minimum path cost for left and upper cells
|
||||
int up = minPathSumDFSMem(grid, mem, i - 1, j);
|
||||
int left = minPathSumDFSMem(grid, mem, i, j - 1);
|
||||
// Record and return the minimum path cost from the top-left to (i, j)
|
||||
// Record and return the minimum path cost from top-left to (i, j)
|
||||
mem[i][j] = Math.min(left, up) + grid[i][j];
|
||||
return mem[i][j];
|
||||
}
|
||||
@@ -62,7 +62,7 @@ public class min_path_sum {
|
||||
for (int i = 1; i < n; i++) {
|
||||
dp[i][0] = dp[i - 1][0] + grid[i][0];
|
||||
}
|
||||
// State transition: the rest of the rows and columns
|
||||
// State transition: rest of the rows and columns
|
||||
for (int i = 1; i < n; i++) {
|
||||
for (int j = 1; j < m; j++) {
|
||||
dp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];
|
||||
@@ -81,11 +81,11 @@ public class min_path_sum {
|
||||
for (int j = 1; j < m; j++) {
|
||||
dp[j] = dp[j - 1] + grid[0][j];
|
||||
}
|
||||
// State transition: the rest of the rows
|
||||
// State transition: rest of the rows
|
||||
for (int i = 1; i < n; i++) {
|
||||
// State transition: first column
|
||||
dp[0] = dp[0] + grid[i][0];
|
||||
// State transition: the rest of the columns
|
||||
// State transition: rest of the columns
|
||||
for (int j = 1; j < m; j++) {
|
||||
dp[j] = Math.min(dp[j - 1], dp[j]) + grid[i][j];
|
||||
}
|
||||
@@ -102,24 +102,24 @@ public class min_path_sum {
|
||||
};
|
||||
int n = grid.length, m = grid[0].length;
|
||||
|
||||
// Brute force search
|
||||
// Brute-force search
|
||||
int res = minPathSumDFS(grid, n - 1, m - 1);
|
||||
System.out.println("The minimum path sum from the top left corner to the bottom right corner is " + res);
|
||||
System.out.println("Minimum path sum from top-left to bottom-right is " + res);
|
||||
|
||||
// Memoized search
|
||||
// Memoization search
|
||||
int[][] mem = new int[n][m];
|
||||
for (int[] row : mem) {
|
||||
Arrays.fill(row, -1);
|
||||
}
|
||||
res = minPathSumDFSMem(grid, mem, n - 1, m - 1);
|
||||
System.out.println("The minimum path sum from the top left corner to the bottom right corner is " + res);
|
||||
System.out.println("Minimum path sum from top-left to bottom-right is " + res);
|
||||
|
||||
// Dynamic programming
|
||||
res = minPathSumDP(grid);
|
||||
System.out.println("The minimum path sum from the top left corner to the bottom right corner is " + res);
|
||||
System.out.println("Minimum path sum from top-left to bottom-right is " + res);
|
||||
|
||||
// Space-optimized dynamic programming
|
||||
res = minPathSumDPComp(grid);
|
||||
System.out.println("The minimum path sum from the top left corner to the bottom right corner is " + res);
|
||||
System.out.println("Minimum path sum from top-left to bottom-right is " + res);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
package chapter_dynamic_programming;
|
||||
|
||||
public class unbounded_knapsack {
|
||||
/* Complete knapsack: Dynamic programming */
|
||||
/* Unbounded knapsack: Dynamic programming */
|
||||
static int unboundedKnapsackDP(int[] wgt, int[] val, int cap) {
|
||||
int n = wgt.length;
|
||||
// Initialize dp table
|
||||
@@ -16,10 +16,10 @@ public class unbounded_knapsack {
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int c = 1; c <= cap; c++) {
|
||||
if (wgt[i - 1] > c) {
|
||||
// If exceeding the knapsack capacity, do not choose item i
|
||||
// If exceeds knapsack capacity, don't select item i
|
||||
dp[i][c] = dp[i - 1][c];
|
||||
} else {
|
||||
// The greater value between not choosing and choosing item i
|
||||
// The larger value between not selecting and selecting item i
|
||||
dp[i][c] = Math.max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]);
|
||||
}
|
||||
}
|
||||
@@ -27,7 +27,7 @@ public class unbounded_knapsack {
|
||||
return dp[n][cap];
|
||||
}
|
||||
|
||||
/* Complete knapsack: Space-optimized dynamic programming */
|
||||
/* Unbounded knapsack: Space-optimized dynamic programming */
|
||||
static int unboundedKnapsackDPComp(int[] wgt, int[] val, int cap) {
|
||||
int n = wgt.length;
|
||||
// Initialize dp table
|
||||
@@ -36,10 +36,10 @@ public class unbounded_knapsack {
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int c = 1; c <= cap; c++) {
|
||||
if (wgt[i - 1] > c) {
|
||||
// If exceeding the knapsack capacity, do not choose item i
|
||||
// If exceeds knapsack capacity, don't select item i
|
||||
dp[c] = dp[c];
|
||||
} else {
|
||||
// The greater value between not choosing and choosing item i
|
||||
// The larger value between not selecting and selecting item i
|
||||
dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);
|
||||
}
|
||||
}
|
||||
@@ -54,10 +54,10 @@ public class unbounded_knapsack {
|
||||
|
||||
// Dynamic programming
|
||||
int res = unboundedKnapsackDP(wgt, val, cap);
|
||||
System.out.println("The maximum value within the bag capacity is " + res);
|
||||
System.out.println("Maximum item value not exceeding knapsack capacity is " + res);
|
||||
|
||||
// Space-optimized dynamic programming
|
||||
res = unboundedKnapsackDPComp(wgt, val, cap);
|
||||
System.out.println("The maximum value within the bag capacity is " + res);
|
||||
System.out.println("Maximum item value not exceeding knapsack capacity is " + res);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,7 +52,7 @@ class GraphAdjList {
|
||||
public void addVertex(Vertex vet) {
|
||||
if (adjList.containsKey(vet))
|
||||
return;
|
||||
// Add a new linked list to the adjacency list
|
||||
// Add a new linked list in the adjacency list
|
||||
adjList.put(vet, new ArrayList<>());
|
||||
}
|
||||
|
||||
@@ -60,15 +60,15 @@ class GraphAdjList {
|
||||
public void removeVertex(Vertex vet) {
|
||||
if (!adjList.containsKey(vet))
|
||||
throw new IllegalArgumentException();
|
||||
// Remove the vertex vet's corresponding linked list from the adjacency list
|
||||
// Remove the linked list corresponding to vertex vet in the adjacency list
|
||||
adjList.remove(vet);
|
||||
// Traverse other vertices' linked lists, removing all edges containing vet
|
||||
// Traverse the linked lists of other vertices and remove all edges containing vet
|
||||
for (List<Vertex> list : adjList.values()) {
|
||||
list.remove(vet);
|
||||
}
|
||||
}
|
||||
|
||||
/* Print the adjacency list */
|
||||
/* Print adjacency list */
|
||||
public void print() {
|
||||
System.out.println("Adjacency list =");
|
||||
for (Map.Entry<Vertex, List<Vertex>> pair : adjList.entrySet()) {
|
||||
@@ -82,36 +82,36 @@ class GraphAdjList {
|
||||
|
||||
public class graph_adjacency_list {
|
||||
public static void main(String[] args) {
|
||||
/* Initialize undirected graph */
|
||||
/* Add edge */
|
||||
Vertex[] v = Vertex.valsToVets(new int[] { 1, 3, 2, 5, 4 });
|
||||
Vertex[][] edges = { { v[0], v[1] }, { v[0], v[3] }, { v[1], v[2] },
|
||||
{ v[2], v[3] }, { v[2], v[4] }, { v[3], v[4] } };
|
||||
GraphAdjList graph = new GraphAdjList(edges);
|
||||
System.out.println("\nAfter initialization, the graph is");
|
||||
System.out.println("\nAfter initialization, graph is");
|
||||
graph.print();
|
||||
|
||||
/* Add edge */
|
||||
// Vertices 1, 2 i.e., v[0], v[2]
|
||||
// Vertices 1, 3 are v[0], v[1]
|
||||
graph.addEdge(v[0], v[2]);
|
||||
System.out.println("\nAfter adding edge 1-2, the graph is");
|
||||
System.out.println("\nAfter adding edge 1-2, graph is");
|
||||
graph.print();
|
||||
|
||||
/* Remove edge */
|
||||
// Vertices 1, 3 i.e., v[0], v[1]
|
||||
// Vertex 3 is v[1]
|
||||
graph.removeEdge(v[0], v[1]);
|
||||
System.out.println("\nAfter removing edge 1-3, the graph is");
|
||||
System.out.println("\nAfter removing edge 1-3, graph is");
|
||||
graph.print();
|
||||
|
||||
/* Add vertex */
|
||||
Vertex v5 = new Vertex(6);
|
||||
graph.addVertex(v5);
|
||||
System.out.println("\nAfter adding vertex 6, the graph is");
|
||||
System.out.println("\nAfter adding vertex 6, graph is");
|
||||
graph.print();
|
||||
|
||||
/* Remove vertex */
|
||||
// Vertex 3 i.e., v[1]
|
||||
// Vertex 3 is v[1]
|
||||
graph.removeVertex(v[1]);
|
||||
System.out.println("\nAfter removing vertex 3, the graph is");
|
||||
System.out.println("\nAfter removing vertex 3, graph is");
|
||||
graph.print();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,8 +11,8 @@ import java.util.*;
|
||||
|
||||
/* Undirected graph class based on adjacency matrix */
|
||||
class GraphAdjMat {
|
||||
List<Integer> vertices; // Vertex list, elements represent "vertex value", index represents "vertex index"
|
||||
List<List<Integer>> adjMat; // Adjacency matrix, row and column indices correspond to "vertex index"
|
||||
List<Integer> vertices; // Vertex list, where the element represents the "vertex value" and the index represents the "vertex index"
|
||||
List<List<Integer>> adjMat; // Adjacency matrix, where the row and column indices correspond to the "vertex index"
|
||||
|
||||
/* Constructor */
|
||||
public GraphAdjMat(int[] vertices, int[][] edges) {
|
||||
@@ -23,7 +23,7 @@ class GraphAdjMat {
|
||||
addVertex(val);
|
||||
}
|
||||
// Add edge
|
||||
// Edges elements represent vertex indices
|
||||
// Note that the edges elements represent vertex indices, i.e., corresponding to the vertices element indices
|
||||
for (int[] e : edges) {
|
||||
addEdge(e[0], e[1]);
|
||||
}
|
||||
@@ -37,7 +37,7 @@ class GraphAdjMat {
|
||||
/* Add vertex */
|
||||
public void addVertex(int val) {
|
||||
int n = size();
|
||||
// Add new vertex value to the vertex list
|
||||
// Add the value of the new vertex to the vertex list
|
||||
vertices.add(val);
|
||||
// Add a row to the adjacency matrix
|
||||
List<Integer> newRow = new ArrayList<>(n);
|
||||
@@ -55,29 +55,29 @@ class GraphAdjMat {
|
||||
public void removeVertex(int index) {
|
||||
if (index >= size())
|
||||
throw new IndexOutOfBoundsException();
|
||||
// Remove vertex at `index` from the vertex list
|
||||
// Remove the vertex at index from the vertex list
|
||||
vertices.remove(index);
|
||||
// Remove the row at `index` from the adjacency matrix
|
||||
// Remove the row at index from the adjacency matrix
|
||||
adjMat.remove(index);
|
||||
// Remove the column at `index` from the adjacency matrix
|
||||
// Remove the column at index from the adjacency matrix
|
||||
for (List<Integer> row : adjMat) {
|
||||
row.remove(index);
|
||||
}
|
||||
}
|
||||
|
||||
/* Add edge */
|
||||
// Parameters i, j correspond to vertices element indices
|
||||
// Parameters i, j correspond to the vertices element indices
|
||||
public void addEdge(int i, int j) {
|
||||
// Handle index out of bounds and equality
|
||||
if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)
|
||||
throw new IndexOutOfBoundsException();
|
||||
// In an undirected graph, the adjacency matrix is symmetric about the main diagonal, i.e., satisfies (i, j) == (j, i)
|
||||
// In an undirected graph, the adjacency matrix is symmetric about the main diagonal, i.e., (i, j) == (j, i)
|
||||
adjMat.get(i).set(j, 1);
|
||||
adjMat.get(j).set(i, 1);
|
||||
}
|
||||
|
||||
/* Remove edge */
|
||||
// Parameters i, j correspond to vertices element indices
|
||||
// Parameters i, j correspond to the vertices element indices
|
||||
public void removeEdge(int i, int j) {
|
||||
// Handle index out of bounds and equality
|
||||
if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)
|
||||
@@ -97,35 +97,35 @@ class GraphAdjMat {
|
||||
|
||||
public class graph_adjacency_matrix {
|
||||
public static void main(String[] args) {
|
||||
/* Initialize undirected graph */
|
||||
// Edges elements represent vertex indices
|
||||
/* Add edge */
|
||||
// Note that the edges elements represent vertex indices, i.e., corresponding to the vertices element indices
|
||||
int[] vertices = { 1, 3, 2, 5, 4 };
|
||||
int[][] edges = { { 0, 1 }, { 0, 3 }, { 1, 2 }, { 2, 3 }, { 2, 4 }, { 3, 4 } };
|
||||
GraphAdjMat graph = new GraphAdjMat(vertices, edges);
|
||||
System.out.println("\nAfter initialization, the graph is");
|
||||
System.out.println("\nAfter initialization, graph is");
|
||||
graph.print();
|
||||
|
||||
/* Add edge */
|
||||
// Indices of vertices 1, 2 are 0, 2 respectively
|
||||
// Add vertex
|
||||
graph.addEdge(0, 2);
|
||||
System.out.println("\nAfter adding edge 1-2, the graph is");
|
||||
System.out.println("\nAfter adding edge 1-2, graph is");
|
||||
graph.print();
|
||||
|
||||
/* Remove edge */
|
||||
// Indices of vertices 1, 3 are 0, 1 respectively
|
||||
// Vertices 1, 3 have indices 0, 1 respectively
|
||||
graph.removeEdge(0, 1);
|
||||
System.out.println("\nAfter removing edge 1-3, the graph is");
|
||||
System.out.println("\nAfter removing edge 1-3, graph is");
|
||||
graph.print();
|
||||
|
||||
/* Add vertex */
|
||||
graph.addVertex(6);
|
||||
System.out.println("\nAfter adding vertex 6, the graph is");
|
||||
System.out.println("\nAfter adding vertex 6, graph is");
|
||||
graph.print();
|
||||
|
||||
/* Remove vertex */
|
||||
// Index of vertex 3 is 1
|
||||
// Vertex 3 has index 1
|
||||
graph.removeVertex(1);
|
||||
System.out.println("\nAfter removing vertex 3, the graph is");
|
||||
System.out.println("\nAfter removing vertex 3, graph is");
|
||||
graph.print();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,11 +11,11 @@ import utils.*;
|
||||
|
||||
public class graph_bfs {
|
||||
/* Breadth-first traversal */
|
||||
// Use adjacency list to represent the graph, to obtain all adjacent vertices of a specified vertex
|
||||
// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex
|
||||
static List<Vertex> graphBFS(GraphAdjList graph, Vertex startVet) {
|
||||
// Vertex traversal sequence
|
||||
List<Vertex> res = new ArrayList<>();
|
||||
// Hash set, used to record visited vertices
|
||||
// Hash set for recording vertices that have been visited
|
||||
Set<Vertex> visited = new HashSet<>();
|
||||
visited.add(startVet);
|
||||
// Queue used to implement BFS
|
||||
@@ -23,28 +23,28 @@ public class graph_bfs {
|
||||
que.offer(startVet);
|
||||
// Starting from vertex vet, loop until all vertices are visited
|
||||
while (!que.isEmpty()) {
|
||||
Vertex vet = que.poll(); // Dequeue the vertex at the head of the queue
|
||||
Vertex vet = que.poll(); // Dequeue the front vertex
|
||||
res.add(vet); // Record visited vertex
|
||||
// Traverse all adjacent vertices of that vertex
|
||||
// Traverse all adjacent vertices of this vertex
|
||||
for (Vertex adjVet : graph.adjList.get(vet)) {
|
||||
if (visited.contains(adjVet))
|
||||
continue; // Skip already visited vertices
|
||||
continue; // Skip vertices that have been visited
|
||||
que.offer(adjVet); // Only enqueue unvisited vertices
|
||||
visited.add(adjVet); // Mark the vertex as visited
|
||||
visited.add(adjVet); // Mark this vertex as visited
|
||||
}
|
||||
}
|
||||
// Return the vertex traversal sequence
|
||||
// Return vertex traversal sequence
|
||||
return res;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
/* Initialize undirected graph */
|
||||
/* Add edge */
|
||||
Vertex[] v = Vertex.valsToVets(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
|
||||
Vertex[][] edges = { { v[0], v[1] }, { v[0], v[3] }, { v[1], v[2] }, { v[1], v[4] },
|
||||
{ v[2], v[5] }, { v[3], v[4] }, { v[3], v[6] }, { v[4], v[5] },
|
||||
{ v[4], v[7] }, { v[5], v[8] }, { v[6], v[7] }, { v[7], v[8] } };
|
||||
GraphAdjList graph = new GraphAdjList(edges);
|
||||
System.out.println("\nAfter initialization, the graph is");
|
||||
System.out.println("\nAfter initialization, graph is");
|
||||
graph.print();
|
||||
|
||||
/* Breadth-first traversal */
|
||||
|
||||
@@ -13,34 +13,34 @@ public class graph_dfs {
|
||||
/* Depth-first traversal helper function */
|
||||
static void dfs(GraphAdjList graph, Set<Vertex> visited, List<Vertex> res, Vertex vet) {
|
||||
res.add(vet); // Record visited vertex
|
||||
visited.add(vet); // Mark the vertex as visited
|
||||
// Traverse all adjacent vertices of that vertex
|
||||
visited.add(vet); // Mark this vertex as visited
|
||||
// Traverse all adjacent vertices of this vertex
|
||||
for (Vertex adjVet : graph.adjList.get(vet)) {
|
||||
if (visited.contains(adjVet))
|
||||
continue; // Skip already visited vertices
|
||||
continue; // Skip vertices that have been visited
|
||||
// Recursively visit adjacent vertices
|
||||
dfs(graph, visited, res, adjVet);
|
||||
}
|
||||
}
|
||||
|
||||
/* Depth-first traversal */
|
||||
// Use adjacency list to represent the graph, to obtain all adjacent vertices of a specified vertex
|
||||
// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex
|
||||
static List<Vertex> graphDFS(GraphAdjList graph, Vertex startVet) {
|
||||
// Vertex traversal sequence
|
||||
List<Vertex> res = new ArrayList<>();
|
||||
// Hash set, used to record visited vertices
|
||||
// Hash set for recording vertices that have been visited
|
||||
Set<Vertex> visited = new HashSet<>();
|
||||
dfs(graph, visited, res, startVet);
|
||||
return res;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
/* Initialize undirected graph */
|
||||
/* Add edge */
|
||||
Vertex[] v = Vertex.valsToVets(new int[] { 0, 1, 2, 3, 4, 5, 6 });
|
||||
Vertex[][] edges = { { v[0], v[1] }, { v[0], v[3] }, { v[1], v[2] },
|
||||
{ v[2], v[5] }, { v[4], v[5] }, { v[5], v[6] } };
|
||||
GraphAdjList graph = new GraphAdjList(edges);
|
||||
System.out.println("\nAfter initialization, the graph is");
|
||||
System.out.println("\nAfter initialization, graph is");
|
||||
graph.print();
|
||||
|
||||
/* Depth-first traversal */
|
||||
|
||||
@@ -9,14 +9,14 @@ package chapter_greedy;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class coin_change_greedy {
|
||||
/* Coin change: Greedy */
|
||||
/* Coin change: Greedy algorithm */
|
||||
static int coinChangeGreedy(int[] coins, int amt) {
|
||||
// Assume coins list is ordered
|
||||
// Assume coins list is sorted
|
||||
int i = coins.length - 1;
|
||||
int count = 0;
|
||||
// Loop for greedy selection until no remaining amount
|
||||
// Loop to make greedy choices until no remaining amount
|
||||
while (amt > 0) {
|
||||
// Find the smallest coin close to and less than the remaining amount
|
||||
// Find the coin that is less than and closest to the remaining amount
|
||||
while (i > 0 && coins[i] > amt) {
|
||||
i--;
|
||||
}
|
||||
@@ -29,27 +29,27 @@ public class coin_change_greedy {
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
// Greedy: can ensure finding a global optimal solution
|
||||
// Greedy algorithm: Can guarantee finding the global optimal solution
|
||||
int[] coins = { 1, 5, 10, 20, 50, 100 };
|
||||
int amt = 186;
|
||||
int res = coinChangeGreedy(coins, amt);
|
||||
System.out.println("\ncoins = " + Arrays.toString(coins) + ", amt = " + amt);
|
||||
System.out.println("The minimum number of coins required to make up " + amt + " is " + res);
|
||||
System.out.println("Minimum number of coins needed to make " + amt + " is " + res);
|
||||
|
||||
// Greedy: cannot ensure finding a global optimal solution
|
||||
// Greedy algorithm: Cannot guarantee finding the global optimal solution
|
||||
coins = new int[] { 1, 20, 50 };
|
||||
amt = 60;
|
||||
res = coinChangeGreedy(coins, amt);
|
||||
System.out.println("\ncoins = " + Arrays.toString(coins) + ", amt = " + amt);
|
||||
System.out.println("The minimum number of coins required to make up " + amt + " is " + res);
|
||||
System.out.println("In reality, the minimum number needed is 3, i.e., 20 + 20 + 20");
|
||||
System.out.println("Minimum number of coins needed to make " + amt + " is " + res);
|
||||
System.out.println("Actually the minimum number needed is 3, i.e., 20 + 20 + 20");
|
||||
|
||||
// Greedy: cannot ensure finding a global optimal solution
|
||||
// Greedy algorithm: Cannot guarantee finding the global optimal solution
|
||||
coins = new int[] { 1, 49, 50 };
|
||||
amt = 98;
|
||||
res = coinChangeGreedy(coins, amt);
|
||||
System.out.println("\ncoins = " + Arrays.toString(coins) + ", amt = " + amt);
|
||||
System.out.println("The minimum number of coins required to make up " + amt + " is " + res);
|
||||
System.out.println("In reality, the minimum number needed is 2, i.e., 49 + 49");
|
||||
System.out.println("Minimum number of coins needed to make " + amt + " is " + res);
|
||||
System.out.println("Actually the minimum number needed is 2, i.e., 49 + 49");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,9 +21,9 @@ class Item {
|
||||
}
|
||||
|
||||
public class fractional_knapsack {
|
||||
/* Fractional knapsack: Greedy */
|
||||
/* Fractional knapsack: Greedy algorithm */
|
||||
static double fractionalKnapsack(int[] wgt, int[] val, int cap) {
|
||||
// Create an item list, containing two properties: weight, value
|
||||
// Create item list with two attributes: weight, value
|
||||
Item[] items = new Item[wgt.length];
|
||||
for (int i = 0; i < wgt.length; i++) {
|
||||
items[i] = new Item(wgt[i], val[i]);
|
||||
@@ -34,13 +34,13 @@ public class fractional_knapsack {
|
||||
double res = 0;
|
||||
for (Item item : items) {
|
||||
if (item.w <= cap) {
|
||||
// If the remaining capacity is sufficient, put the entire item into the knapsack
|
||||
// If remaining capacity is sufficient, put the entire current item into the knapsack
|
||||
res += item.v;
|
||||
cap -= item.w;
|
||||
} else {
|
||||
// If the remaining capacity is insufficient, put part of the item into the knapsack
|
||||
// If remaining capacity is insufficient, put part of the current item into the knapsack
|
||||
res += (double) item.v / item.w * cap;
|
||||
// No remaining capacity left, thus break the loop
|
||||
// No remaining capacity, so break out of the loop
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -54,6 +54,6 @@ public class fractional_knapsack {
|
||||
|
||||
// Greedy algorithm
|
||||
double res = fractionalKnapsack(wgt, val, cap);
|
||||
System.out.println("The maximum value within the bag capacity is " + res);
|
||||
System.out.println("Maximum item value not exceeding knapsack capacity is " + res);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,15 +7,15 @@
|
||||
package chapter_greedy;
|
||||
|
||||
public class max_capacity {
|
||||
/* Maximum capacity: Greedy */
|
||||
/* Max capacity: Greedy algorithm */
|
||||
static int maxCapacity(int[] ht) {
|
||||
// Initialize i, j, making them split the array at both ends
|
||||
// Initialize i, j to be at both ends of the array
|
||||
int i = 0, j = ht.length - 1;
|
||||
// Initial maximum capacity is 0
|
||||
// Initial max capacity is 0
|
||||
int res = 0;
|
||||
// Loop for greedy selection until the two boards meet
|
||||
while (i < j) {
|
||||
// Update maximum capacity
|
||||
// Update max capacity
|
||||
int cap = Math.min(ht[i], ht[j]) * (j - i);
|
||||
res = Math.max(res, cap);
|
||||
// Move the shorter board inward
|
||||
@@ -33,6 +33,6 @@ public class max_capacity {
|
||||
|
||||
// Greedy algorithm
|
||||
int res = maxCapacity(ht);
|
||||
System.out.println("The maximum capacity is " + res);
|
||||
System.out.println("Maximum capacity is " + res);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,17 +9,17 @@ package chapter_greedy;
|
||||
import java.lang.Math;
|
||||
|
||||
public class max_product_cutting {
|
||||
/* Maximum product of cutting: Greedy */
|
||||
/* Max product cutting: Greedy algorithm */
|
||||
public static int maxProductCutting(int n) {
|
||||
// When n <= 3, must cut out a 1
|
||||
if (n <= 3) {
|
||||
return 1 * (n - 1);
|
||||
}
|
||||
// Greedy cut out 3s, a is the number of 3s, b is the remainder
|
||||
// Greedily cut out 3, a is the number of 3s, b is the remainder
|
||||
int a = n / 3;
|
||||
int b = n % 3;
|
||||
if (b == 1) {
|
||||
// When the remainder is 1, convert a pair of 1 * 3 into 2 * 2
|
||||
// When the remainder is 1, convert a pair of 1 * 3 to 2 * 2
|
||||
return (int) Math.pow(3, a - 1) * 2 * 2;
|
||||
}
|
||||
if (b == 2) {
|
||||
@@ -35,6 +35,6 @@ public class max_product_cutting {
|
||||
|
||||
// Greedy algorithm
|
||||
int res = maxProductCutting(n);
|
||||
System.out.println("The maximum product of division is " + res);
|
||||
System.out.println("Maximum cutting product is " + res);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ class ArrayHashMap {
|
||||
private List<Pair> buckets;
|
||||
|
||||
public ArrayHashMap() {
|
||||
// Initialize an array, containing 100 buckets
|
||||
// Initialize array with 100 buckets
|
||||
buckets = new ArrayList<>();
|
||||
for (int i = 0; i < 100; i++) {
|
||||
buckets.add(null);
|
||||
@@ -56,7 +56,7 @@ class ArrayHashMap {
|
||||
/* Remove operation */
|
||||
public void remove(int key) {
|
||||
int index = hashFunc(key);
|
||||
// Set to null, indicating removal
|
||||
// Set to null to represent deletion
|
||||
buckets.set(index, null);
|
||||
}
|
||||
|
||||
@@ -105,23 +105,23 @@ public class array_hash_map {
|
||||
|
||||
/* Add operation */
|
||||
// Add key-value pair (key, value) to the hash table
|
||||
map.put(12836, "Ha");
|
||||
map.put(15937, "Luo");
|
||||
map.put(16750, "Suan");
|
||||
map.put(13276, "Fa");
|
||||
map.put(10583, "Ya");
|
||||
System.out.println("\nAfter adding, the hash table is\nKey -> Value");
|
||||
map.put(12836, "Xiao Ha");
|
||||
map.put(15937, "Xiao Luo");
|
||||
map.put(16750, "Xiao Suan");
|
||||
map.put(13276, "Xiao Fa");
|
||||
map.put(10583, "Xiao Ya");
|
||||
System.out.println("\nAfter adding is complete, hash table is\nKey -> Value");
|
||||
map.print();
|
||||
|
||||
/* Query operation */
|
||||
// Enter key to the hash table, get value
|
||||
// Input key into hash table to get value
|
||||
String name = map.get(15937);
|
||||
System.out.println("\nEnter student ID 15937, found name " + name);
|
||||
System.out.println("\nInput student ID 15937, query name " + name);
|
||||
|
||||
/* Remove operation */
|
||||
// Remove key-value pair (key, value) from the hash table
|
||||
// Remove key-value pair (key, value) from hash table
|
||||
map.remove(10583);
|
||||
System.out.println("\nAfter removing 10583, the hash table is\nKey -> Value");
|
||||
System.out.println("\nAfter removing 10583, hash table is\nKey -> Value");
|
||||
map.print();
|
||||
|
||||
/* Traverse hash table */
|
||||
@@ -129,11 +129,11 @@ public class array_hash_map {
|
||||
for (Pair kv : map.pairSet()) {
|
||||
System.out.println(kv.key + " -> " + kv.val);
|
||||
}
|
||||
System.out.println("\nIndividually traverse keys Key");
|
||||
System.out.println("\nTraverse keys only Key");
|
||||
for (int key : map.keySet()) {
|
||||
System.out.println(key);
|
||||
}
|
||||
System.out.println("\nIndividually traverse values Value");
|
||||
System.out.println("\nTraverse values only Value");
|
||||
for (String val : map.valueSet()) {
|
||||
System.out.println(val);
|
||||
}
|
||||
|
||||
@@ -13,26 +13,26 @@ public class built_in_hash {
|
||||
public static void main(String[] args) {
|
||||
int num = 3;
|
||||
int hashNum = Integer.hashCode(num);
|
||||
System.out.println("The hash value of integer " + num + " is " + hashNum);
|
||||
System.out.println("Hash value of integer " + num + " is " + hashNum);
|
||||
|
||||
boolean bol = true;
|
||||
int hashBol = Boolean.hashCode(bol);
|
||||
System.out.println("The hash value of boolean " + bol + " is " + hashBol);
|
||||
System.out.println("Hash value of boolean " + bol + " is " + hashBol);
|
||||
|
||||
double dec = 3.14159;
|
||||
int hashDec = Double.hashCode(dec);
|
||||
System.out.println("The hash value of decimal " + dec + " is " + hashDec);
|
||||
System.out.println("Hash value of decimal " + dec + " is " + hashDec);
|
||||
|
||||
String str = "Hello algorithm";
|
||||
String str = "Hello Algo";
|
||||
int hashStr = str.hashCode();
|
||||
System.out.println("The hash value of string " + str + " is " + hashStr);
|
||||
System.out.println("Hash value of string " + str + " is " + hashStr);
|
||||
|
||||
Object[] arr = { 12836, "Ha" };
|
||||
Object[] arr = { 12836, "Xiao Ha" };
|
||||
int hashTup = Arrays.hashCode(arr);
|
||||
System.out.println("The hash value of array " + Arrays.toString(arr) + " is " + hashTup);
|
||||
System.out.println("Hash value of array " + Arrays.toString(arr) + " is " + hashTup);
|
||||
|
||||
ListNode obj = new ListNode(0);
|
||||
int hashObj = obj.hashCode();
|
||||
System.out.println("The hash value of node object " + obj + " is " + hashObj);
|
||||
System.out.println("Hash value of node object " + obj + " is " + hashObj);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,23 +16,23 @@ public class hash_map {
|
||||
|
||||
/* Add operation */
|
||||
// Add key-value pair (key, value) to the hash table
|
||||
map.put(12836, "Ha");
|
||||
map.put(15937, "Luo");
|
||||
map.put(16750, "Suan");
|
||||
map.put(13276, "Fa");
|
||||
map.put(10583, "Ya");
|
||||
System.out.println("\nAfter adding, the hash table is\nKey -> Value");
|
||||
map.put(12836, "Xiao Ha");
|
||||
map.put(15937, "Xiao Luo");
|
||||
map.put(16750, "Xiao Suan");
|
||||
map.put(13276, "Xiao Fa");
|
||||
map.put(10583, "Xiao Ya");
|
||||
System.out.println("\nAfter adding is complete, hash table is\nKey -> Value");
|
||||
PrintUtil.printHashMap(map);
|
||||
|
||||
/* Query operation */
|
||||
// Enter key to the hash table, get value
|
||||
// Input key into hash table to get value
|
||||
String name = map.get(15937);
|
||||
System.out.println("\nEnter student ID 15937, found name " + name);
|
||||
System.out.println("\nInput student ID 15937, query name " + name);
|
||||
|
||||
/* Remove operation */
|
||||
// Remove key-value pair (key, value) from the hash table
|
||||
// Remove key-value pair (key, value) from hash table
|
||||
map.remove(10583);
|
||||
System.out.println("\nAfter removing 10583, the hash table is\nKey -> Value");
|
||||
System.out.println("\nAfter removing 10583, hash table is\nKey -> Value");
|
||||
PrintUtil.printHashMap(map);
|
||||
|
||||
/* Traverse hash table */
|
||||
@@ -40,11 +40,11 @@ public class hash_map {
|
||||
for (Map.Entry<Integer, String> kv : map.entrySet()) {
|
||||
System.out.println(kv.getKey() + " -> " + kv.getValue());
|
||||
}
|
||||
System.out.println("\nIndividually traverse keys Key");
|
||||
System.out.println("\nTraverse keys only Key");
|
||||
for (int key : map.keySet()) {
|
||||
System.out.println(key);
|
||||
}
|
||||
System.out.println("\nIndividually traverse values Value");
|
||||
System.out.println("\nTraverse values only Value");
|
||||
for (String val : map.values()) {
|
||||
System.out.println(val);
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ package chapter_hashing;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/* Chained address hash table */
|
||||
/* Hash table with separate chaining */
|
||||
class HashMapChaining {
|
||||
int size; // Number of key-value pairs
|
||||
int capacity; // Hash table capacity
|
||||
@@ -43,7 +43,7 @@ class HashMapChaining {
|
||||
String get(int key) {
|
||||
int index = hashFunc(key);
|
||||
List<Pair> bucket = buckets.get(index);
|
||||
// Traverse the bucket, if the key is found, return the corresponding val
|
||||
// Traverse bucket, if key is found, return corresponding val
|
||||
for (Pair pair : bucket) {
|
||||
if (pair.key == key) {
|
||||
return pair.val;
|
||||
@@ -55,20 +55,20 @@ class HashMapChaining {
|
||||
|
||||
/* Add operation */
|
||||
void put(int key, String val) {
|
||||
// When the load factor exceeds the threshold, perform expansion
|
||||
// When load factor exceeds threshold, perform expansion
|
||||
if (loadFactor() > loadThres) {
|
||||
extend();
|
||||
}
|
||||
int index = hashFunc(key);
|
||||
List<Pair> bucket = buckets.get(index);
|
||||
// Traverse the bucket, if the specified key is encountered, update the corresponding val and return
|
||||
// Traverse bucket, if specified key is encountered, update corresponding val and return
|
||||
for (Pair pair : bucket) {
|
||||
if (pair.key == key) {
|
||||
pair.val = val;
|
||||
return;
|
||||
}
|
||||
}
|
||||
// If the key is not found, add the key-value pair to the end
|
||||
// If key does not exist, append key-value pair to the end
|
||||
Pair pair = new Pair(key, val);
|
||||
bucket.add(pair);
|
||||
size++;
|
||||
@@ -78,7 +78,7 @@ class HashMapChaining {
|
||||
void remove(int key) {
|
||||
int index = hashFunc(key);
|
||||
List<Pair> bucket = buckets.get(index);
|
||||
// Traverse the bucket, remove the key-value pair from it
|
||||
// Traverse bucket and remove key-value pair from it
|
||||
for (Pair pair : bucket) {
|
||||
if (pair.key == key) {
|
||||
bucket.remove(pair);
|
||||
@@ -88,18 +88,18 @@ class HashMapChaining {
|
||||
}
|
||||
}
|
||||
|
||||
/* Extend hash table */
|
||||
/* Expand hash table */
|
||||
void extend() {
|
||||
// Temporarily store the original hash table
|
||||
List<List<Pair>> bucketsTmp = buckets;
|
||||
// Initialize the extended new hash table
|
||||
// Initialize expanded new hash table
|
||||
capacity *= extendRatio;
|
||||
buckets = new ArrayList<>(capacity);
|
||||
for (int i = 0; i < capacity; i++) {
|
||||
buckets.add(new ArrayList<>());
|
||||
}
|
||||
size = 0;
|
||||
// Move key-value pairs from the original hash table to the new hash table
|
||||
// Move key-value pairs from original hash table to new hash table
|
||||
for (List<Pair> bucket : bucketsTmp) {
|
||||
for (Pair pair : bucket) {
|
||||
put(pair.key, pair.val);
|
||||
@@ -126,23 +126,23 @@ public class hash_map_chaining {
|
||||
|
||||
/* Add operation */
|
||||
// Add key-value pair (key, value) to the hash table
|
||||
map.put(12836, "Ha");
|
||||
map.put(15937, "Luo");
|
||||
map.put(16750, "Suan");
|
||||
map.put(13276, "Fa");
|
||||
map.put(10583, "Ya");
|
||||
System.out.println("\nAfter adding, the hash table is\nKey -> Value");
|
||||
map.put(12836, "Xiao Ha");
|
||||
map.put(15937, "Xiao Luo");
|
||||
map.put(16750, "Xiao Suan");
|
||||
map.put(13276, "Xiao Fa");
|
||||
map.put(10583, "Xiao Ya");
|
||||
System.out.println("\nAfter adding is complete, hash table is\nKey -> Value");
|
||||
map.print();
|
||||
|
||||
/* Query operation */
|
||||
// Enter key to the hash table, get value
|
||||
// Input key into hash table to get value
|
||||
String name = map.get(13276);
|
||||
System.out.println("\nEnter student ID 13276, found name " + name);
|
||||
System.out.println("\nInput student ID 13276, query name " + name);
|
||||
|
||||
/* Remove operation */
|
||||
// Remove key-value pair (key, value) from the hash table
|
||||
// Remove key-value pair (key, value) from hash table
|
||||
map.remove(12836);
|
||||
System.out.println("\nAfter removing 12836, the hash table is\nKey -> Value");
|
||||
System.out.println("\nAfter removing 12836, hash table is\nKey -> Value");
|
||||
map.print();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,14 +6,14 @@
|
||||
|
||||
package chapter_hashing;
|
||||
|
||||
/* Open addressing hash table */
|
||||
/* Hash table with open addressing */
|
||||
class HashMapOpenAddressing {
|
||||
private int size; // Number of key-value pairs
|
||||
private int capacity = 4; // Hash table capacity
|
||||
private final double loadThres = 2.0 / 3.0; // Load factor threshold for triggering expansion
|
||||
private final int extendRatio = 2; // Expansion multiplier
|
||||
private Pair[] buckets; // Bucket array
|
||||
private final Pair TOMBSTONE = new Pair(-1, "-1"); // Removal mark
|
||||
private final Pair TOMBSTONE = new Pair(-1, "-1"); // Removal marker
|
||||
|
||||
/* Constructor */
|
||||
public HashMapOpenAddressing() {
|
||||
@@ -31,15 +31,15 @@ class HashMapOpenAddressing {
|
||||
return (double) size / capacity;
|
||||
}
|
||||
|
||||
/* Search for the bucket index corresponding to key */
|
||||
/* Search for bucket index corresponding to key */
|
||||
private int findBucket(int key) {
|
||||
int index = hashFunc(key);
|
||||
int firstTombstone = -1;
|
||||
// Linear probing, break when encountering an empty bucket
|
||||
while (buckets[index] != null) {
|
||||
// If the key is encountered, return the corresponding bucket index
|
||||
// If key is encountered, return the corresponding bucket index
|
||||
if (buckets[index].key == key) {
|
||||
// If a removal mark was encountered earlier, move the key-value pair to that index
|
||||
// If a removal marker was encountered before, move the key-value pair to that index
|
||||
if (firstTombstone != -1) {
|
||||
buckets[firstTombstone] = buckets[index];
|
||||
buckets[index] = TOMBSTONE;
|
||||
@@ -47,67 +47,67 @@ class HashMapOpenAddressing {
|
||||
}
|
||||
return index; // Return bucket index
|
||||
}
|
||||
// Record the first encountered removal mark
|
||||
// Record the first removal marker encountered
|
||||
if (firstTombstone == -1 && buckets[index] == TOMBSTONE) {
|
||||
firstTombstone = index;
|
||||
}
|
||||
// Calculate the bucket index, return to the head if exceeding the tail
|
||||
// Calculate bucket index, wrap around to the head if past the tail
|
||||
index = (index + 1) % capacity;
|
||||
}
|
||||
// If the key does not exist, return the index of the insertion point
|
||||
// If key does not exist, return the index for insertion
|
||||
return firstTombstone == -1 ? index : firstTombstone;
|
||||
}
|
||||
|
||||
/* Query operation */
|
||||
public String get(int key) {
|
||||
// Search for the bucket index corresponding to key
|
||||
// Search for bucket index corresponding to key
|
||||
int index = findBucket(key);
|
||||
// If the key-value pair is found, return the corresponding val
|
||||
// If key-value pair is found, return corresponding val
|
||||
if (buckets[index] != null && buckets[index] != TOMBSTONE) {
|
||||
return buckets[index].val;
|
||||
}
|
||||
// If the key-value pair does not exist, return null
|
||||
// If key-value pair does not exist, return null
|
||||
return null;
|
||||
}
|
||||
|
||||
/* Add operation */
|
||||
public void put(int key, String val) {
|
||||
// When the load factor exceeds the threshold, perform expansion
|
||||
// When load factor exceeds threshold, perform expansion
|
||||
if (loadFactor() > loadThres) {
|
||||
extend();
|
||||
}
|
||||
// Search for the bucket index corresponding to key
|
||||
// Search for bucket index corresponding to key
|
||||
int index = findBucket(key);
|
||||
// If the key-value pair is found, overwrite val and return
|
||||
// If key-value pair is found, overwrite val and return
|
||||
if (buckets[index] != null && buckets[index] != TOMBSTONE) {
|
||||
buckets[index].val = val;
|
||||
return;
|
||||
}
|
||||
// If the key-value pair does not exist, add the key-value pair
|
||||
// If key-value pair does not exist, add the key-value pair
|
||||
buckets[index] = new Pair(key, val);
|
||||
size++;
|
||||
}
|
||||
|
||||
/* Remove operation */
|
||||
public void remove(int key) {
|
||||
// Search for the bucket index corresponding to key
|
||||
// Search for bucket index corresponding to key
|
||||
int index = findBucket(key);
|
||||
// If the key-value pair is found, cover it with a removal mark
|
||||
// If key-value pair is found, overwrite it with removal marker
|
||||
if (buckets[index] != null && buckets[index] != TOMBSTONE) {
|
||||
buckets[index] = TOMBSTONE;
|
||||
size--;
|
||||
}
|
||||
}
|
||||
|
||||
/* Extend hash table */
|
||||
/* Expand hash table */
|
||||
private void extend() {
|
||||
// Temporarily store the original hash table
|
||||
Pair[] bucketsTmp = buckets;
|
||||
// Initialize the extended new hash table
|
||||
// Initialize expanded new hash table
|
||||
capacity *= extendRatio;
|
||||
buckets = new Pair[capacity];
|
||||
size = 0;
|
||||
// Move key-value pairs from the original hash table to the new hash table
|
||||
// Move key-value pairs from original hash table to new hash table
|
||||
for (Pair pair : bucketsTmp) {
|
||||
if (pair != null && pair != TOMBSTONE) {
|
||||
put(pair.key, pair.val);
|
||||
@@ -136,23 +136,23 @@ public class hash_map_open_addressing {
|
||||
|
||||
// Add operation
|
||||
// Add key-value pair (key, val) to the hash table
|
||||
hashmap.put(12836, "Ha");
|
||||
hashmap.put(15937, "Luo");
|
||||
hashmap.put(16750, "Suan");
|
||||
hashmap.put(13276, "Fa");
|
||||
hashmap.put(10583, "Ya");
|
||||
System.out.println("\nAfter adding, the hash table is\nKey -> Value");
|
||||
hashmap.put(12836, "Xiao Ha");
|
||||
hashmap.put(15937, "Xiao Luo");
|
||||
hashmap.put(16750, "Xiao Suan");
|
||||
hashmap.put(13276, "Xiao Fa");
|
||||
hashmap.put(10583, "Xiao Ya");
|
||||
System.out.println("\nAfter adding is complete, hash table is\nKey -> Value");
|
||||
hashmap.print();
|
||||
|
||||
// Query operation
|
||||
// Enter key to the hash table, get value val
|
||||
// Input key into hash table to get value val
|
||||
String name = hashmap.get(13276);
|
||||
System.out.println("\nEnter student ID 13276, found name " + name);
|
||||
System.out.println("\nInput student ID 13276, query name " + name);
|
||||
|
||||
// Remove operation
|
||||
// Remove key-value pair (key, val) from the hash table
|
||||
// Remove key-value pair (key, val) from hash table
|
||||
hashmap.remove(16750);
|
||||
System.out.println("\nAfter removing 16750, the hash table is\nKey -> Value");
|
||||
System.out.println("\nAfter removing 16750, hash table is\nKey -> Value");
|
||||
hashmap.print();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ public class simple_hash {
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
String key = "Hello algorithm";
|
||||
String key = "Hello Algo";
|
||||
|
||||
int hash = addHash(key);
|
||||
System.out.println("Additive hash value is " + hash);
|
||||
|
||||
@@ -11,38 +11,38 @@ import java.util.*;
|
||||
|
||||
public class heap {
|
||||
public static void testPush(Queue<Integer> heap, int val) {
|
||||
heap.offer(val); // Push the element into heap
|
||||
System.out.format("\nAfter element %d is added to the heap\n", val);
|
||||
heap.offer(val); // Element enters heap
|
||||
System.out.format("\nAfter element %d enters heap\n", val);
|
||||
PrintUtil.printHeap(heap);
|
||||
}
|
||||
|
||||
public static void testPop(Queue<Integer> heap) {
|
||||
int val = heap.poll(); // Pop the element at the heap top
|
||||
System.out.format("\nAfter the top element %d is removed from the heap\n", val);
|
||||
int val = heap.poll(); // Time complexity is O(n), not O(nlogn)
|
||||
System.out.format("\nAfter heap top element %d exits heap\n", val);
|
||||
PrintUtil.printHeap(heap);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
/* Initialize the heap */
|
||||
// Initialize min-heap
|
||||
/* Initialize heap */
|
||||
// Python's heapq module implements min heap by default
|
||||
Queue<Integer> minHeap = new PriorityQueue<>();
|
||||
// Initialize the max-heap (using lambda expression to modify Comparator if necessary)
|
||||
// Initialize max heap (modify Comparator using lambda expression)
|
||||
Queue<Integer> maxHeap = new PriorityQueue<>((a, b) -> b - a);
|
||||
|
||||
System.out.println("\nThe following test case is for max-heap");
|
||||
System.out.println("\nThe following test cases are for max heap");
|
||||
|
||||
/* Push the element into heap */
|
||||
/* Element enters heap */
|
||||
testPush(maxHeap, 1);
|
||||
testPush(maxHeap, 3);
|
||||
testPush(maxHeap, 2);
|
||||
testPush(maxHeap, 5);
|
||||
testPush(maxHeap, 4);
|
||||
|
||||
/* Access heap top element */
|
||||
/* Check if heap is empty */
|
||||
int peek = maxHeap.peek();
|
||||
System.out.format("\nTop element of the heap is %d\n", peek);
|
||||
System.out.format("\nHeap top element is %d\n", peek);
|
||||
|
||||
/* Pop the element at the heap top */
|
||||
/* Time complexity is O(n), not O(nlogn) */
|
||||
testPop(maxHeap);
|
||||
testPop(maxHeap);
|
||||
testPop(maxHeap);
|
||||
@@ -51,16 +51,16 @@ public class heap {
|
||||
|
||||
/* Get heap size */
|
||||
int size = maxHeap.size();
|
||||
System.out.format("\nNumber of elements in the heap is %d\n", size);
|
||||
System.out.format("\nHeap element count is %d\n", size);
|
||||
|
||||
/* Determine if heap is empty */
|
||||
/* Check if heap is empty */
|
||||
boolean isEmpty = maxHeap.isEmpty();
|
||||
System.out.format("\nIs the heap empty %b\n", isEmpty);
|
||||
System.out.format("\nHeap is empty %b\n", isEmpty);
|
||||
|
||||
/* Enter list and build heap */
|
||||
/* Input list and build heap */
|
||||
// Time complexity is O(n), not O(nlogn)
|
||||
minHeap = new PriorityQueue<>(Arrays.asList(1, 3, 2, 5, 4));
|
||||
System.out.println("\nEnter list and build min-heap");
|
||||
System.out.println("\nAfter inputting list and building min heap");
|
||||
PrintUtil.printHeap(minHeap);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,16 +9,16 @@ package chapter_heap;
|
||||
import utils.*;
|
||||
import java.util.*;
|
||||
|
||||
/* Max-heap */
|
||||
/* Max heap */
|
||||
class MaxHeap {
|
||||
// Use list instead of array to avoid the need for resizing
|
||||
// Use list instead of array, no need to consider capacity expansion
|
||||
private List<Integer> maxHeap;
|
||||
|
||||
/* Constructor, build heap based on input list */
|
||||
public MaxHeap(List<Integer> nums) {
|
||||
// Add all list elements into the heap
|
||||
// Add list elements to heap as is
|
||||
maxHeap = new ArrayList<>(nums);
|
||||
// Heapify all nodes except leaves
|
||||
// Heapify all nodes except leaf nodes
|
||||
for (int i = parent(size() - 1); i >= 0; i--) {
|
||||
siftDown(i);
|
||||
}
|
||||
@@ -36,7 +36,7 @@ class MaxHeap {
|
||||
|
||||
/* Get index of parent node */
|
||||
private int parent(int i) {
|
||||
return (i - 1) / 2; // Integer division down
|
||||
return (i - 1) / 2; // Floor division
|
||||
}
|
||||
|
||||
/* Swap elements */
|
||||
@@ -51,17 +51,17 @@ class MaxHeap {
|
||||
return maxHeap.size();
|
||||
}
|
||||
|
||||
/* Determine if heap is empty */
|
||||
/* Check if heap is empty */
|
||||
public boolean isEmpty() {
|
||||
return size() == 0;
|
||||
}
|
||||
|
||||
/* Access heap top element */
|
||||
/* Access top element */
|
||||
public int peek() {
|
||||
return maxHeap.get(0);
|
||||
}
|
||||
|
||||
/* Push the element into heap */
|
||||
/* Element enters heap */
|
||||
public void push(int val) {
|
||||
// Add node
|
||||
maxHeap.add(val);
|
||||
@@ -69,46 +69,46 @@ class MaxHeap {
|
||||
siftUp(size() - 1);
|
||||
}
|
||||
|
||||
/* Start heapifying node i, from bottom to top */
|
||||
/* Starting from node i, heapify from bottom to top */
|
||||
private void siftUp(int i) {
|
||||
while (true) {
|
||||
// Get parent node of node i
|
||||
int p = parent(i);
|
||||
// When "crossing the root node" or "node does not need repair", end heapification
|
||||
// When "crossing root node" or "node needs no repair", end heapify
|
||||
if (p < 0 || maxHeap.get(i) <= maxHeap.get(p))
|
||||
break;
|
||||
// Swap two nodes
|
||||
swap(i, p);
|
||||
// Loop upwards heapification
|
||||
// Loop upward heapify
|
||||
i = p;
|
||||
}
|
||||
}
|
||||
|
||||
/* Element exits heap */
|
||||
public int pop() {
|
||||
// Empty handling
|
||||
// Handle empty case
|
||||
if (isEmpty())
|
||||
throw new IndexOutOfBoundsException();
|
||||
// Swap the root node with the rightmost leaf node (swap the first element with the last element)
|
||||
// Delete node
|
||||
swap(0, size() - 1);
|
||||
// Remove node
|
||||
int val = maxHeap.remove(size() - 1);
|
||||
// Heapify from top to bottom
|
||||
// Return top element
|
||||
siftDown(0);
|
||||
// Return heap top element
|
||||
return val;
|
||||
}
|
||||
|
||||
/* Start heapifying node i, from top to bottom */
|
||||
/* Starting from node i, heapify from top to bottom */
|
||||
private void siftDown(int i) {
|
||||
while (true) {
|
||||
// Determine the largest node among i, l, r, noted as ma
|
||||
// If node i is largest or indices l, r are out of bounds, no need to continue heapify, break
|
||||
int l = left(i), r = right(i), ma = i;
|
||||
if (l < size() && maxHeap.get(l) > maxHeap.get(ma))
|
||||
ma = l;
|
||||
if (r < size() && maxHeap.get(r) > maxHeap.get(ma))
|
||||
ma = r;
|
||||
// If node i is the largest or indices l, r are out of bounds, no further heapification needed, break
|
||||
// Swap two nodes
|
||||
if (ma == i)
|
||||
break;
|
||||
// Swap two nodes
|
||||
@@ -118,7 +118,7 @@ class MaxHeap {
|
||||
}
|
||||
}
|
||||
|
||||
/* Print heap (binary tree) */
|
||||
/* Driver Code */
|
||||
public void print() {
|
||||
Queue<Integer> queue = new PriorityQueue<>((a, b) -> { return b - a; });
|
||||
queue.addAll(maxHeap);
|
||||
@@ -128,32 +128,32 @@ class MaxHeap {
|
||||
|
||||
public class my_heap {
|
||||
public static void main(String[] args) {
|
||||
/* Initialize max-heap */
|
||||
/* Consider negating the elements before entering the heap, which can reverse the size relationship, thus implementing max heap */
|
||||
MaxHeap maxHeap = new MaxHeap(Arrays.asList(9, 8, 6, 6, 7, 5, 2, 1, 4, 3, 6, 2));
|
||||
System.out.println("\nEnter list and build heap");
|
||||
System.out.println("\nAfter inputting list and building heap");
|
||||
maxHeap.print();
|
||||
|
||||
/* Access heap top element */
|
||||
/* Check if heap is empty */
|
||||
int peek = maxHeap.peek();
|
||||
System.out.format("\nTop element of the heap is %d\n", peek);
|
||||
System.out.format("\nHeap top element is %d\n", peek);
|
||||
|
||||
/* Push the element into heap */
|
||||
/* Element enters heap */
|
||||
int val = 7;
|
||||
maxHeap.push(val);
|
||||
System.out.format("\nAfter element %d is added to the heap\n", val);
|
||||
System.out.format("\nAfter element %d enters heap\n", val);
|
||||
maxHeap.print();
|
||||
|
||||
/* Pop the element at the heap top */
|
||||
/* Time complexity is O(n), not O(nlogn) */
|
||||
peek = maxHeap.pop();
|
||||
System.out.format("\nAfter the top element %d is removed from the heap\n", peek);
|
||||
System.out.format("\nAfter heap top element %d exits heap\n", peek);
|
||||
maxHeap.print();
|
||||
|
||||
/* Get heap size */
|
||||
int size = maxHeap.size();
|
||||
System.out.format("\nNumber of elements in the heap is %d\n", size);
|
||||
System.out.format("\nHeap element count is %d\n", size);
|
||||
|
||||
/* Determine if heap is empty */
|
||||
/* Check if heap is empty */
|
||||
boolean isEmpty = maxHeap.isEmpty();
|
||||
System.out.format("\nIs the heap empty %b\n", isEmpty);
|
||||
System.out.format("\nHeap is empty %b\n", isEmpty);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,17 +10,17 @@ import utils.*;
|
||||
import java.util.*;
|
||||
|
||||
public class top_k {
|
||||
/* Using heap to find the largest k elements in an array */
|
||||
/* Find the largest k elements in array based on heap */
|
||||
static Queue<Integer> topKHeap(int[] nums, int k) {
|
||||
// Initialize min-heap
|
||||
// Python's heapq module implements min heap by default
|
||||
Queue<Integer> heap = new PriorityQueue<Integer>();
|
||||
// Enter the first k elements of the array into the heap
|
||||
// Enter the first k elements of array into heap
|
||||
for (int i = 0; i < k; i++) {
|
||||
heap.offer(nums[i]);
|
||||
}
|
||||
// From the k+1th element, keep the heap length as k
|
||||
// Starting from the (k+1)th element, maintain heap length as k
|
||||
for (int i = k; i < nums.length; i++) {
|
||||
// If the current element is larger than the heap top element, remove the heap top element and enter the current element into the heap
|
||||
// If current element is greater than top element, top element exits heap, current element enters heap
|
||||
if (nums[i] > heap.peek()) {
|
||||
heap.poll();
|
||||
heap.offer(nums[i]);
|
||||
|
||||
@@ -7,39 +7,39 @@
|
||||
package chapter_searching;
|
||||
|
||||
public class binary_search {
|
||||
/* Binary search (double closed interval) */
|
||||
/* Binary search (closed interval on both sides) */
|
||||
static int binarySearch(int[] nums, int target) {
|
||||
// Initialize double closed interval [0, n-1], i.e., i, j point to the first element and last element of the array respectively
|
||||
// Initialize closed interval [0, n-1], i.e., i, j point to the first and last elements of the array
|
||||
int i = 0, j = nums.length - 1;
|
||||
// Loop until the search interval is empty (when i > j, it is empty)
|
||||
// Loop, exit when the search interval is empty (empty when i > j)
|
||||
while (i <= j) {
|
||||
int m = i + (j - i) / 2; // Calculate midpoint index m
|
||||
if (nums[m] < target) // This situation indicates that target is in the interval [m+1, j]
|
||||
int m = i + (j - i) / 2; // Calculate the midpoint index m
|
||||
if (nums[m] < target) // This means target is in the interval [m+1, j]
|
||||
i = m + 1;
|
||||
else if (nums[m] > target) // This situation indicates that target is in the interval [i, m-1]
|
||||
else if (nums[m] > target) // This means target is in the interval [i, m-1]
|
||||
j = m - 1;
|
||||
else // Found the target element, thus return its index
|
||||
else // Found the target element, return its index
|
||||
return m;
|
||||
}
|
||||
// Did not find the target element, thus return -1
|
||||
// Target element not found, return -1
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Binary search (left closed right open interval) */
|
||||
/* Binary search (left-closed right-open interval) */
|
||||
static int binarySearchLCRO(int[] nums, int target) {
|
||||
// Initialize left closed right open interval [0, n), i.e., i, j point to the first element and the last element +1 of the array respectively
|
||||
// Initialize left-closed right-open interval [0, n), i.e., i, j point to the first element and last element+1
|
||||
int i = 0, j = nums.length;
|
||||
// Loop until the search interval is empty (when i = j, it is empty)
|
||||
// Loop, exit when the search interval is empty (empty when i = j)
|
||||
while (i < j) {
|
||||
int m = i + (j - i) / 2; // Calculate midpoint index m
|
||||
if (nums[m] < target) // This situation indicates that target is in the interval [m+1, j)
|
||||
int m = i + (j - i) / 2; // Calculate the midpoint index m
|
||||
if (nums[m] < target) // This means target is in the interval [m+1, j)
|
||||
i = m + 1;
|
||||
else if (nums[m] > target) // This situation indicates that target is in the interval [i, m)
|
||||
else if (nums[m] > target) // This means target is in the interval [i, m)
|
||||
j = m;
|
||||
else // Found the target element, thus return its index
|
||||
else // Found the target element, return its index
|
||||
return m;
|
||||
}
|
||||
// Did not find the target element, thus return -1
|
||||
// Target element not found, return -1
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -47,12 +47,12 @@ public class binary_search {
|
||||
int target = 6;
|
||||
int[] nums = { 1, 3, 6, 8, 12, 15, 23, 26, 31, 35 };
|
||||
|
||||
/* Binary search (double closed interval) */
|
||||
/* Binary search (closed interval on both sides) */
|
||||
int index = binarySearch(nums, target);
|
||||
System.out.println("Index of target element 6 =" + index);
|
||||
System.out.println("Index of target element 6 = " + index);
|
||||
|
||||
/* Binary search (left closed right open interval) */
|
||||
/* Binary search (left-closed right-open interval) */
|
||||
index = binarySearchLCRO(nums, target);
|
||||
System.out.println("Index of target element 6 =" + index);
|
||||
System.out.println("Index of target element 6 = " + index);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ public class binary_search_edge {
|
||||
static int binarySearchLeftEdge(int[] nums, int target) {
|
||||
// Equivalent to finding the insertion point of target
|
||||
int i = binary_search_insertion.binarySearchInsertion(nums, target);
|
||||
// Did not find target, thus return -1
|
||||
// Target not found, return -1
|
||||
if (i == nums.length || nums[i] != target) {
|
||||
return -1;
|
||||
}
|
||||
@@ -25,7 +25,7 @@ public class binary_search_edge {
|
||||
int i = binary_search_insertion.binarySearchInsertion(nums, target + 1);
|
||||
// j points to the rightmost target, i points to the first element greater than target
|
||||
int j = i - 1;
|
||||
// Did not find target, thus return -1
|
||||
// Target not found, return -1
|
||||
if (j == -1 || nums[j] != target) {
|
||||
return -1;
|
||||
}
|
||||
@@ -38,12 +38,12 @@ public class binary_search_edge {
|
||||
int[] nums = { 1, 3, 6, 6, 6, 6, 6, 10, 12, 15 };
|
||||
System.out.println("\nArray nums = " + java.util.Arrays.toString(nums));
|
||||
|
||||
// Binary search for left and right boundaries
|
||||
// Binary search left and right boundaries
|
||||
for (int target : new int[] { 6, 7 }) {
|
||||
int index = binarySearchLeftEdge(nums, target);
|
||||
System.out.println("The leftmost index of element " + target + " is " + index);
|
||||
System.out.println("Index of leftmost element " + target + " is " + index);
|
||||
index = binarySearchRightEdge(nums, target);
|
||||
System.out.println("The rightmost index of element " + target + " is " + index);
|
||||
System.out.println("Index of rightmost element " + target + " is " + index);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,32 +9,32 @@ package chapter_searching;
|
||||
class binary_search_insertion {
|
||||
/* Binary search for insertion point (no duplicate elements) */
|
||||
static int binarySearchInsertionSimple(int[] nums, int target) {
|
||||
int i = 0, j = nums.length - 1; // Initialize double closed interval [0, n-1]
|
||||
int i = 0, j = nums.length - 1; // Initialize closed interval [0, n-1]
|
||||
while (i <= j) {
|
||||
int m = i + (j - i) / 2; // Calculate midpoint index m
|
||||
int m = i + (j - i) / 2; // Calculate the midpoint index m
|
||||
if (nums[m] < target) {
|
||||
i = m + 1; // Target is in interval [m+1, j]
|
||||
i = m + 1; // target is in the interval [m+1, j]
|
||||
} else if (nums[m] > target) {
|
||||
j = m - 1; // Target is in interval [i, m-1]
|
||||
j = m - 1; // target is in the interval [i, m-1]
|
||||
} else {
|
||||
return m; // Found target, return insertion point m
|
||||
}
|
||||
}
|
||||
// Did not find target, return insertion point i
|
||||
// Target not found, return insertion point i
|
||||
return i;
|
||||
}
|
||||
|
||||
/* Binary search for insertion point (with duplicate elements) */
|
||||
static int binarySearchInsertion(int[] nums, int target) {
|
||||
int i = 0, j = nums.length - 1; // Initialize double closed interval [0, n-1]
|
||||
int i = 0, j = nums.length - 1; // Initialize closed interval [0, n-1]
|
||||
while (i <= j) {
|
||||
int m = i + (j - i) / 2; // Calculate midpoint index m
|
||||
int m = i + (j - i) / 2; // Calculate the midpoint index m
|
||||
if (nums[m] < target) {
|
||||
i = m + 1; // Target is in interval [m+1, j]
|
||||
i = m + 1; // target is in the interval [m+1, j]
|
||||
} else if (nums[m] > target) {
|
||||
j = m - 1; // Target is in interval [i, m-1]
|
||||
j = m - 1; // target is in the interval [i, m-1]
|
||||
} else {
|
||||
j = m - 1; // First element less than target is in interval [i, m-1]
|
||||
j = m - 1; // The first element less than target is in the interval [i, m-1]
|
||||
}
|
||||
}
|
||||
// Return insertion point i
|
||||
@@ -48,7 +48,7 @@ class binary_search_insertion {
|
||||
// Binary search for insertion point
|
||||
for (int target : new int[] { 6, 9 }) {
|
||||
int index = binarySearchInsertionSimple(nums, target);
|
||||
System.out.println("The insertion point index for element " + target + " is " + index);
|
||||
System.out.println("Insertion point index for element " + target + " is " + index);
|
||||
}
|
||||
|
||||
// Array with duplicate elements
|
||||
@@ -57,7 +57,7 @@ class binary_search_insertion {
|
||||
// Binary search for insertion point
|
||||
for (int target : new int[] { 2, 6, 20 }) {
|
||||
int index = binarySearchInsertion(nums, target);
|
||||
System.out.println("The insertion point index for element " + target + " is " + index);
|
||||
System.out.println("Insertion point index for element " + target + " is " + index);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,14 +13,14 @@ public class hashing_search {
|
||||
/* Hash search (array) */
|
||||
static int hashingSearchArray(Map<Integer, Integer> map, int target) {
|
||||
// Hash table's key: target element, value: index
|
||||
// If the hash table does not contain this key, return -1
|
||||
// If this key does not exist in the hash table, return -1
|
||||
return map.getOrDefault(target, -1);
|
||||
}
|
||||
|
||||
/* Hash search (linked list) */
|
||||
static ListNode hashingSearchLinkedList(Map<Integer, ListNode> map, int target) {
|
||||
// Hash table key: target node value, value: node object
|
||||
// If the key is not in the hash table, return null
|
||||
// If key is not in hash table, return null
|
||||
return map.getOrDefault(target, null);
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ public class hashing_search {
|
||||
map.put(nums[i], i); // key: element, value: index
|
||||
}
|
||||
int index = hashingSearchArray(map, target);
|
||||
System.out.println("The index of target element 3 is " + index);
|
||||
System.out.println("Index of target element 3 = " + index);
|
||||
|
||||
/* Hash search (linked list) */
|
||||
ListNode head = ListNode.arrToLinkedList(nums);
|
||||
@@ -46,6 +46,6 @@ public class hashing_search {
|
||||
head = head.next;
|
||||
}
|
||||
ListNode node = hashingSearchLinkedList(map1, target);
|
||||
System.out.println("The corresponding node object for target node value 3 is " + node);
|
||||
System.out.println("Node object corresponding to target node value 3 is " + node);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,24 +13,24 @@ public class linear_search {
|
||||
static int linearSearchArray(int[] nums, int target) {
|
||||
// Traverse array
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
// Found the target element, thus return its index
|
||||
// Found the target element, return its index
|
||||
if (nums[i] == target)
|
||||
return i;
|
||||
}
|
||||
// Did not find the target element, thus return -1
|
||||
// Target element not found, return -1
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Linear search (linked list) */
|
||||
static ListNode linearSearchLinkedList(ListNode head, int target) {
|
||||
// Traverse the list
|
||||
// Traverse the linked list
|
||||
while (head != null) {
|
||||
// Found the target node, return it
|
||||
if (head.val == target)
|
||||
return head;
|
||||
head = head.next;
|
||||
}
|
||||
// If the target node is not found, return null
|
||||
// Target node not found, return null
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -40,11 +40,11 @@ public class linear_search {
|
||||
/* Perform linear search in array */
|
||||
int[] nums = { 1, 5, 3, 2, 4, 7, 5, 9, 10, 8 };
|
||||
int index = linearSearchArray(nums, target);
|
||||
System.out.println("The index of target element 3 is " + index);
|
||||
System.out.println("Index of target element 3 = " + index);
|
||||
|
||||
/* Perform linear search in linked list */
|
||||
ListNode head = ListNode.arrToLinkedList(nums);
|
||||
ListNode node = linearSearchLinkedList(head, target);
|
||||
System.out.println("The corresponding node object for target node value 3 is " + node);
|
||||
System.out.println("Node object corresponding to target node value 3 is " + node);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,10 +9,10 @@ package chapter_searching;
|
||||
import java.util.*;
|
||||
|
||||
public class two_sum {
|
||||
/* Method one: Brute force enumeration */
|
||||
/* Method 1: Brute force enumeration */
|
||||
static int[] twoSumBruteForce(int[] nums, int target) {
|
||||
int size = nums.length;
|
||||
// Two-layer loop, time complexity is O(n^2)
|
||||
// Two nested loops, time complexity is O(n^2)
|
||||
for (int i = 0; i < size - 1; i++) {
|
||||
for (int j = i + 1; j < size; j++) {
|
||||
if (nums[i] + nums[j] == target)
|
||||
@@ -22,12 +22,12 @@ public class two_sum {
|
||||
return new int[0];
|
||||
}
|
||||
|
||||
/* Method two: Auxiliary hash table */
|
||||
/* Method 2: Auxiliary hash table */
|
||||
static int[] twoSumHashTable(int[] nums, int target) {
|
||||
int size = nums.length;
|
||||
// Auxiliary hash table, space complexity is O(n)
|
||||
Map<Integer, Integer> dic = new HashMap<>();
|
||||
// Single-layer loop, time complexity is O(n)
|
||||
// Single loop, time complexity is O(n)
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (dic.containsKey(target - nums[i])) {
|
||||
return new int[] { dic.get(target - nums[i]), i };
|
||||
@@ -43,11 +43,11 @@ public class two_sum {
|
||||
int target = 13;
|
||||
|
||||
// ====== Driver Code ======
|
||||
// Method one
|
||||
// Method 1
|
||||
int[] res = twoSumBruteForce(nums, target);
|
||||
System.out.println("Method one res = " + Arrays.toString(res));
|
||||
// Method two
|
||||
System.out.println("Method 1 res = " + Arrays.toString(res));
|
||||
// Method 2
|
||||
res = twoSumHashTable(nums, target);
|
||||
System.out.println("Method two res = " + Arrays.toString(res));
|
||||
System.out.println("Method 2 res = " + Arrays.toString(res));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ public class bubble_sort {
|
||||
static void bubbleSort(int[] nums) {
|
||||
// Outer loop: unsorted range is [0, i]
|
||||
for (int i = nums.length - 1; i > 0; i--) {
|
||||
// Inner loop: swap the largest element in the unsorted range [0, i] to the right end of the range
|
||||
// Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range
|
||||
for (int j = 0; j < i; j++) {
|
||||
if (nums[j] > nums[j + 1]) {
|
||||
// Swap nums[j] and nums[j + 1]
|
||||
@@ -25,33 +25,33 @@ public class bubble_sort {
|
||||
}
|
||||
}
|
||||
|
||||
/* Bubble sort (optimized with flag) */
|
||||
/* Bubble sort (flag optimization) */
|
||||
static void bubbleSortWithFlag(int[] nums) {
|
||||
// Outer loop: unsorted range is [0, i]
|
||||
for (int i = nums.length - 1; i > 0; i--) {
|
||||
boolean flag = false; // Initialize flag
|
||||
// Inner loop: swap the largest element in the unsorted range [0, i] to the right end of the range
|
||||
// Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range
|
||||
for (int j = 0; j < i; j++) {
|
||||
if (nums[j] > nums[j + 1]) {
|
||||
// Swap nums[j] and nums[j + 1]
|
||||
int tmp = nums[j];
|
||||
nums[j] = nums[j + 1];
|
||||
nums[j + 1] = tmp;
|
||||
flag = true; // Record swapped elements
|
||||
flag = true; // Record element swap
|
||||
}
|
||||
}
|
||||
if (!flag)
|
||||
break; // If no elements were swapped in this round of "bubbling", exit
|
||||
break; // No elements were swapped in this round of "bubbling", exit directly
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[] nums = { 4, 1, 3, 1, 5, 2 };
|
||||
bubbleSort(nums);
|
||||
System.out.println("After bubble sort, nums = " + Arrays.toString(nums));
|
||||
System.out.println("After bubble sort completes, nums = " + Arrays.toString(nums));
|
||||
|
||||
int[] nums1 = { 4, 1, 3, 1, 5, 2 };
|
||||
bubbleSortWithFlag(nums1);
|
||||
System.out.println("After bubble sort, nums1 = " + Arrays.toString(nums1));
|
||||
System.out.println("After bubble sort completes, nums1 = " + Arrays.toString(nums1));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,9 +39,9 @@ public class bucket_sort {
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
// Assume input data is floating point, range [0, 1)
|
||||
// Assume input data is floating point, interval [0, 1)
|
||||
float[] nums = { 0.49f, 0.96f, 0.82f, 0.09f, 0.57f, 0.43f, 0.91f, 0.75f, 0.15f, 0.37f };
|
||||
bucketSort(nums);
|
||||
System.out.println("After bucket sort, nums = " + Arrays.toString(nums));
|
||||
System.out.println("After bucket sort completes, nums = " + Arrays.toString(nums));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ public class counting_sort {
|
||||
for (int num : nums) {
|
||||
m = Math.max(m, num);
|
||||
}
|
||||
// 2. Count the occurrence of each digit
|
||||
// 2. Count the occurrence of each number
|
||||
// counter[num] represents the occurrence of num
|
||||
int[] counter = new int[m + 1];
|
||||
for (int num : nums) {
|
||||
@@ -40,7 +40,7 @@ public class counting_sort {
|
||||
for (int num : nums) {
|
||||
m = Math.max(m, num);
|
||||
}
|
||||
// 2. Count the occurrence of each digit
|
||||
// 2. Count the occurrence of each number
|
||||
// counter[num] represents the occurrence of num
|
||||
int[] counter = new int[m + 1];
|
||||
for (int num : nums) {
|
||||
@@ -69,10 +69,10 @@ public class counting_sort {
|
||||
public static void main(String[] args) {
|
||||
int[] nums = { 1, 0, 1, 2, 0, 4, 0, 2, 2, 4 };
|
||||
countingSortNaive(nums);
|
||||
System.out.println("After count sort (unable to sort objects), nums = " + Arrays.toString(nums));
|
||||
System.out.println("After counting sort (cannot sort objects) completes, nums = " + Arrays.toString(nums));
|
||||
|
||||
int[] nums1 = { 1, 0, 1, 2, 0, 4, 0, 2, 2, 4 };
|
||||
countingSort(nums1);
|
||||
System.out.println("After count sort, nums1 = " + Arrays.toString(nums1));
|
||||
System.out.println("After counting sort completes, nums1 = " + Arrays.toString(nums1));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ public class heap_sort {
|
||||
/* Heap length is n, start heapifying node i, from top to bottom */
|
||||
public static void siftDown(int[] nums, int n, int i) {
|
||||
while (true) {
|
||||
// Determine the largest node among i, l, r, noted as ma
|
||||
// If node i is largest or indices l, r are out of bounds, no need to continue heapify, break
|
||||
int l = 2 * i + 1;
|
||||
int r = 2 * i + 2;
|
||||
int ma = i;
|
||||
@@ -20,7 +20,7 @@ public class heap_sort {
|
||||
ma = l;
|
||||
if (r < n && nums[r] > nums[ma])
|
||||
ma = r;
|
||||
// If node i is the largest or indices l, r are out of bounds, no further heapification needed, break
|
||||
// Swap two nodes
|
||||
if (ma == i)
|
||||
break;
|
||||
// Swap two nodes
|
||||
@@ -40,7 +40,7 @@ public class heap_sort {
|
||||
}
|
||||
// Extract the largest element from the heap and repeat for n-1 rounds
|
||||
for (int i = nums.length - 1; i > 0; i--) {
|
||||
// Swap the root node with the rightmost leaf node (swap the first element with the last element)
|
||||
// Delete node
|
||||
int tmp = nums[0];
|
||||
nums[0] = nums[i];
|
||||
nums[i] = tmp;
|
||||
@@ -52,6 +52,6 @@ public class heap_sort {
|
||||
public static void main(String[] args) {
|
||||
int[] nums = { 4, 1, 3, 1, 5, 2 };
|
||||
heapSort(nums);
|
||||
System.out.println("After heap sort, nums = " + Arrays.toString(nums));
|
||||
System.out.println("After heap sort completes, nums = " + Arrays.toString(nums));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,10 +11,10 @@ import java.util.*;
|
||||
public class insertion_sort {
|
||||
/* Insertion sort */
|
||||
static void insertionSort(int[] nums) {
|
||||
// Outer loop: sorted range is [0, i-1]
|
||||
// Outer loop: sorted interval is [0, i-1]
|
||||
for (int i = 1; i < nums.length; i++) {
|
||||
int base = nums[i], j = i - 1;
|
||||
// Inner loop: insert base into the correct position within the sorted range [0, i-1]
|
||||
// Inner loop: insert base into the correct position within the sorted interval [0, i-1]
|
||||
while (j >= 0 && nums[j] > base) {
|
||||
nums[j + 1] = nums[j]; // Move nums[j] to the right by one position
|
||||
j--;
|
||||
@@ -26,6 +26,6 @@ public class insertion_sort {
|
||||
public static void main(String[] args) {
|
||||
int[] nums = { 4, 1, 3, 1, 5, 2 };
|
||||
insertionSort(nums);
|
||||
System.out.println("After insertion sort, nums = " + Arrays.toString(nums));
|
||||
System.out.println("After insertion sort completes, nums = " + Arrays.toString(nums));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ public class merge_sort {
|
||||
// Termination condition
|
||||
if (left >= right)
|
||||
return; // Terminate recursion when subarray length is 1
|
||||
// Partition stage
|
||||
// Divide and conquer stage
|
||||
int mid = left + (right - left) / 2; // Calculate midpoint
|
||||
mergeSort(nums, left, mid); // Recursively process the left subarray
|
||||
mergeSort(nums, mid + 1, right); // Recursively process the right subarray
|
||||
@@ -53,6 +53,6 @@ public class merge_sort {
|
||||
/* Merge sort */
|
||||
int[] nums = { 7, 3, 2, 6, 0, 1, 5, 4 };
|
||||
mergeSort(nums, 0, nums.length - 1);
|
||||
System.out.println("After merge sort, nums = " + Arrays.toString(nums));
|
||||
System.out.println("After merge sort completes, nums = " + Arrays.toString(nums));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ class QuickSort {
|
||||
nums[j] = tmp;
|
||||
}
|
||||
|
||||
/* Partition */
|
||||
/* Sentinel partition */
|
||||
static int partition(int[] nums, int left, int right) {
|
||||
// Use nums[left] as the pivot
|
||||
int i = left, j = right;
|
||||
@@ -37,7 +37,7 @@ class QuickSort {
|
||||
// Terminate recursion when subarray length is 1
|
||||
if (left >= right)
|
||||
return;
|
||||
// Partition
|
||||
// Sentinel partition
|
||||
int pivot = partition(nums, left, right);
|
||||
// Recursively process the left subarray and right subarray
|
||||
quickSort(nums, left, pivot - 1);
|
||||
@@ -64,7 +64,7 @@ class QuickSortMedian {
|
||||
return right;
|
||||
}
|
||||
|
||||
/* Partition (median of three) */
|
||||
/* Sentinel partition (median of three) */
|
||||
static int partition(int[] nums, int left, int right) {
|
||||
// Select the median of three candidate elements
|
||||
int med = medianThree(nums, left, (left + right) / 2, right);
|
||||
@@ -88,7 +88,7 @@ class QuickSortMedian {
|
||||
// Terminate recursion when subarray length is 1
|
||||
if (left >= right)
|
||||
return;
|
||||
// Partition
|
||||
// Sentinel partition
|
||||
int pivot = partition(nums, left, right);
|
||||
// Recursively process the left subarray and right subarray
|
||||
quickSort(nums, left, pivot - 1);
|
||||
@@ -96,7 +96,7 @@ class QuickSortMedian {
|
||||
}
|
||||
}
|
||||
|
||||
/* Quick sort class (tail recursion optimization) */
|
||||
/* Quick sort class (recursion depth optimization) */
|
||||
class QuickSortTailCall {
|
||||
/* Swap elements */
|
||||
static void swap(int[] nums, int i, int j) {
|
||||
@@ -105,7 +105,7 @@ class QuickSortTailCall {
|
||||
nums[j] = tmp;
|
||||
}
|
||||
|
||||
/* Partition */
|
||||
/* Sentinel partition */
|
||||
static int partition(int[] nums, int left, int right) {
|
||||
// Use nums[left] as the pivot
|
||||
int i = left, j = right;
|
||||
@@ -120,11 +120,11 @@ class QuickSortTailCall {
|
||||
return i; // Return the index of the pivot
|
||||
}
|
||||
|
||||
/* Quick sort (tail recursion optimization) */
|
||||
/* Quick sort (recursion depth optimization) */
|
||||
public static void quickSort(int[] nums, int left, int right) {
|
||||
// Terminate when subarray length is 1
|
||||
while (left < right) {
|
||||
// Partition operation
|
||||
// Sentinel partition operation
|
||||
int pivot = partition(nums, left, right);
|
||||
// Perform quick sort on the shorter of the two subarrays
|
||||
if (pivot - left < right - pivot) {
|
||||
@@ -143,16 +143,16 @@ public class quick_sort {
|
||||
/* Quick sort */
|
||||
int[] nums = { 2, 4, 1, 0, 3, 5 };
|
||||
QuickSort.quickSort(nums, 0, nums.length - 1);
|
||||
System.out.println("After quick sort, nums = " + Arrays.toString(nums));
|
||||
System.out.println("After quick sort completes, nums = " + Arrays.toString(nums));
|
||||
|
||||
/* Quick sort (median pivot optimization) */
|
||||
/* Quick sort (recursion depth optimization) */
|
||||
int[] nums1 = { 2, 4, 1, 0, 3, 5 };
|
||||
QuickSortMedian.quickSort(nums1, 0, nums1.length - 1);
|
||||
System.out.println("After quick sort with median pivot optimization, nums1 = " + Arrays.toString(nums1));
|
||||
System.out.println("After quick sort (median pivot optimization) completes, nums1 = " + Arrays.toString(nums1));
|
||||
|
||||
/* Quick sort (tail recursion optimization) */
|
||||
/* Quick sort (recursion depth optimization) */
|
||||
int[] nums2 = { 2, 4, 1, 0, 3, 5 };
|
||||
QuickSortTailCall.quickSort(nums2, 0, nums2.length - 1);
|
||||
System.out.println("After quick sort with tail recursion optimization, nums2 = " + Arrays.toString(nums2));
|
||||
System.out.println("After quick sort (recursion depth optimization) completes, nums2 = " + Arrays.toString(nums2));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,6 +64,6 @@ public class radix_sort {
|
||||
int[] nums = { 10546151, 35663510, 42865989, 34862445, 81883077,
|
||||
88906420, 72429244, 30524779, 82060337, 63832996 };
|
||||
radixSort(nums);
|
||||
System.out.println("After radix sort, nums = " + Arrays.toString(nums));
|
||||
System.out.println("After radix sort completes, nums = " + Arrays.toString(nums));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,15 +12,15 @@ public class selection_sort {
|
||||
/* Selection sort */
|
||||
public static void selectionSort(int[] nums) {
|
||||
int n = nums.length;
|
||||
// Outer loop: unsorted range is [i, n-1]
|
||||
// Outer loop: unsorted interval is [i, n-1]
|
||||
for (int i = 0; i < n - 1; i++) {
|
||||
// Inner loop: find the smallest element within the unsorted range
|
||||
// Inner loop: find the smallest element within the unsorted interval
|
||||
int k = i;
|
||||
for (int j = i + 1; j < n; j++) {
|
||||
if (nums[j] < nums[k])
|
||||
k = j; // Record the index of the smallest element
|
||||
}
|
||||
// Swap the smallest element with the first element of the unsorted range
|
||||
// Swap the smallest element with the first element of the unsorted interval
|
||||
int temp = nums[i];
|
||||
nums[i] = nums[k];
|
||||
nums[k] = temp;
|
||||
@@ -30,6 +30,6 @@ public class selection_sort {
|
||||
public static void main(String[] args) {
|
||||
int[] nums = { 4, 1, 3, 1, 5, 2 };
|
||||
selectionSort(nums);
|
||||
System.out.println("After selection sort, nums = " + Arrays.toString(nums));
|
||||
System.out.println("After selection sort completes, nums = " + Arrays.toString(nums));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,11 +8,11 @@ package chapter_stack_and_queue;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/* Double-ended queue class based on circular array */
|
||||
/* Double-ended queue based on circular array implementation */
|
||||
class ArrayDeque {
|
||||
private int[] nums; // Array used to store elements of the double-ended queue
|
||||
private int front; // Front pointer, pointing to the front element
|
||||
private int queSize; // Length of the double-ended queue
|
||||
private int[] nums; // Array for storing double-ended queue elements
|
||||
private int front; // Front pointer, points to the front of the queue element
|
||||
private int queSize; // Double-ended queue length
|
||||
|
||||
/* Constructor */
|
||||
public ArrayDeque(int capacity) {
|
||||
@@ -30,81 +30,81 @@ class ArrayDeque {
|
||||
return queSize;
|
||||
}
|
||||
|
||||
/* Determine if the double-ended queue is empty */
|
||||
/* Check if the double-ended queue is empty */
|
||||
public boolean isEmpty() {
|
||||
return queSize == 0;
|
||||
}
|
||||
|
||||
/* Calculate circular array index */
|
||||
private int index(int i) {
|
||||
// Implement circular array by modulo operation
|
||||
// When i exceeds the tail of the array, return to the head
|
||||
// When i exceeds the head of the array, return to the tail
|
||||
// Use modulo operation to wrap the array head and tail together
|
||||
// When i passes the tail of the array, return to the head
|
||||
// When i passes the head of the array, return to the tail
|
||||
return (i + capacity()) % capacity();
|
||||
}
|
||||
|
||||
/* Front enqueue */
|
||||
/* Front of the queue enqueue */
|
||||
public void pushFirst(int num) {
|
||||
if (queSize == capacity()) {
|
||||
System.out.println("Double-ended queue is full");
|
||||
return;
|
||||
}
|
||||
// Move the front pointer one position to the left
|
||||
// Implement front crossing the head of the array to return to the tail by modulo operation
|
||||
// Use modulo operation to wrap front around to the tail after passing the head of the array
|
||||
// Add num to the front of the queue
|
||||
front = index(front - 1);
|
||||
// Add num to the front
|
||||
// Add num to front of queue
|
||||
nums[front] = num;
|
||||
queSize++;
|
||||
}
|
||||
|
||||
/* Rear enqueue */
|
||||
/* Rear of the queue enqueue */
|
||||
public void pushLast(int num) {
|
||||
if (queSize == capacity()) {
|
||||
System.out.println("Double-ended queue is full");
|
||||
return;
|
||||
}
|
||||
// Calculate rear pointer, pointing to rear index + 1
|
||||
// Use modulo operation to wrap rear around to the head after passing the tail of the array
|
||||
int rear = index(front + queSize);
|
||||
// Add num to the rear
|
||||
// Front pointer moves one position backward
|
||||
nums[rear] = num;
|
||||
queSize++;
|
||||
}
|
||||
|
||||
/* Front dequeue */
|
||||
/* Rear of the queue dequeue */
|
||||
public int popFirst() {
|
||||
int num = peekFirst();
|
||||
// Move front pointer one position backward
|
||||
// Move front pointer backward by one position
|
||||
front = index(front + 1);
|
||||
queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* Rear dequeue */
|
||||
/* Access rear of the queue element */
|
||||
public int popLast() {
|
||||
int num = peekLast();
|
||||
queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* Access front element */
|
||||
/* Return list for printing */
|
||||
public int peekFirst() {
|
||||
if (isEmpty())
|
||||
throw new IndexOutOfBoundsException();
|
||||
return nums[front];
|
||||
}
|
||||
|
||||
/* Access rear element */
|
||||
/* Driver Code */
|
||||
public int peekLast() {
|
||||
if (isEmpty())
|
||||
throw new IndexOutOfBoundsException();
|
||||
// Calculate rear element index
|
||||
// Initialize double-ended queue
|
||||
int last = index(front + queSize - 1);
|
||||
return nums[last];
|
||||
}
|
||||
|
||||
/* Return array for printing */
|
||||
public int[] toArray() {
|
||||
// Only convert elements within valid length range
|
||||
// Elements enqueue
|
||||
int[] res = new int[queSize];
|
||||
for (int i = 0, j = front; i < queSize; i++, j++) {
|
||||
res[i] = nums[index(j)];
|
||||
@@ -115,37 +115,37 @@ class ArrayDeque {
|
||||
|
||||
public class array_deque {
|
||||
public static void main(String[] args) {
|
||||
/* Initialize double-ended queue */
|
||||
/* Get the length of the double-ended queue */
|
||||
ArrayDeque deque = new ArrayDeque(10);
|
||||
deque.pushLast(3);
|
||||
deque.pushLast(2);
|
||||
deque.pushLast(5);
|
||||
System.out.println("Double-ended queue deque = " + Arrays.toString(deque.toArray()));
|
||||
|
||||
/* Access element */
|
||||
/* Update element */
|
||||
int peekFirst = deque.peekFirst();
|
||||
System.out.println("Front element peekFirst = " + peekFirst);
|
||||
int peekLast = deque.peekLast();
|
||||
System.out.println("Back element peekLast = " + peekLast);
|
||||
System.out.println("Rear element peekLast = " + peekLast);
|
||||
|
||||
/* Element enqueue */
|
||||
/* Elements enqueue */
|
||||
deque.pushLast(4);
|
||||
System.out.println("Element 4 enqueued at the tail, deque = " + Arrays.toString(deque.toArray()));
|
||||
System.out.println("After element 4 enqueues at rear, deque = " + Arrays.toString(deque.toArray()));
|
||||
deque.pushFirst(1);
|
||||
System.out.println("Element 1 enqueued at the head, deque = " + Arrays.toString(deque.toArray()));
|
||||
System.out.println("After element 1 enqueues at front, deque = " + Arrays.toString(deque.toArray()));
|
||||
|
||||
/* Element dequeue */
|
||||
int popLast = deque.popLast();
|
||||
System.out.println("Deque tail element = " + popLast + ", after dequeuing from the tail" + Arrays.toString(deque.toArray()));
|
||||
System.out.println("Rear dequeue element = " + popLast + ", after rear dequeue, deque = " + Arrays.toString(deque.toArray()));
|
||||
int popFirst = deque.popFirst();
|
||||
System.out.println("Deque front element = " + popFirst + ", after dequeuing from the front" + Arrays.toString(deque.toArray()));
|
||||
System.out.println("Front dequeue element = " + popFirst + ", after front dequeue, deque = " + Arrays.toString(deque.toArray()));
|
||||
|
||||
/* Get the length of the double-ended queue */
|
||||
int size = deque.size();
|
||||
System.out.println("Length of the double-ended queue size = " + size);
|
||||
System.out.println("Double-ended queue length size = " + size);
|
||||
|
||||
/* Determine if the double-ended queue is empty */
|
||||
/* Check if the double-ended queue is empty */
|
||||
boolean isEmpty = deque.isEmpty();
|
||||
System.out.println("Is the double-ended queue empty = " + isEmpty);
|
||||
System.out.println("Double-ended queue is empty = " + isEmpty);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,10 +8,10 @@ package chapter_stack_and_queue;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/* Queue class based on circular array */
|
||||
/* Queue based on circular array implementation */
|
||||
class ArrayQueue {
|
||||
private int[] nums; // Array for storing queue elements
|
||||
private int front; // Front pointer, pointing to the front element
|
||||
private int front; // Front pointer, points to the front of the queue element
|
||||
private int queSize; // Queue length
|
||||
|
||||
public ArrayQueue(int capacity) {
|
||||
@@ -29,7 +29,7 @@ class ArrayQueue {
|
||||
return queSize;
|
||||
}
|
||||
|
||||
/* Determine if the queue is empty */
|
||||
/* Check if the queue is empty */
|
||||
public boolean isEmpty() {
|
||||
return queSize == 0;
|
||||
}
|
||||
@@ -40,10 +40,10 @@ class ArrayQueue {
|
||||
System.out.println("Queue is full");
|
||||
return;
|
||||
}
|
||||
// Calculate rear pointer, pointing to rear index + 1
|
||||
// Use modulo operation to wrap the rear pointer from the end of the array back to the start
|
||||
// Use modulo operation to wrap rear around to the head after passing the tail of the array
|
||||
// Add num to the rear of the queue
|
||||
int rear = (front + queSize) % capacity();
|
||||
// Add num to the rear
|
||||
// Front pointer moves one position backward
|
||||
nums[rear] = num;
|
||||
queSize++;
|
||||
}
|
||||
@@ -51,13 +51,13 @@ class ArrayQueue {
|
||||
/* Dequeue */
|
||||
public int pop() {
|
||||
int num = peek();
|
||||
// Move front pointer one position backward, returning to the head of the array if it exceeds the tail
|
||||
// Move front pointer backward by one position, if it passes the tail, return to array head
|
||||
front = (front + 1) % capacity();
|
||||
queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* Access front element */
|
||||
/* Return list for printing */
|
||||
public int peek() {
|
||||
if (isEmpty())
|
||||
throw new IndexOutOfBoundsException();
|
||||
@@ -66,7 +66,7 @@ class ArrayQueue {
|
||||
|
||||
/* Return array */
|
||||
public int[] toArray() {
|
||||
// Only convert elements within valid length range
|
||||
// Elements enqueue
|
||||
int[] res = new int[queSize];
|
||||
for (int i = 0, j = front; i < queSize; i++, j++) {
|
||||
res[i] = nums[j % capacity()];
|
||||
@@ -77,11 +77,11 @@ class ArrayQueue {
|
||||
|
||||
public class array_queue {
|
||||
public static void main(String[] args) {
|
||||
/* Initialize queue */
|
||||
/* Access front of the queue element */
|
||||
int capacity = 10;
|
||||
ArrayQueue queue = new ArrayQueue(capacity);
|
||||
|
||||
/* Element enqueue */
|
||||
/* Elements enqueue */
|
||||
queue.push(1);
|
||||
queue.push(3);
|
||||
queue.push(2);
|
||||
@@ -89,27 +89,27 @@ public class array_queue {
|
||||
queue.push(4);
|
||||
System.out.println("Queue queue = " + Arrays.toString(queue.toArray()));
|
||||
|
||||
/* Access front element */
|
||||
/* Return list for printing */
|
||||
int peek = queue.peek();
|
||||
System.out.println("Front element peek = " + peek);
|
||||
|
||||
/* Element dequeue */
|
||||
int pop = queue.pop();
|
||||
System.out.println("Dequeued element = " + pop + ", after dequeuing" + Arrays.toString(queue.toArray()));
|
||||
System.out.println("Dequeue element pop = " + pop + ", after dequeue, queue = " + Arrays.toString(queue.toArray()));
|
||||
|
||||
/* Get the length of the queue */
|
||||
int size = queue.size();
|
||||
System.out.println("Length of the queue size = " + size);
|
||||
System.out.println("Queue length size = " + size);
|
||||
|
||||
/* Determine if the queue is empty */
|
||||
/* Check if the queue is empty */
|
||||
boolean isEmpty = queue.isEmpty();
|
||||
System.out.println("Is the queue empty = " + isEmpty);
|
||||
System.out.println("Queue is empty = " + isEmpty);
|
||||
|
||||
/* Test circular array */
|
||||
for (int i = 0; i < 10; i++) {
|
||||
queue.push(i);
|
||||
queue.pop();
|
||||
System.out.println("After the " + i + "th round of enqueueing + dequeuing, queue = " + Arrays.toString(queue.toArray()));
|
||||
System.out.println("After round " + i + " enqueue + dequeue, queue = " + Arrays.toString(queue.toArray()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,12 +8,12 @@ package chapter_stack_and_queue;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/* Stack class based on array */
|
||||
/* Stack based on array implementation */
|
||||
class ArrayStack {
|
||||
private ArrayList<Integer> stack;
|
||||
|
||||
public ArrayStack() {
|
||||
// Initialize the list (dynamic array)
|
||||
// Initialize list (dynamic array)
|
||||
stack = new ArrayList<>();
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ class ArrayStack {
|
||||
return stack.size();
|
||||
}
|
||||
|
||||
/* Determine if the stack is empty */
|
||||
/* Check if the stack is empty */
|
||||
public boolean isEmpty() {
|
||||
return size() == 0;
|
||||
}
|
||||
@@ -39,14 +39,14 @@ class ArrayStack {
|
||||
return stack.remove(size() - 1);
|
||||
}
|
||||
|
||||
/* Access stack top element */
|
||||
/* Return list for printing */
|
||||
public int peek() {
|
||||
if (isEmpty())
|
||||
throw new IndexOutOfBoundsException();
|
||||
return stack.get(size() - 1);
|
||||
}
|
||||
|
||||
/* Convert the List to Array and return */
|
||||
/* Convert List to Array and return */
|
||||
public Object[] toArray() {
|
||||
return stack.toArray();
|
||||
}
|
||||
@@ -54,10 +54,10 @@ class ArrayStack {
|
||||
|
||||
public class array_stack {
|
||||
public static void main(String[] args) {
|
||||
/* Initialize stack */
|
||||
/* Access top of the stack element */
|
||||
ArrayStack stack = new ArrayStack();
|
||||
|
||||
/* Element push */
|
||||
/* Elements push onto stack */
|
||||
stack.push(1);
|
||||
stack.push(3);
|
||||
stack.push(2);
|
||||
@@ -65,20 +65,20 @@ public class array_stack {
|
||||
stack.push(4);
|
||||
System.out.println("Stack stack = " + Arrays.toString(stack.toArray()));
|
||||
|
||||
/* Access stack top element */
|
||||
/* Return list for printing */
|
||||
int peek = stack.peek();
|
||||
System.out.println("Top element peek = " + peek);
|
||||
System.out.println("Stack top element peek = " + peek);
|
||||
|
||||
/* Element pop */
|
||||
/* Element pop from stack */
|
||||
int pop = stack.pop();
|
||||
System.out.println("Popped element = " + pop + ", after popping" + Arrays.toString(stack.toArray()));
|
||||
System.out.println("Pop element pop = " + pop + ", after pop, stack = " + Arrays.toString(stack.toArray()));
|
||||
|
||||
/* Get the length of the stack */
|
||||
int size = stack.size();
|
||||
System.out.println("Length of the stack size = " + size);
|
||||
System.out.println("Stack length size = " + size);
|
||||
|
||||
/* Determine if it's empty */
|
||||
/* Check if empty */
|
||||
boolean isEmpty = stack.isEmpty();
|
||||
System.out.println("Is the stack empty = " + isEmpty);
|
||||
System.out.println("Stack is empty = " + isEmpty);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,37 +10,37 @@ import java.util.*;
|
||||
|
||||
public class deque {
|
||||
public static void main(String[] args) {
|
||||
/* Initialize double-ended queue */
|
||||
/* Get the length of the double-ended queue */
|
||||
Deque<Integer> deque = new LinkedList<>();
|
||||
deque.offerLast(3);
|
||||
deque.offerLast(2);
|
||||
deque.offerLast(5);
|
||||
System.out.println("Double-ended queue deque = " + deque);
|
||||
|
||||
/* Access element */
|
||||
/* Update element */
|
||||
int peekFirst = deque.peekFirst();
|
||||
System.out.println("Front element peekFirst = " + peekFirst);
|
||||
int peekLast = deque.peekLast();
|
||||
System.out.println("Back element peekLast = " + peekLast);
|
||||
System.out.println("Rear element peekLast = " + peekLast);
|
||||
|
||||
/* Element enqueue */
|
||||
/* Elements enqueue */
|
||||
deque.offerLast(4);
|
||||
System.out.println("Element 4 enqueued at the tail, deque = " + deque);
|
||||
System.out.println("After element 4 enqueues at rear, deque = " + deque);
|
||||
deque.offerFirst(1);
|
||||
System.out.println("Element 1 enqueued at the head, deque = " + deque);
|
||||
System.out.println("After element 1 enqueues at front, deque = " + deque);
|
||||
|
||||
/* Element dequeue */
|
||||
int popLast = deque.pollLast();
|
||||
System.out.println("Deque tail element = " + popLast + ", after dequeuing from the tail" + deque);
|
||||
System.out.println("Rear dequeue element = " + popLast + ", after rear dequeue, deque = " + deque);
|
||||
int popFirst = deque.pollFirst();
|
||||
System.out.println("Deque front element = " + popFirst + ", after dequeuing from the front" + deque);
|
||||
System.out.println("Front dequeue element = " + popFirst + ", after front dequeue, deque = " + deque);
|
||||
|
||||
/* Get the length of the double-ended queue */
|
||||
int size = deque.size();
|
||||
System.out.println("Length of the double-ended queue size = " + size);
|
||||
System.out.println("Double-ended queue length size = " + size);
|
||||
|
||||
/* Determine if the double-ended queue is empty */
|
||||
/* Check if the double-ended queue is empty */
|
||||
boolean isEmpty = deque.isEmpty();
|
||||
System.out.println("Is the double-ended queue empty = " + isEmpty);
|
||||
System.out.println("Double-ended queue is empty = " + isEmpty);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,11 +8,11 @@ package chapter_stack_and_queue;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/* Double-linked list node */
|
||||
/* Doubly linked list node */
|
||||
class ListNode {
|
||||
int val; // Node value
|
||||
ListNode next; // Reference to successor node
|
||||
ListNode prev; // Reference to predecessor node
|
||||
ListNode next; // Successor node reference
|
||||
ListNode prev; // Predecessor node reference
|
||||
|
||||
ListNode(int val) {
|
||||
this.val = val;
|
||||
@@ -20,9 +20,9 @@ class ListNode {
|
||||
}
|
||||
}
|
||||
|
||||
/* Double-ended queue class based on double-linked list */
|
||||
/* Double-ended queue based on doubly linked list implementation */
|
||||
class LinkedListDeque {
|
||||
private ListNode front, rear; // Front node front, back node rear
|
||||
private ListNode front, rear; // Head node front, tail node rear
|
||||
private int queSize = 0; // Length of the double-ended queue
|
||||
|
||||
public LinkedListDeque() {
|
||||
@@ -34,7 +34,7 @@ class LinkedListDeque {
|
||||
return queSize;
|
||||
}
|
||||
|
||||
/* Determine if the double-ended queue is empty */
|
||||
/* Check if the double-ended queue is empty */
|
||||
public boolean isEmpty() {
|
||||
return size() == 0;
|
||||
}
|
||||
@@ -42,18 +42,18 @@ class LinkedListDeque {
|
||||
/* Enqueue operation */
|
||||
private void push(int num, boolean isFront) {
|
||||
ListNode node = new ListNode(num);
|
||||
// If the list is empty, make front and rear both point to node
|
||||
// If the linked list is empty, make both front and rear point to node
|
||||
if (isEmpty())
|
||||
front = rear = node;
|
||||
// Front enqueue operation
|
||||
// Front of the queue enqueue operation
|
||||
else if (isFront) {
|
||||
// Add node to the head of the list
|
||||
// Add node to the head of the linked list
|
||||
front.prev = node;
|
||||
node.next = front;
|
||||
front = node; // Update head node
|
||||
// Rear enqueue operation
|
||||
// Rear of the queue enqueue operation
|
||||
} else {
|
||||
// Add node to the tail of the list
|
||||
// Add node to the tail of the linked list
|
||||
rear.next = node;
|
||||
node.prev = rear;
|
||||
rear = node; // Update tail node
|
||||
@@ -61,12 +61,12 @@ class LinkedListDeque {
|
||||
queSize++; // Update queue length
|
||||
}
|
||||
|
||||
/* Front enqueue */
|
||||
/* Front of the queue enqueue */
|
||||
public void pushFirst(int num) {
|
||||
push(num, true);
|
||||
}
|
||||
|
||||
/* Rear enqueue */
|
||||
/* Rear of the queue enqueue */
|
||||
public void pushLast(int num) {
|
||||
push(num, false);
|
||||
}
|
||||
@@ -76,20 +76,20 @@ class LinkedListDeque {
|
||||
if (isEmpty())
|
||||
throw new IndexOutOfBoundsException();
|
||||
int val;
|
||||
// Front dequeue operation
|
||||
// Temporarily store head node value
|
||||
if (isFront) {
|
||||
val = front.val; // Temporarily store the head node value
|
||||
// Remove head node
|
||||
val = front.val; // Delete head node
|
||||
// Delete head node
|
||||
ListNode fNext = front.next;
|
||||
if (fNext != null) {
|
||||
fNext.prev = null;
|
||||
front.next = null;
|
||||
}
|
||||
front = fNext; // Update head node
|
||||
// Rear dequeue operation
|
||||
// Temporarily store tail node value
|
||||
} else {
|
||||
val = rear.val; // Temporarily store the tail node value
|
||||
// Remove tail node
|
||||
val = rear.val; // Delete tail node
|
||||
// Update tail node
|
||||
ListNode rPrev = rear.prev;
|
||||
if (rPrev != null) {
|
||||
rPrev.next = null;
|
||||
@@ -101,24 +101,24 @@ class LinkedListDeque {
|
||||
return val;
|
||||
}
|
||||
|
||||
/* Front dequeue */
|
||||
/* Rear of the queue dequeue */
|
||||
public int popFirst() {
|
||||
return pop(true);
|
||||
}
|
||||
|
||||
/* Rear dequeue */
|
||||
/* Access rear of the queue element */
|
||||
public int popLast() {
|
||||
return pop(false);
|
||||
}
|
||||
|
||||
/* Access front element */
|
||||
/* Return list for printing */
|
||||
public int peekFirst() {
|
||||
if (isEmpty())
|
||||
throw new IndexOutOfBoundsException();
|
||||
return front.val;
|
||||
}
|
||||
|
||||
/* Access rear element */
|
||||
/* Driver Code */
|
||||
public int peekLast() {
|
||||
if (isEmpty())
|
||||
throw new IndexOutOfBoundsException();
|
||||
@@ -139,37 +139,37 @@ class LinkedListDeque {
|
||||
|
||||
public class linkedlist_deque {
|
||||
public static void main(String[] args) {
|
||||
/* Initialize double-ended queue */
|
||||
/* Get the length of the double-ended queue */
|
||||
LinkedListDeque deque = new LinkedListDeque();
|
||||
deque.pushLast(3);
|
||||
deque.pushLast(2);
|
||||
deque.pushLast(5);
|
||||
System.out.println("Double-ended queue deque = " + Arrays.toString(deque.toArray()));
|
||||
|
||||
/* Access element */
|
||||
/* Update element */
|
||||
int peekFirst = deque.peekFirst();
|
||||
System.out.println("Front element peekFirst = " + peekFirst);
|
||||
int peekLast = deque.peekLast();
|
||||
System.out.println("Back element peekLast = " + peekLast);
|
||||
System.out.println("Rear element peekLast = " + peekLast);
|
||||
|
||||
/* Element enqueue */
|
||||
/* Elements enqueue */
|
||||
deque.pushLast(4);
|
||||
System.out.println("Element 4 enqueued at the tail, deque = " + Arrays.toString(deque.toArray()));
|
||||
System.out.println("After element 4 enqueues at rear, deque = " + Arrays.toString(deque.toArray()));
|
||||
deque.pushFirst(1);
|
||||
System.out.println("Element 1 enqueued at the head, deque = " + Arrays.toString(deque.toArray()));
|
||||
System.out.println("After element 1 enqueues at front, deque = " + Arrays.toString(deque.toArray()));
|
||||
|
||||
/* Element dequeue */
|
||||
int popLast = deque.popLast();
|
||||
System.out.println("Deque tail element = " + popLast + ", after dequeuing from the tail" + Arrays.toString(deque.toArray()));
|
||||
System.out.println("Rear dequeue element = " + popLast + ", after rear dequeue, deque = " + Arrays.toString(deque.toArray()));
|
||||
int popFirst = deque.popFirst();
|
||||
System.out.println("Deque front element = " + popFirst + ", after dequeuing from the front" + Arrays.toString(deque.toArray()));
|
||||
System.out.println("Front dequeue element = " + popFirst + ", after front dequeue, deque = " + Arrays.toString(deque.toArray()));
|
||||
|
||||
/* Get the length of the double-ended queue */
|
||||
int size = deque.size();
|
||||
System.out.println("Length of the double-ended queue size = " + size);
|
||||
System.out.println("Double-ended queue length size = " + size);
|
||||
|
||||
/* Determine if the double-ended queue is empty */
|
||||
/* Check if the double-ended queue is empty */
|
||||
boolean isEmpty = deque.isEmpty();
|
||||
System.out.println("Is the double-ended queue empty = " + isEmpty);
|
||||
System.out.println("Double-ended queue is empty = " + isEmpty);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,9 +8,9 @@ package chapter_stack_and_queue;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/* Queue class based on linked list */
|
||||
/* Queue based on linked list implementation */
|
||||
class LinkedListQueue {
|
||||
private ListNode front, rear; // Front node front, back node rear
|
||||
private ListNode front, rear; // Head node front, tail node rear
|
||||
private int queSize = 0;
|
||||
|
||||
public LinkedListQueue() {
|
||||
@@ -23,20 +23,20 @@ class LinkedListQueue {
|
||||
return queSize;
|
||||
}
|
||||
|
||||
/* Determine if the queue is empty */
|
||||
/* Check if the queue is empty */
|
||||
public boolean isEmpty() {
|
||||
return size() == 0;
|
||||
}
|
||||
|
||||
/* Enqueue */
|
||||
public void push(int num) {
|
||||
// Add num behind the tail node
|
||||
// Add num after the tail node
|
||||
ListNode node = new ListNode(num);
|
||||
// If the queue is empty, make the head and tail nodes both point to that node
|
||||
// If the queue is empty, make both front and rear point to the node
|
||||
if (front == null) {
|
||||
front = node;
|
||||
rear = node;
|
||||
// If the queue is not empty, add that node behind the tail node
|
||||
// If the queue is not empty, add the node after the tail node
|
||||
} else {
|
||||
rear.next = node;
|
||||
rear = node;
|
||||
@@ -47,20 +47,20 @@ class LinkedListQueue {
|
||||
/* Dequeue */
|
||||
public int pop() {
|
||||
int num = peek();
|
||||
// Remove head node
|
||||
// Delete head node
|
||||
front = front.next;
|
||||
queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* Access front element */
|
||||
/* Return list for printing */
|
||||
public int peek() {
|
||||
if (isEmpty())
|
||||
throw new IndexOutOfBoundsException();
|
||||
return front.val;
|
||||
}
|
||||
|
||||
/* Convert the linked list to Array and return */
|
||||
/* Convert linked list to Array and return */
|
||||
public int[] toArray() {
|
||||
ListNode node = front;
|
||||
int[] res = new int[size()];
|
||||
@@ -74,10 +74,10 @@ class LinkedListQueue {
|
||||
|
||||
public class linkedlist_queue {
|
||||
public static void main(String[] args) {
|
||||
/* Initialize queue */
|
||||
/* Access front of the queue element */
|
||||
LinkedListQueue queue = new LinkedListQueue();
|
||||
|
||||
/* Element enqueue */
|
||||
/* Elements enqueue */
|
||||
queue.push(1);
|
||||
queue.push(3);
|
||||
queue.push(2);
|
||||
@@ -85,20 +85,20 @@ public class linkedlist_queue {
|
||||
queue.push(4);
|
||||
System.out.println("Queue queue = " + Arrays.toString(queue.toArray()));
|
||||
|
||||
/* Access front element */
|
||||
/* Return list for printing */
|
||||
int peek = queue.peek();
|
||||
System.out.println("Front element peek = " + peek);
|
||||
|
||||
/* Element dequeue */
|
||||
int pop = queue.pop();
|
||||
System.out.println("Dequeued element = " + pop + ", after dequeuing" + Arrays.toString(queue.toArray()));
|
||||
System.out.println("Dequeue element pop = " + pop + ", after dequeue, queue = " + Arrays.toString(queue.toArray()));
|
||||
|
||||
/* Get the length of the queue */
|
||||
int size = queue.size();
|
||||
System.out.println("Length of the queue size = " + size);
|
||||
System.out.println("Queue length size = " + size);
|
||||
|
||||
/* Determine if the queue is empty */
|
||||
/* Check if the queue is empty */
|
||||
boolean isEmpty = queue.isEmpty();
|
||||
System.out.println("Is the queue empty = " + isEmpty);
|
||||
System.out.println("Queue is empty = " + isEmpty);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,10 +9,10 @@ package chapter_stack_and_queue;
|
||||
import java.util.*;
|
||||
import utils.*;
|
||||
|
||||
/* Stack class based on linked list */
|
||||
/* Stack based on linked list implementation */
|
||||
class LinkedListStack {
|
||||
private ListNode stackPeek; // Use the head node as the top of the stack
|
||||
private int stkSize = 0; // Length of the stack
|
||||
private ListNode stackPeek; // Use head node as stack top
|
||||
private int stkSize = 0; // Stack length
|
||||
|
||||
public LinkedListStack() {
|
||||
stackPeek = null;
|
||||
@@ -23,7 +23,7 @@ class LinkedListStack {
|
||||
return stkSize;
|
||||
}
|
||||
|
||||
/* Determine if the stack is empty */
|
||||
/* Check if the stack is empty */
|
||||
public boolean isEmpty() {
|
||||
return size() == 0;
|
||||
}
|
||||
@@ -44,14 +44,14 @@ class LinkedListStack {
|
||||
return num;
|
||||
}
|
||||
|
||||
/* Access stack top element */
|
||||
/* Return list for printing */
|
||||
public int peek() {
|
||||
if (isEmpty())
|
||||
throw new IndexOutOfBoundsException();
|
||||
return stackPeek.val;
|
||||
}
|
||||
|
||||
/* Convert the List to Array and return */
|
||||
/* Convert List to Array and return */
|
||||
public int[] toArray() {
|
||||
ListNode node = stackPeek;
|
||||
int[] res = new int[size()];
|
||||
@@ -65,10 +65,10 @@ class LinkedListStack {
|
||||
|
||||
public class linkedlist_stack {
|
||||
public static void main(String[] args) {
|
||||
/* Initialize stack */
|
||||
/* Access top of the stack element */
|
||||
LinkedListStack stack = new LinkedListStack();
|
||||
|
||||
/* Element push */
|
||||
/* Elements push onto stack */
|
||||
stack.push(1);
|
||||
stack.push(3);
|
||||
stack.push(2);
|
||||
@@ -76,20 +76,20 @@ public class linkedlist_stack {
|
||||
stack.push(4);
|
||||
System.out.println("Stack stack = " + Arrays.toString(stack.toArray()));
|
||||
|
||||
/* Access stack top element */
|
||||
/* Return list for printing */
|
||||
int peek = stack.peek();
|
||||
System.out.println("Top element peek = " + peek);
|
||||
System.out.println("Stack top element peek = " + peek);
|
||||
|
||||
/* Element pop */
|
||||
/* Element pop from stack */
|
||||
int pop = stack.pop();
|
||||
System.out.println("Popped element = " + pop + ", after popping" + Arrays.toString(stack.toArray()));
|
||||
System.out.println("Pop element pop = " + pop + ", after pop, stack = " + Arrays.toString(stack.toArray()));
|
||||
|
||||
/* Get the length of the stack */
|
||||
int size = stack.size();
|
||||
System.out.println("Length of the stack size = " + size);
|
||||
System.out.println("Stack length size = " + size);
|
||||
|
||||
/* Determine if it's empty */
|
||||
/* Check if empty */
|
||||
boolean isEmpty = stack.isEmpty();
|
||||
System.out.println("Is the stack empty = " + isEmpty);
|
||||
System.out.println("Stack is empty = " + isEmpty);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,10 +10,10 @@ import java.util.*;
|
||||
|
||||
public class queue {
|
||||
public static void main(String[] args) {
|
||||
/* Initialize queue */
|
||||
/* Access front of the queue element */
|
||||
Queue<Integer> queue = new LinkedList<>();
|
||||
|
||||
/* Element enqueue */
|
||||
/* Elements enqueue */
|
||||
queue.offer(1);
|
||||
queue.offer(3);
|
||||
queue.offer(2);
|
||||
@@ -21,20 +21,20 @@ public class queue {
|
||||
queue.offer(4);
|
||||
System.out.println("Queue queue = " + queue);
|
||||
|
||||
/* Access front element */
|
||||
/* Return list for printing */
|
||||
int peek = queue.peek();
|
||||
System.out.println("Front element peek = " + peek);
|
||||
|
||||
/* Element dequeue */
|
||||
int pop = queue.poll();
|
||||
System.out.println("Dequeued element = " + pop + ", after dequeuing" + queue);
|
||||
System.out.println("Dequeue element pop = " + pop + ", after dequeue, queue = " + queue);
|
||||
|
||||
/* Get the length of the queue */
|
||||
int size = queue.size();
|
||||
System.out.println("Length of the queue size = " + size);
|
||||
System.out.println("Queue length size = " + size);
|
||||
|
||||
/* Determine if the queue is empty */
|
||||
/* Check if the queue is empty */
|
||||
boolean isEmpty = queue.isEmpty();
|
||||
System.out.println("Is the queue empty = " + isEmpty);
|
||||
System.out.println("Queue is empty = " + isEmpty);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,10 +10,10 @@ import java.util.*;
|
||||
|
||||
public class stack {
|
||||
public static void main(String[] args) {
|
||||
/* Initialize stack */
|
||||
/* Access top of the stack element */
|
||||
Stack<Integer> stack = new Stack<>();
|
||||
|
||||
/* Element push */
|
||||
/* Elements push onto stack */
|
||||
stack.push(1);
|
||||
stack.push(3);
|
||||
stack.push(2);
|
||||
@@ -21,20 +21,20 @@ public class stack {
|
||||
stack.push(4);
|
||||
System.out.println("Stack stack = " + stack);
|
||||
|
||||
/* Access stack top element */
|
||||
/* Return list for printing */
|
||||
int peek = stack.peek();
|
||||
System.out.println("Top element peek = " + peek);
|
||||
System.out.println("Stack top element peek = " + peek);
|
||||
|
||||
/* Element pop */
|
||||
/* Element pop from stack */
|
||||
int pop = stack.pop();
|
||||
System.out.println("Popped element = " + pop + ", after popping" + stack);
|
||||
System.out.println("Pop element pop = " + pop + ", after pop, stack = " + stack);
|
||||
|
||||
/* Get the length of the stack */
|
||||
int size = stack.size();
|
||||
System.out.println("Length of the stack size = " + size);
|
||||
System.out.println("Stack length size = " + size);
|
||||
|
||||
/* Determine if it's empty */
|
||||
/* Check if empty */
|
||||
boolean isEmpty = stack.isEmpty();
|
||||
System.out.println("Is the stack empty = " + isEmpty);
|
||||
System.out.println("Stack is empty = " + isEmpty);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ package chapter_tree;
|
||||
import utils.*;
|
||||
import java.util.*;
|
||||
|
||||
/* Array-based binary tree class */
|
||||
/* Binary tree class represented by array */
|
||||
class ArrayBinaryTree {
|
||||
private List<Integer> tree;
|
||||
|
||||
@@ -23,25 +23,25 @@ class ArrayBinaryTree {
|
||||
return tree.size();
|
||||
}
|
||||
|
||||
/* Get the value of the node at index i */
|
||||
/* Get value of node at index i */
|
||||
public Integer val(int i) {
|
||||
// If the index is out of bounds, return null, representing an empty spot
|
||||
// If index out of bounds, return null to represent empty position
|
||||
if (i < 0 || i >= size())
|
||||
return null;
|
||||
return tree.get(i);
|
||||
}
|
||||
|
||||
/* Get the index of the left child of the node at index i */
|
||||
/* Get index of left child node of node at index i */
|
||||
public Integer left(int i) {
|
||||
return 2 * i + 1;
|
||||
}
|
||||
|
||||
/* Get the index of the right child of the node at index i */
|
||||
/* Get index of right child node of node at index i */
|
||||
public Integer right(int i) {
|
||||
return 2 * i + 2;
|
||||
}
|
||||
|
||||
/* Get the index of the parent of the node at index i */
|
||||
/* Get index of parent node of node at index i */
|
||||
public Integer parent(int i) {
|
||||
return (i - 1) / 2;
|
||||
}
|
||||
@@ -49,7 +49,7 @@ class ArrayBinaryTree {
|
||||
/* Level-order traversal */
|
||||
public List<Integer> levelOrder() {
|
||||
List<Integer> res = new ArrayList<>();
|
||||
// Traverse array
|
||||
// Traverse array directly
|
||||
for (int i = 0; i < size(); i++) {
|
||||
if (val(i) != null)
|
||||
res.add(val(i));
|
||||
@@ -59,37 +59,37 @@ class ArrayBinaryTree {
|
||||
|
||||
/* Depth-first traversal */
|
||||
private void dfs(Integer i, String order, List<Integer> res) {
|
||||
// If it is an empty spot, return
|
||||
// If empty position, return
|
||||
if (val(i) == null)
|
||||
return;
|
||||
// Pre-order traversal
|
||||
// Preorder traversal
|
||||
if ("pre".equals(order))
|
||||
res.add(val(i));
|
||||
dfs(left(i), order, res);
|
||||
// In-order traversal
|
||||
// Inorder traversal
|
||||
if ("in".equals(order))
|
||||
res.add(val(i));
|
||||
dfs(right(i), order, res);
|
||||
// Post-order traversal
|
||||
// Postorder traversal
|
||||
if ("post".equals(order))
|
||||
res.add(val(i));
|
||||
}
|
||||
|
||||
/* Pre-order traversal */
|
||||
/* Preorder traversal */
|
||||
public List<Integer> preOrder() {
|
||||
List<Integer> res = new ArrayList<>();
|
||||
dfs(0, "pre", res);
|
||||
return res;
|
||||
}
|
||||
|
||||
/* In-order traversal */
|
||||
/* Inorder traversal */
|
||||
public List<Integer> inOrder() {
|
||||
List<Integer> res = new ArrayList<>();
|
||||
dfs(0, "in", res);
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Post-order traversal */
|
||||
/* Postorder traversal */
|
||||
public List<Integer> postOrder() {
|
||||
List<Integer> res = new ArrayList<>();
|
||||
dfs(0, "post", res);
|
||||
@@ -100,17 +100,17 @@ class ArrayBinaryTree {
|
||||
public class array_binary_tree {
|
||||
public static void main(String[] args) {
|
||||
// Initialize binary tree
|
||||
// Use a specific function to convert an array into a binary tree
|
||||
// Here we use a function to generate a binary tree directly from an array
|
||||
List<Integer> arr = Arrays.asList(1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15);
|
||||
|
||||
TreeNode root = TreeNode.listToTree(arr);
|
||||
System.out.println("\nInitialize binary tree\n");
|
||||
System.out.println("Array representation of the binary tree:");
|
||||
System.out.println("Array representation of binary tree:");
|
||||
System.out.println(arr);
|
||||
System.out.println("Linked list representation of the binary tree:");
|
||||
System.out.println("Linked list representation of binary tree:");
|
||||
PrintUtil.printTree(root);
|
||||
|
||||
// Array-based binary tree class
|
||||
// Binary tree class represented by array
|
||||
ArrayBinaryTree abt = new ArrayBinaryTree(arr);
|
||||
|
||||
// Access node
|
||||
@@ -118,19 +118,19 @@ public class array_binary_tree {
|
||||
Integer l = abt.left(i);
|
||||
Integer r = abt.right(i);
|
||||
Integer p = abt.parent(i);
|
||||
System.out.println("\nThe current node's index is " + i + ", value = " + abt.val(i));
|
||||
System.out.println("Its left child's index is " + l + ", value = " + (l == null ? "null" : abt.val(l)));
|
||||
System.out.println("Its right child's index is " + r + ", value = " + (r == null ? "null" : abt.val(r)));
|
||||
System.out.println("Its parent's index is " + p + ", value = " + (p == null ? "null" : abt.val(p)));
|
||||
System.out.println("\nCurrent node index is " + i + ", value is " + abt.val(i));
|
||||
System.out.println("Its left child node index is " + l + ", value is " + (l == null ? "null" : abt.val(l)));
|
||||
System.out.println("Its right child node index is " + r + ", value is " + (r == null ? "null" : abt.val(r)));
|
||||
System.out.println("Its parent node index is " + p + ", value is " + (p == null ? "null" : abt.val(p)));
|
||||
|
||||
// Traverse tree
|
||||
List<Integer> res = abt.levelOrder();
|
||||
System.out.println("\nLevel-order traversal is:" + res);
|
||||
res = abt.preOrder();
|
||||
System.out.println("Pre-order traversal is:" + res);
|
||||
System.out.println("Preorder traversal is:" + res);
|
||||
res = abt.inOrder();
|
||||
System.out.println("In-order traversal is:" + res);
|
||||
System.out.println("Inorder traversal is:" + res);
|
||||
res = abt.postOrder();
|
||||
System.out.println("Post-order traversal is:" + res);
|
||||
System.out.println("Postorder traversal is:" + res);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,13 +37,13 @@ class AVLTree {
|
||||
private TreeNode rightRotate(TreeNode node) {
|
||||
TreeNode child = node.left;
|
||||
TreeNode grandChild = child.right;
|
||||
// Rotate node to the right around child
|
||||
// Using child as pivot, rotate node to the right
|
||||
child.right = node;
|
||||
node.left = grandChild;
|
||||
// Update node height
|
||||
updateHeight(node);
|
||||
updateHeight(child);
|
||||
// Return the root of the subtree after rotation
|
||||
// Return root node of subtree after rotation
|
||||
return child;
|
||||
}
|
||||
|
||||
@@ -51,19 +51,19 @@ class AVLTree {
|
||||
private TreeNode leftRotate(TreeNode node) {
|
||||
TreeNode child = node.right;
|
||||
TreeNode grandChild = child.left;
|
||||
// Rotate node to the left around child
|
||||
// Using child as pivot, rotate node to the left
|
||||
child.left = node;
|
||||
node.right = grandChild;
|
||||
// Update node height
|
||||
updateHeight(node);
|
||||
updateHeight(child);
|
||||
// Return the root of the subtree after rotation
|
||||
// Return root node of subtree after rotation
|
||||
return child;
|
||||
}
|
||||
|
||||
/* Perform rotation operation to restore balance to the subtree */
|
||||
/* Perform rotation operation to restore balance to this subtree */
|
||||
private TreeNode rotate(TreeNode node) {
|
||||
// Get the balance factor of node
|
||||
// Get balance factor of node
|
||||
int balanceFactor = balanceFactor(node);
|
||||
// Left-leaning tree
|
||||
if (balanceFactor > 1) {
|
||||
@@ -87,7 +87,7 @@ class AVLTree {
|
||||
return leftRotate(node);
|
||||
}
|
||||
}
|
||||
// Balanced tree, no rotation needed, return
|
||||
// Balanced tree, no rotation needed, return directly
|
||||
return node;
|
||||
}
|
||||
|
||||
@@ -106,11 +106,11 @@ class AVLTree {
|
||||
else if (val > node.val)
|
||||
node.right = insertHelper(node.right, val);
|
||||
else
|
||||
return node; // Do not insert duplicate nodes, return
|
||||
return node; // Duplicate node not inserted, return directly
|
||||
updateHeight(node); // Update node height
|
||||
/* 2. Perform rotation operation to restore balance to the subtree */
|
||||
/* 2. Perform rotation operation to restore balance to this subtree */
|
||||
node = rotate(node);
|
||||
// Return the root node of the subtree
|
||||
// Return root node of subtree
|
||||
return node;
|
||||
}
|
||||
|
||||
@@ -119,11 +119,11 @@ class AVLTree {
|
||||
root = removeHelper(root, val);
|
||||
}
|
||||
|
||||
/* Recursively remove node (helper method) */
|
||||
/* Recursively delete node (helper method) */
|
||||
private TreeNode removeHelper(TreeNode node, int val) {
|
||||
if (node == null)
|
||||
return null;
|
||||
/* 1. Find and remove the node */
|
||||
/* 1. Find node and delete */
|
||||
if (val < node.val)
|
||||
node.left = removeHelper(node.left, val);
|
||||
else if (val > node.val)
|
||||
@@ -131,14 +131,14 @@ class AVLTree {
|
||||
else {
|
||||
if (node.left == null || node.right == null) {
|
||||
TreeNode child = node.left != null ? node.left : node.right;
|
||||
// Number of child nodes = 0, remove node and return
|
||||
// Number of child nodes = 0, delete node directly and return
|
||||
if (child == null)
|
||||
return null;
|
||||
// Number of child nodes = 1, remove node
|
||||
// Number of child nodes = 1, delete node directly
|
||||
else
|
||||
node = child;
|
||||
} else {
|
||||
// Number of child nodes = 2, remove the next node in in-order traversal and replace the current node with it
|
||||
// Number of child nodes = 2, delete the next node in inorder traversal and replace current node with it
|
||||
TreeNode temp = node.right;
|
||||
while (temp.left != null) {
|
||||
temp = temp.left;
|
||||
@@ -148,16 +148,16 @@ class AVLTree {
|
||||
}
|
||||
}
|
||||
updateHeight(node); // Update node height
|
||||
/* 2. Perform rotation operation to restore balance to the subtree */
|
||||
/* 2. Perform rotation operation to restore balance to this subtree */
|
||||
node = rotate(node);
|
||||
// Return the root node of the subtree
|
||||
// Return root node of subtree
|
||||
return node;
|
||||
}
|
||||
|
||||
/* Search node */
|
||||
public TreeNode search(int val) {
|
||||
TreeNode cur = root;
|
||||
// Loop find, break after passing leaf nodes
|
||||
// Loop search, exit after passing leaf node
|
||||
while (cur != null) {
|
||||
// Target node is in cur's right subtree
|
||||
if (cur.val < val)
|
||||
@@ -165,7 +165,7 @@ class AVLTree {
|
||||
// Target node is in cur's left subtree
|
||||
else if (cur.val > val)
|
||||
cur = cur.left;
|
||||
// Found target node, break loop
|
||||
// Found target node, exit loop
|
||||
else
|
||||
break;
|
||||
}
|
||||
@@ -177,22 +177,22 @@ class AVLTree {
|
||||
public class avl_tree {
|
||||
static void testInsert(AVLTree tree, int val) {
|
||||
tree.insert(val);
|
||||
System.out.println("\nAfter inserting node " + val + ", the AVL tree is ");
|
||||
System.out.println("\nAfter inserting node " + val + ", AVL tree is");
|
||||
PrintUtil.printTree(tree.root);
|
||||
}
|
||||
|
||||
static void testRemove(AVLTree tree, int val) {
|
||||
tree.remove(val);
|
||||
System.out.println("\nAfter removing node " + val + ", the AVL tree is ");
|
||||
System.out.println("\nAfter removing node " + val + ", AVL tree is");
|
||||
PrintUtil.printTree(tree.root);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
/* Initialize empty AVL tree */
|
||||
/* Please pay attention to how the AVL tree maintains balance after inserting nodes */
|
||||
AVLTree avlTree = new AVLTree();
|
||||
|
||||
/* Insert node */
|
||||
// Notice how the AVL tree maintains balance after inserting nodes
|
||||
// Delete nodes
|
||||
testInsert(avlTree, 1);
|
||||
testInsert(avlTree, 2);
|
||||
testInsert(avlTree, 3);
|
||||
@@ -204,17 +204,17 @@ public class avl_tree {
|
||||
testInsert(avlTree, 10);
|
||||
testInsert(avlTree, 6);
|
||||
|
||||
/* Insert duplicate node */
|
||||
/* Please pay attention to how the AVL tree maintains balance after deleting nodes */
|
||||
testInsert(avlTree, 7);
|
||||
|
||||
/* Remove node */
|
||||
// Notice how the AVL tree maintains balance after removing nodes
|
||||
testRemove(avlTree, 8); // Remove node with degree 0
|
||||
// Delete node with degree 1
|
||||
testRemove(avlTree, 8); // Delete node with degree 2
|
||||
testRemove(avlTree, 5); // Remove node with degree 1
|
||||
testRemove(avlTree, 4); // Remove node with degree 2
|
||||
|
||||
/* Search node */
|
||||
TreeNode node = avlTree.search(7);
|
||||
System.out.println("\nThe found node object is " + node + ", node value = " + node.val);
|
||||
System.out.println("\nFound node object is " + node + ", node value = " + node.val);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ class BinarySearchTree {
|
||||
/* Search node */
|
||||
public TreeNode search(int num) {
|
||||
TreeNode cur = root;
|
||||
// Loop find, break after passing leaf nodes
|
||||
// Loop search, exit after passing leaf node
|
||||
while (cur != null) {
|
||||
// Target node is in cur's right subtree
|
||||
if (cur.val < num)
|
||||
@@ -34,7 +34,7 @@ class BinarySearchTree {
|
||||
// Target node is in cur's left subtree
|
||||
else if (cur.val > num)
|
||||
cur = cur.left;
|
||||
// Found target node, break loop
|
||||
// Found target node, exit loop
|
||||
else
|
||||
break;
|
||||
}
|
||||
@@ -50,9 +50,9 @@ class BinarySearchTree {
|
||||
return;
|
||||
}
|
||||
TreeNode cur = root, pre = null;
|
||||
// Loop find, break after passing leaf nodes
|
||||
// Loop search, exit after passing leaf node
|
||||
while (cur != null) {
|
||||
// Found duplicate node, thus return
|
||||
// Found duplicate node, return directly
|
||||
if (cur.val == num)
|
||||
return;
|
||||
pre = cur;
|
||||
@@ -73,49 +73,49 @@ class BinarySearchTree {
|
||||
|
||||
/* Remove node */
|
||||
public void remove(int num) {
|
||||
// If tree is empty, return
|
||||
// If tree is empty, return directly
|
||||
if (root == null)
|
||||
return;
|
||||
TreeNode cur = root, pre = null;
|
||||
// Loop find, break after passing leaf nodes
|
||||
// Loop search, exit after passing leaf node
|
||||
while (cur != null) {
|
||||
// Found node to be removed, break loop
|
||||
// Found node to delete, exit loop
|
||||
if (cur.val == num)
|
||||
break;
|
||||
pre = cur;
|
||||
// Node to be removed is in cur's right subtree
|
||||
// Node to delete is in cur's right subtree
|
||||
if (cur.val < num)
|
||||
cur = cur.right;
|
||||
// Node to be removed is in cur's left subtree
|
||||
// Node to delete is in cur's left subtree
|
||||
else
|
||||
cur = cur.left;
|
||||
}
|
||||
// If no node to be removed, return
|
||||
// If no node to delete, return directly
|
||||
if (cur == null)
|
||||
return;
|
||||
// Number of child nodes = 0 or 1
|
||||
if (cur.left == null || cur.right == null) {
|
||||
// When the number of child nodes = 0/1, child = null/that child node
|
||||
// When number of child nodes = 0 / 1, child = null / that child node
|
||||
TreeNode child = cur.left != null ? cur.left : cur.right;
|
||||
// Remove node cur
|
||||
// Delete node cur
|
||||
if (cur != root) {
|
||||
if (pre.left == cur)
|
||||
pre.left = child;
|
||||
else
|
||||
pre.right = child;
|
||||
} else {
|
||||
// If the removed node is the root, reassign the root
|
||||
// If deleted node is root node, reassign root node
|
||||
root = child;
|
||||
}
|
||||
}
|
||||
// Number of child nodes = 2
|
||||
else {
|
||||
// Get the next node in in-order traversal of cur
|
||||
// Get next node of cur in inorder traversal
|
||||
TreeNode tmp = cur.right;
|
||||
while (tmp.left != null) {
|
||||
tmp = tmp.left;
|
||||
}
|
||||
// Recursively remove node tmp
|
||||
// Recursively delete node tmp
|
||||
remove(tmp.val);
|
||||
// Replace cur with tmp
|
||||
cur.val = tmp.val;
|
||||
@@ -127,7 +127,7 @@ public class binary_search_tree {
|
||||
public static void main(String[] args) {
|
||||
/* Initialize binary search tree */
|
||||
BinarySearchTree bst = new BinarySearchTree();
|
||||
// Note that different insertion orders can result in various tree structures. This particular sequence creates a perfect binary tree
|
||||
// Please note that different insertion orders will generate different binary trees, this sequence can generate a perfect binary tree
|
||||
int[] nums = { 8, 4, 12, 2, 6, 10, 14, 1, 3, 5, 7, 9, 11, 13, 15 };
|
||||
for (int num : nums) {
|
||||
bst.insert(num);
|
||||
@@ -137,22 +137,22 @@ public class binary_search_tree {
|
||||
|
||||
/* Search node */
|
||||
TreeNode node = bst.search(7);
|
||||
System.out.println("\nThe found node object is " + node + ", node value = " + node.val);
|
||||
System.out.println("\nFound node object is " + node + ", node value = " + node.val);
|
||||
|
||||
/* Insert node */
|
||||
bst.insert(16);
|
||||
System.out.println("\nAfter inserting node 16, the binary tree is\n");
|
||||
System.out.println("\nAfter inserting node 16, binary tree is\n");
|
||||
PrintUtil.printTree(bst.getRoot());
|
||||
|
||||
/* Remove node */
|
||||
bst.remove(1);
|
||||
System.out.println("\nAfter removing node 1, the binary tree is\n");
|
||||
System.out.println("\nAfter removing node 1, binary tree is\n");
|
||||
PrintUtil.printTree(bst.getRoot());
|
||||
bst.remove(2);
|
||||
System.out.println("\nAfter removing node 2, the binary tree is\n");
|
||||
System.out.println("\nAfter removing node 2, binary tree is\n");
|
||||
PrintUtil.printTree(bst.getRoot());
|
||||
bst.remove(4);
|
||||
System.out.println("\nAfter removing node 4, the binary tree is\n");
|
||||
System.out.println("\nAfter removing node 4, binary tree is\n");
|
||||
PrintUtil.printTree(bst.getRoot());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,13 +11,13 @@ import utils.*;
|
||||
public class binary_tree {
|
||||
public static void main(String[] args) {
|
||||
/* Initialize binary tree */
|
||||
// Initialize node
|
||||
// Initialize nodes
|
||||
TreeNode n1 = new TreeNode(1);
|
||||
TreeNode n2 = new TreeNode(2);
|
||||
TreeNode n3 = new TreeNode(3);
|
||||
TreeNode n4 = new TreeNode(4);
|
||||
TreeNode n5 = new TreeNode(5);
|
||||
// Construct node references (pointers)
|
||||
// Build references (pointers) between nodes
|
||||
n1.left = n2;
|
||||
n1.right = n3;
|
||||
n2.left = n4;
|
||||
@@ -25,9 +25,9 @@ public class binary_tree {
|
||||
System.out.println("\nInitialize binary tree\n");
|
||||
PrintUtil.printTree(n1);
|
||||
|
||||
/* Insert and remove nodes */
|
||||
/* Insert node P between n1 -> n2 */
|
||||
TreeNode P = new TreeNode(0);
|
||||
// Insert node P between n1 -> n2
|
||||
// Delete node
|
||||
n1.left = P;
|
||||
P.left = n2;
|
||||
System.out.println("\nAfter inserting node P\n");
|
||||
|
||||
@@ -15,28 +15,28 @@ public class binary_tree_bfs {
|
||||
// Initialize queue, add root node
|
||||
Queue<TreeNode> queue = new LinkedList<>();
|
||||
queue.add(root);
|
||||
// Initialize a list to store the traversal sequence
|
||||
// Initialize a list to save the traversal sequence
|
||||
List<Integer> list = new ArrayList<>();
|
||||
while (!queue.isEmpty()) {
|
||||
TreeNode node = queue.poll(); // Queue dequeues
|
||||
TreeNode node = queue.poll(); // Dequeue
|
||||
list.add(node.val); // Save node value
|
||||
if (node.left != null)
|
||||
queue.offer(node.left); // Left child node enqueues
|
||||
queue.offer(node.left); // Left child node enqueue
|
||||
if (node.right != null)
|
||||
queue.offer(node.right); // Right child node enqueues
|
||||
queue.offer(node.right); // Right child node enqueue
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
/* Initialize binary tree */
|
||||
// Use a specific function to convert an array into a binary tree
|
||||
// Here we use a function to generate a binary tree directly from an array
|
||||
TreeNode root = TreeNode.listToTree(Arrays.asList(1, 2, 3, 4, 5, 6, 7));
|
||||
System.out.println("\nInitialize binary tree\n");
|
||||
PrintUtil.printTree(root);
|
||||
|
||||
/* Level-order traversal */
|
||||
List<Integer> list = levelOrder(root);
|
||||
System.out.println("\nPrint sequence of nodes from level-order traversal = " + list);
|
||||
System.out.println("\nLevel-order traversal node print sequence = " + list);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,10 +10,10 @@ import utils.*;
|
||||
import java.util.*;
|
||||
|
||||
public class binary_tree_dfs {
|
||||
// Initialize the list for storing traversal sequences
|
||||
// Initialize list for storing traversal sequence
|
||||
static ArrayList<Integer> list = new ArrayList<>();
|
||||
|
||||
/* Pre-order traversal */
|
||||
/* Preorder traversal */
|
||||
static void preOrder(TreeNode root) {
|
||||
if (root == null)
|
||||
return;
|
||||
@@ -23,7 +23,7 @@ public class binary_tree_dfs {
|
||||
preOrder(root.right);
|
||||
}
|
||||
|
||||
/* In-order traversal */
|
||||
/* Inorder traversal */
|
||||
static void inOrder(TreeNode root) {
|
||||
if (root == null)
|
||||
return;
|
||||
@@ -33,7 +33,7 @@ public class binary_tree_dfs {
|
||||
inOrder(root.right);
|
||||
}
|
||||
|
||||
/* Post-order traversal */
|
||||
/* Postorder traversal */
|
||||
static void postOrder(TreeNode root) {
|
||||
if (root == null)
|
||||
return;
|
||||
@@ -45,24 +45,24 @@ public class binary_tree_dfs {
|
||||
|
||||
public static void main(String[] args) {
|
||||
/* Initialize binary tree */
|
||||
// Use a specific function to convert an array into a binary tree
|
||||
// Here we use a function to generate a binary tree directly from an array
|
||||
TreeNode root = TreeNode.listToTree(Arrays.asList(1, 2, 3, 4, 5, 6, 7));
|
||||
System.out.println("\nInitialize binary tree\n");
|
||||
PrintUtil.printTree(root);
|
||||
|
||||
/* Pre-order traversal */
|
||||
/* Preorder traversal */
|
||||
list.clear();
|
||||
preOrder(root);
|
||||
System.out.println("\nPrint sequence of nodes from pre-order traversal = " + list);
|
||||
System.out.println("\nPreorder traversal node print sequence = " + list);
|
||||
|
||||
/* In-order traversal */
|
||||
/* Inorder traversal */
|
||||
list.clear();
|
||||
inOrder(root);
|
||||
System.out.println("\nPrint sequence of nodes from in-order traversal = " + list);
|
||||
System.out.println("\nInorder traversal node print sequence = " + list);
|
||||
|
||||
/* Post-order traversal */
|
||||
/* Postorder traversal */
|
||||
list.clear();
|
||||
postOrder(root);
|
||||
System.out.println("\nPrint sequence of nodes from post-order traversal = " + list);
|
||||
System.out.println("\nPostorder traversal node print sequence = " + list);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ public class PrintUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* Print binary tree
|
||||
* 打印二叉树
|
||||
* This tree printer is borrowed from TECHIE DELIGHT
|
||||
* https://www.techiedelight.com/c-program-print-binary-tree/
|
||||
*/
|
||||
@@ -104,12 +104,12 @@ public class PrintUtil {
|
||||
}
|
||||
}
|
||||
|
||||
/* Print heap (Priority queue) */
|
||||
/* 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.print("Heap array representation:");
|
||||
System.out.println(list);
|
||||
System.out.println("Tree representation of the heap:");
|
||||
System.out.println("Heap tree representation:");
|
||||
TreeNode root = TreeNode.listToTree(list);
|
||||
printTree(root);
|
||||
}
|
||||
|
||||
@@ -12,19 +12,19 @@ import java.util.*;
|
||||
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
|
||||
public TreeNode left; // Reference to left child node
|
||||
public TreeNode right; // Reference to right child node
|
||||
|
||||
/* Constructor */
|
||||
public TreeNode(int x) {
|
||||
val = x;
|
||||
}
|
||||
|
||||
// For serialization encoding rules, refer to:
|
||||
// For the serialization encoding rules, please refer to:
|
||||
// https://www.hello-algo.com/chapter_tree/array_representation_of_tree/
|
||||
// Array representation of the binary tree:
|
||||
// Array representation of binary tree:
|
||||
// [1, 2, 3, 4, None, 6, 7, 8, 9, None, None, 12, None, None, 15]
|
||||
// Linked list representation of the binary tree:
|
||||
// Linked list representation of binary tree:
|
||||
// /——— 15
|
||||
// /——— 7
|
||||
// /——— 3
|
||||
@@ -36,7 +36,7 @@ public class TreeNode {
|
||||
// \——— 4
|
||||
// \——— 8
|
||||
|
||||
/* Deserialize a list into a binary tree: Recursively */
|
||||
/* Deserialize a list into a binary tree: recursion */
|
||||
private static TreeNode listToTreeDFS(List<Integer> arr, int i) {
|
||||
if (i < 0 || i >= arr.size() || arr.get(i) == null) {
|
||||
return null;
|
||||
@@ -52,7 +52,7 @@ public class TreeNode {
|
||||
return listToTreeDFS(arr, 0);
|
||||
}
|
||||
|
||||
/* Serialize a binary tree into a list: Recursively */
|
||||
/* Serialize a binary tree into a list: recursion */
|
||||
private static void treeToListDFS(TreeNode root, int i, List<Integer> res) {
|
||||
if (root == null)
|
||||
return;
|
||||
|
||||
@@ -16,7 +16,7 @@ public class Vertex {
|
||||
this.val = val;
|
||||
}
|
||||
|
||||
/* Input a list of values vals, return a list of vertices vets */
|
||||
/* Input value list vals, return vertex list vets */
|
||||
public static Vertex[] valsToVets(int[] vals) {
|
||||
Vertex[] vets = new Vertex[vals.length];
|
||||
for (int i = 0; i < vals.length; i++) {
|
||||
@@ -25,7 +25,7 @@ public class Vertex {
|
||||
return vets;
|
||||
}
|
||||
|
||||
/* Input a list of vertices vets, return a list of values vals */
|
||||
/* Input vertex list vets, return value list vals */
|
||||
public static List<Integer> vetsToVals(List<Vertex> vets) {
|
||||
List<Integer> vals = new ArrayList<>();
|
||||
for (Vertex vet : vets) {
|
||||
|
||||
Reference in New Issue
Block a user