算法:手写堆

master
msb_16686 2 years ago
parent b80ed3e553
commit d6f22d3f04

@ -7,6 +7,10 @@
<groupId>org.example</groupId>
<artifactId>Algorithm</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
</project>

@ -0,0 +1,45 @@
package algorithm;
import java.util.Stack;
public class C02_Stack {
/**
*
* poppushgetMinO(1)
* <p>
*
*
*/
public static class MyStack1 {
private Stack<Integer> dataStack;
private Stack<Integer> minStack;
public MyStack1() {
dataStack = new Stack<>();
minStack = new Stack<>();
}
public int push(int e) {
dataStack.push(e);
Integer peek = minStack.peek();
if (peek == null) {
minStack.push(e);
} else {
minStack.push(Math.min(e, peek));
}
return e;
}
public int pop() {
Integer res = dataStack.pop();
Integer min = minStack.pop();
return res;
}
public int getMin() {
return minStack.peek();
}
}
}

@ -0,0 +1,62 @@
package algorithm;
import common.Node;
public class C21_Node {
public static void main(String args[]) {
System.out.println("开始:");
for (int i = 0; i < 20; i++) {
Node head = createRandomNode(8, 6);
printNodes(head);
System.out.print(" ===>> ");
Node node = removeValue(head, 5);
printNodes(node);
System.out.println();
}
}
public static void printNodes(Node head) {
Node pre = head;
while (pre != null) {
System.out.print(pre.value + " ");
pre = pre.next;
}
}
public static Node createRandomNode(int len, int max) {
Node head = new Node(randomNum(max));
Node next = head;
for (int i = 1; i < len; i++) {
int n = randomNum(max);
next.next = new Node(n);
next = next.next;
}
return head;
}
// 生成一个 [0, n) 的随机数
public static int randomNum(int n) {
return (int) (Math.random() * n);
}
public static Node removeValue(Node head, int num) {
while (head != null) {
if (head.value != num) {
break;
}
head = head.next;
}
Node pre = head;
Node cur = head;
while (cur != null) {
if (cur.value == num) {
pre.next = cur.next;
} else {
pre = cur;
}
cur = cur.next;
}
return head;
}
}

@ -0,0 +1,10 @@
package common;
public class Node {
public int value;
public Node next;
public Node(int value) {
this.value = value;
}
}

@ -0,0 +1,41 @@
package common;
public class Student {
private int id;
private int age;
private String name;
public Student() {
}
public Student(int id, int age, String name) {
this.id = id;
this.age = age;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

@ -0,0 +1,47 @@
package heap;
import common.Student;
import java.util.Arrays;
import java.util.Comparator;
public class C01_Comparator {
public static class Student {
public int id;
public int age;
public String name;
public Student(int id, int age, String name) {
this.id = id;
this.age = age;
this.name = name;
}
}
/**
*
*/
public static class OrderAsc implements Comparator<Student> {
@Override
public int compare(Student o1, Student o2) {
return o1.id == o2.id ? o1.age - o2.age : o1.id - o2.id;
}
}
public static void main(String[] args) {
Student s1 = new Student(1, 21, "A");
Student s2 = new Student(2, 30, "B");
Student s3 = new Student(3, 21, "C");
Student[] s = new Student[]{s1, s2, s3};
Arrays.sort(s, new OrderAsc());
System.out.println("====1====");
for (Student c : s) {
System.out.println("name: " + c.name + ", id: " + c.id + ", age: " + c.age);
}
}
}

@ -0,0 +1,7 @@
package heap;
public class C02_heap {
}

@ -0,0 +1,119 @@
package heap;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
/**
*
*
* @param <T>
*/
public class HeapGreater<T> {
private ArrayList<T> heap;
// 反向索引表
private HashMap<T, Integer> indexMap;
private int heapSize;
private Comparator<? super T> comp;
public HeapGreater(Comparator<T> comp) {
heap = new ArrayList<>();
indexMap = new HashMap<>();
heapSize = 0;
this.comp = comp;
}
public boolean isEmpty() {
return heapSize == 0;
}
public int size() {
return heapSize;
}
public boolean contains(T obj) {
return indexMap.containsKey(obj);
}
public T peek() {
return heap.get(0);
}
public void push(T obj) {
heap.add(obj);
indexMap.put(obj, heapSize);
heapInsert(heapSize++);
}
public T pop() {
T ans = heap.get(0);
swap(0, heapSize - 1);
indexMap.remove(ans);
heap.remove(--heapSize);
heapify(0);
return ans;
}
/**
*
*
* @param obj
*/
public void resign(T obj) {
heapInsert(indexMap.get(obj));
heapify(indexMap.get(obj));
}
/**
*
*
* @param obj
*/
public void remove(T obj) {
T replace = heap.get(heapSize - 1);
int index = indexMap.get(obj);
indexMap.remove(obj);
heap.remove(--heapSize);
if (obj != replace) {
heap.set(index, replace);
indexMap.put(replace, index);
resign(replace);
}
}
private void heapify(int index) {
int left = index << 1 + 1;
while (left < heapSize) {
int best = left + 1 < heapSize && comp.compare(heap.get(left + 1), heap.get(left)) < 0 ? (left + 1) : left;
best = comp.compare(heap.get(best), heap.get(index)) < 0 ? best : index;
if (best == index) {
break;
}
swap(best, index);
index = best;
left = index << 1 + 1;
}
}
private void heapInsert(int index) {
while (comp.compare(heap.get(index), heap.get((index - 1) / 2)) < 0) {
swap(index, (index - 1) / 2);
index = (index - 1) / 2;
}
}
private void swap(int i, int j) {
T o1 = heap.get(i);
T o2 = heap.get(j);
heap.set(i, o2);
heap.set(j, o1);
indexMap.put(o1, j);
indexMap.put(o2, i);
}
public static void main(String[] args) {
System.out.println(-1 / 2);
}
}
Loading…
Cancel
Save