-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeap.java
84 lines (70 loc) · 1.67 KB
/
Heap.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
class Heap {
int MAX_SIZE = 100000;
Node[] item;
int size;
public Heap() {
this.item = new Node[MAX_SIZE];
this.size = 0;
}
class Node {
int key;
int meta_data;
public Node(int key, int meta_data) {
this.key = key;
this.meta_data = meta_data;
}
}
private int parent(int i) { return i / 2; }
private int left(int i) { return i * 2; }
private int right(int i) { return i * 2 + 1; }
public void push(int key, int metadata)
{
item[++size] = new Node(key, metadata);
up(size);
}
public Node pop()
{
Node ret = item[1];
item[1] = item[--size];
down(1);
return ret;
}
private void up(int i)
{
while (i > 1 && item[i].key > item[parent(i)].key) {
swap(i, parent(i));
i = parent(i);
}
}
private void down(int i)
{
int biggest = i;
if (left(i) <= size && item[i].key < item[left(i)]. key) biggest = left(i);
if (right(i) <= size && item[i].key < item[right(i)]. key) biggest = right(i);
if (biggest != i) {
swap(i,biggest);
down(biggest);
}
}
void swap(int i , int j)
{
Node temp = item[i];
item[i] = item[j];
item[j] = temp;
}
void deleteAt(int key)
{
int pos = 0;
for (int i = 1 ; i < size; i++) {
if (item[i].key == key)
{
pos = i;
break;
}
}
if (pos == 0) return;
item[pos] = item[--size];
up(pos);
down(pos);
}
}