-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMaxHeap.java
111 lines (94 loc) · 2.6 KB
/
MaxHeap.java
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
import java.util.Random;
/**
* Author : 13152
* Date : 2020/1/31
* Time : 10:45
* Version : 1.0.0
**/
public class MaxHeap<E extends Comparable<E>> {
private Array<E> data;
public MaxHeap(int capacity) {
data = new Array<>(capacity);
}
public MaxHeap(E[] arr) {
data = new Array<E>(arr);
for (int i = parent(arr.length - 1); i >= 0; i--)
siftDown(i);
}
public MaxHeap() {
data = new Array<>();
}
public int size() {
return data.getSize();
}
public boolean isEmpty() {
return data.isEmpty();
}
private int parent(int index) {
if (index == 0)
throw new IllegalArgumentException("index isn't zero");
return (index - 1) / 2;
}
private int leftChild(int index) {
return index * 2 + 1;
}
private int rightChild(int index) {
return index * 2 + 2;
}
public void add(E e) {
data.addLast(e);
siftUp(data.getSize() - 1);
}
private void siftUp(int index) {
while (index > 0 && data.get(parent(index)).compareTo(data.get(index)) < 0) {
data.swap(parent(index), index);
index = (index - 1) / 2;
}
}
public E findMax() {
if (isEmpty())
throw new IllegalArgumentException("asdsa");
return data.get(0);
}
public E extractMax() {
E ret = findMax();
data.swap(0, size() - 1);
data.removeLast();
siftDown(0);
return ret;
}
private void siftDown(int index) {
while (leftChild(index) < size()) {
int j = leftChild(index);
if (j + 1 < data.getSize() && data.get(j + 1).compareTo(data.get(j)) > 0)
j++;
if (data.get(index).compareTo(data.get(j)) >= 0)
break;
else
data.swap(index, j);
index = j;
}
}
public E replace(E e) {
E ret = findMax();
data.set(0, e);
siftDown(0);
return ret;
}
public static void main(String[] args) {
int n = 100000;
MaxHeap<Integer> maxHeap = new MaxHeap<>();
Random random = new Random();
for (int i = 0; i < n; i++) {
maxHeap.add(random.nextInt(Integer.MAX_VALUE));
}
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = maxHeap.extractMax();
}
for (int i = 1; i < n; i++)
if (arr[i-1] < arr[i])
throw new IllegalArgumentException("Error");
System.out.println("Yes");
}
}