-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeap3.java
More file actions
149 lines (125 loc) · 3.59 KB
/
Heap3.java
File metadata and controls
149 lines (125 loc) · 3.59 KB
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
package heap;
/****************************************************************************
* This demonstrates binary heap operations along with the heapSort.
*
*****************************************************************************/
// https://www.youtube.com/watch?v=W81Qzuz4qH0&index=11&list=PLs8TmeZHJEeF2UMA8KCI6g0BMDrVUgB0r&t=2s
import java.util.*;
@SuppressWarnings("unchecked")
public class Heap3<AnyType extends Comparable<AnyType>>
{
private static final int CAPACITY = 2;
private int size; // Number of elements in heap
private AnyType[] heap; // The heap array
public Heap3()
{
size = 0;
heap = (AnyType[]) new Comparable[CAPACITY];
}
/**
* Construct the binary heap given an array of items.
*/
public Heap3(AnyType[] array)
{
size = array.length;
heap = (AnyType[]) new Comparable[array.length+1];
System.arraycopy(array, 0, heap, 1, array.length);//we do not use 0 index
buildHeap();
}
/**
* runs at O(size)
*/
private void buildHeap()
{
for (int k = size/2; k > 0; k--)
{
percolatingDown(k);
}
}
private void percolatingDown(int k)
{
AnyType tmp = heap[k];
int child;
for(; 2*k <= size; k = child)
{
child = 2*k;
if(child != size &&
heap[child].compareTo(heap[child + 1]) > 0) child++;
if(tmp.compareTo(heap[child]) > 0) heap[k] = heap[child];
else
break;
}
heap[k] = tmp;
}
/**
* Sorts a given array of items.
*/
public void heapSort(AnyType[] array)
{
size = array.length;
heap = (AnyType[]) new Comparable[size+1];
System.arraycopy(array, 0, heap, 1, size);
buildHeap();
for (int i = size; i > 0; i--)
{
AnyType tmp = heap[i]; //move top item to the end of the heap array
heap[i] = heap[1];
heap[1] = tmp;
size--;
percolatingDown(1);
}
for(int k = 0; k < heap.length-1; k++)
array[k] = heap[heap.length - 1 - k];
}
/**
* Deletes the top item
*/
public AnyType deleteMin() throws RuntimeException
{
if (size == 0) throw new RuntimeException();
AnyType min = heap[1];
heap[1] = heap[size--];
percolatingDown(1);
return min;
}
/**
* Inserts a new item
*/
public void insert(AnyType x)
{
if(size == heap.length - 1) doubleSize();
//Insert a new item to the end of the array
int pos = ++size;
//Percolate up
for(; pos > 1 && x.compareTo(heap[pos/2]) < 0; pos = pos/2 )
heap[pos] = heap[pos/2];
heap[pos] = x;
}
private void doubleSize()
{
AnyType [] old = heap;
heap = (AnyType []) new Comparable[heap.length * 2];
System.arraycopy(old, 1, heap, 1, size);
}
public String toString()
{
String out = "";
for(int k = 1; k <= size; k++) out += heap[k]+" ";
return out;
}
public static void main(String[] args)
{
Heap3<String> h = new Heap3<String>();
h.insert("p");
h.insert("r");
h.insert("i");
h.insert("o");
System.out.println(h);
h.deleteMin();
System.out.println(h);
Heap3<Integer> tmp = new Heap3<Integer>();
Integer[] a = {4,7,7,7,5,0,2,3,5,1};
tmp.heapSort(a);
System.out.println(Arrays.toString(a));
}
}