mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 21:24:53 +08:00
translation: Add Python and Java code for EN version (#1345)
* Add the intial translation of code of all the languages * test * revert * Remove * Add Python and Java code for EN version
This commit is contained in:
66
en/codes/java/chapter_heap/heap.java
Normal file
66
en/codes/java/chapter_heap/heap.java
Normal file
@ -0,0 +1,66 @@
|
||||
/**
|
||||
* File: heap.java
|
||||
* Created Time: 2023-01-07
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package chapter_heap;
|
||||
|
||||
import utils.*;
|
||||
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);
|
||||
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);
|
||||
PrintUtil.printHeap(heap);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
/* Initialize the heap */
|
||||
// Initialize min-heap
|
||||
Queue<Integer> minHeap = new PriorityQueue<>();
|
||||
// Initialize the max-heap (using lambda expression to modify Comparator if necessary)
|
||||
Queue<Integer> maxHeap = new PriorityQueue<>((a, b) -> b - a);
|
||||
|
||||
System.out.println("\nThe following test case is for max-heap");
|
||||
|
||||
/* Push the element into heap */
|
||||
testPush(maxHeap, 1);
|
||||
testPush(maxHeap, 3);
|
||||
testPush(maxHeap, 2);
|
||||
testPush(maxHeap, 5);
|
||||
testPush(maxHeap, 4);
|
||||
|
||||
/* Access heap top element */
|
||||
int peek = maxHeap.peek();
|
||||
System.out.format("\nTop element of the heap is %d\n", peek);
|
||||
|
||||
/* Pop the element at the heap top */
|
||||
testPop(maxHeap);
|
||||
testPop(maxHeap);
|
||||
testPop(maxHeap);
|
||||
testPop(maxHeap);
|
||||
testPop(maxHeap);
|
||||
|
||||
/* Get heap size */
|
||||
int size = maxHeap.size();
|
||||
System.out.format("\nNumber of elements in the heap is %d\n", size);
|
||||
|
||||
/* Determine if heap is empty */
|
||||
boolean isEmpty = maxHeap.isEmpty();
|
||||
System.out.format("\nIs the heap empty %b\n", isEmpty);
|
||||
|
||||
/* Enter 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");
|
||||
PrintUtil.printHeap(minHeap);
|
||||
}
|
||||
}
|
||||
159
en/codes/java/chapter_heap/my_heap.java
Normal file
159
en/codes/java/chapter_heap/my_heap.java
Normal file
@ -0,0 +1,159 @@
|
||||
/**
|
||||
* File: my_heap.java
|
||||
* Created Time: 2023-01-07
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package chapter_heap;
|
||||
|
||||
import utils.*;
|
||||
import java.util.*;
|
||||
|
||||
/* Max-heap */
|
||||
class MaxHeap {
|
||||
// Use list instead of array to avoid the need for resizing
|
||||
private List<Integer> maxHeap;
|
||||
|
||||
/* Constructor, build heap based on input list */
|
||||
public MaxHeap(List<Integer> nums) {
|
||||
// Add all list elements into the heap
|
||||
maxHeap = new ArrayList<>(nums);
|
||||
// Heapify all nodes except leaves
|
||||
for (int i = parent(size() - 1); i >= 0; i--) {
|
||||
siftDown(i);
|
||||
}
|
||||
}
|
||||
|
||||
/* Get index of left child node */
|
||||
private int left(int i) {
|
||||
return 2 * i + 1;
|
||||
}
|
||||
|
||||
/* Get index of right child node */
|
||||
private int right(int i) {
|
||||
return 2 * i + 2;
|
||||
}
|
||||
|
||||
/* Get index of parent node */
|
||||
private int parent(int i) {
|
||||
return (i - 1) / 2; // Integer division down
|
||||
}
|
||||
|
||||
/* Swap elements */
|
||||
private void swap(int i, int j) {
|
||||
int tmp = maxHeap.get(i);
|
||||
maxHeap.set(i, maxHeap.get(j));
|
||||
maxHeap.set(j, tmp);
|
||||
}
|
||||
|
||||
/* Get heap size */
|
||||
public int size() {
|
||||
return maxHeap.size();
|
||||
}
|
||||
|
||||
/* Determine if heap is empty */
|
||||
public boolean isEmpty() {
|
||||
return size() == 0;
|
||||
}
|
||||
|
||||
/* Access heap top element */
|
||||
public int peek() {
|
||||
return maxHeap.get(0);
|
||||
}
|
||||
|
||||
/* Push the element into heap */
|
||||
public void push(int val) {
|
||||
// Add node
|
||||
maxHeap.add(val);
|
||||
// Heapify from bottom to top
|
||||
siftUp(size() - 1);
|
||||
}
|
||||
|
||||
/* Start heapifying node i, 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
|
||||
if (p < 0 || maxHeap.get(i) <= maxHeap.get(p))
|
||||
break;
|
||||
// Swap two nodes
|
||||
swap(i, p);
|
||||
// Loop upwards heapification
|
||||
i = p;
|
||||
}
|
||||
}
|
||||
|
||||
/* Element exits heap */
|
||||
public int pop() {
|
||||
// Empty handling
|
||||
if (isEmpty())
|
||||
throw new IndexOutOfBoundsException();
|
||||
// Swap the root node with the rightmost leaf node (swap the first element with the last element)
|
||||
swap(0, size() - 1);
|
||||
// Remove node
|
||||
int val = maxHeap.remove(size() - 1);
|
||||
// Heapify from top to bottom
|
||||
siftDown(0);
|
||||
// Return heap top element
|
||||
return val;
|
||||
}
|
||||
|
||||
/* Start heapifying node i, from top to bottom */
|
||||
private void siftDown(int i) {
|
||||
while (true) {
|
||||
// Determine the largest node among i, l, r, noted as ma
|
||||
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
|
||||
if (ma == i)
|
||||
break;
|
||||
// Swap two nodes
|
||||
swap(i, ma);
|
||||
// Loop downwards heapification
|
||||
i = ma;
|
||||
}
|
||||
}
|
||||
|
||||
/* Print heap (binary tree) */
|
||||
public void print() {
|
||||
Queue<Integer> queue = new PriorityQueue<>((a, b) -> { return b - a; });
|
||||
queue.addAll(maxHeap);
|
||||
PrintUtil.printHeap(queue);
|
||||
}
|
||||
}
|
||||
|
||||
public class my_heap {
|
||||
public static void main(String[] args) {
|
||||
/* Initialize 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");
|
||||
maxHeap.print();
|
||||
|
||||
/* Access heap top element */
|
||||
int peek = maxHeap.peek();
|
||||
System.out.format("\nTop element of the heap is %d\n", peek);
|
||||
|
||||
/* Push the element into heap */
|
||||
int val = 7;
|
||||
maxHeap.push(val);
|
||||
System.out.format("\nAfter element %d is added to the heap\n", val);
|
||||
maxHeap.print();
|
||||
|
||||
/* Pop the element at the heap top */
|
||||
peek = maxHeap.pop();
|
||||
System.out.format("\nAfter the top element %d is removed from the 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);
|
||||
|
||||
/* Determine if heap is empty */
|
||||
boolean isEmpty = maxHeap.isEmpty();
|
||||
System.out.format("\nIs the heap empty %b\n", isEmpty);
|
||||
}
|
||||
}
|
||||
40
en/codes/java/chapter_heap/top_k.java
Normal file
40
en/codes/java/chapter_heap/top_k.java
Normal file
@ -0,0 +1,40 @@
|
||||
/**
|
||||
* File: top_k.java
|
||||
* Created Time: 2023-06-12
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package chapter_heap;
|
||||
|
||||
import utils.*;
|
||||
import java.util.*;
|
||||
|
||||
public class top_k {
|
||||
/* Using heap to find the largest k elements in an array */
|
||||
static Queue<Integer> topKHeap(int[] nums, int k) {
|
||||
// Initialize min-heap
|
||||
Queue<Integer> heap = new PriorityQueue<Integer>();
|
||||
// Enter the first k elements of the array into the heap
|
||||
for (int i = 0; i < k; i++) {
|
||||
heap.offer(nums[i]);
|
||||
}
|
||||
// From the k+1th element, keep the 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 (nums[i] > heap.peek()) {
|
||||
heap.poll();
|
||||
heap.offer(nums[i]);
|
||||
}
|
||||
}
|
||||
return heap;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[] nums = { 1, 7, 6, 3, 2 };
|
||||
int k = 3;
|
||||
|
||||
Queue<Integer> res = topKHeap(nums, k);
|
||||
System.out.println("The largest " + k + " elements are");
|
||||
PrintUtil.printHeap(res);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user