1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
| package org.kurt.datastructure.heap;
import java.util.ArrayList; import java.util.HashMap; import java.util.PriorityQueue;
public class MyHeapType<T extends Comparable> {
private int size; private ArrayList<T> arr;
private HashMap<T,Integer> indexMap ;
public MyHeapType(){ arr = new ArrayList<>(); indexMap = new HashMap<>(); }
public void push(T value) { heapInsert(value, size++); }
public T pop() { if (size > 0) { swap(0, size - 1); T ans = arr.get(size-1); size--; heapify(0); return ans; } return null; }
public T peek(){ if(size>0){ return arr.get(0); } return null; }
public void resign(T t) { heapInsert(t, indexMap.get(t)); heapify(indexMap.get(t)); }
private void heapify(int index) { int child = 0; while (index < size) { child = 2 * index + 1; if (child >= size) { break; } child = child + 1 < size && arr.get(child + 1).compareTo(arr.get(child))<0 ? child + 1 : child; if (arr.get(index).compareTo(arr.get(child)) > 0) { swap(index, child); } else { break; } index = child; }
}
private void swap(int a, int b) { T tmp = arr.get(a); arr.set(a, arr.get(b)); arr.set(b, tmp); indexMap.put(arr.get(a), a); indexMap.put(arr.get(b), b); }
private void heapInsert(T value, int index) { if(index >= arr.size()){ arr.add(value); }else{ arr.add(index, value); } indexMap.put(value, index); while (arr.get(index).compareTo(arr.get((index - 1) / 2)) < 0) { swap(index, (index - 1) / 2); index = (index - 1) / 2; } }
public static class Student implements Comparable<Student>{ public int score; public String name;
public Student(int score, String name) { this.score = score; this.name = name; }
@Override public int compareTo(Student o) { if (score > o.score) { return -1; } else if (score < o.score) { return 1; } return 0; }
@Override public String toString() { return "Student{" + "score=" + score + ", name='" + name + '\'' + '}'; } }
public static void main(String[] args) { for (int i = 0; i < 1000; i++) { MyHeapType<Student> heapType = new MyHeapType<>(); PriorityQueue<Student> queue = new PriorityQueue<>(); int size = (int) (Math.random() * 1000); for (int j = 0; j < size; j++) { int score = (int) (Math.random() * 1000); Student tmp = new Student(score, "A" + score); heapType.push(tmp); queue.offer(tmp); } System.out.println(queue.peek() + " " + heapType.peek()); if (queue.peek() != heapType.peek()) { System.out.println(queue.peek() + " " + heapType.peek()); System.out.println("ERROR"); } } System.out.println("PASS");
MyHeapType<Student> heapType = new MyHeapType<>(); Student a = new Student(100,"a"); Student b = new Student(18,"b"); Student c = new Student(30,"c"); heapType.push(a); heapType.push(b); heapType.push(c); System.out.println(heapType.peek()); b.score=110; heapType.resign(b); System.out.println(heapType.peek()); }
}
|
Gitalking ...