-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathHeap.java
178 lines (150 loc) · 3.32 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
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
178
package DataStructure;
import java.util.ArrayList;
class Heap<T extends Comparable<T>>
{
private int heapSize;
private ArrayList<T> heap;
public Heap()
{
heap= new ArrayList<T>();
heapSize=0;
}
public Heap(int capacity)
{
heap= new ArrayList<T>(capacity);
heapSize=0;
}
public int getHeapSize()
{
return heap.size();
}
private int getLeftChildIndex(int parentIndex)
{
return 2*parentIndex+1;
}
private int getRightChildIndex(int parentIndex)
{
return 2*parentIndex+2;
}
private int getParentIndex(int childIndex)
{
return (childIndex-1)/2;
}
private boolean hasLeftChild(int parentIndex)
{
if(getLeftChildIndex(parentIndex)<heapSize)
return true;
else
return false;
}
private boolean hasRightChild(int parentIndex)
{
if(getRightChildIndex(parentIndex)<heapSize)
return true;
else
return false;
}
private boolean hasParent(int childIndex)
{
if(childIndex>0)
return true;
else
return false;
}
private T getLeftChild(int index)
{
return heap.get(getLeftChildIndex(index));
}
private T getRightChild(int index)
{
return heap.get(getRightChildIndex(index));
}
private T getParent(int index)
{
return heap.get(getParentIndex(index));
}
private void swap(int index1,int index2)
{
T temp=heap.get(index1);
heap.set(index1,heap.get(index2));
heap.set(index2,temp);
}
private void heapify(int index)
{
int swapIndex=index;
if(hasLeftChild(index))
{
heapify(getLeftChildIndex(index));
swapIndex=swapCiteria(index,getLeftChildIndex(index));
}
if(hasRightChild(index))
{
heapify(getRightChildIndex(index));
swapIndex=swapCiteria(swapIndex, getRightChildIndex(index));
}
if(swapIndex!=index)
{
swap(index,swapIndex);
heapify(swapIndex);
}
}
private void heapifyUp(int index)
{
while(hasParent(index) && heap.get(index).compareTo(getParent(index)) <=0)
{
swap(getParentIndex(index),index);
index=getParentIndex(index);
}
}
private int swapCiteria(int index1, int index2)
{
int value=heap.get(index1).compareTo( heap.get(index2));
if(value==-1)
return index2;
else
return index1;
}
public void add(T item)
{
heap.add(item);
heapifyUp(heap.size()-1);
}
public T peek()
{
if(heap.size()==0)
throw new IllegalStateException("Heap is Empty");
return heap.get(0);
}
public T poll()
{
if(heap.size()==0)
throw new IllegalStateException("Heap is Empty");
T item=heap.get(0);
swap(0, heap.size()-1);
heapifyDown(0);
return item;
}
private void heapifyDown(int index)
{
// TODO Auto-generated method stub
int swapIndex;
index=0;
while(hasLeftChild(index))
{
swapIndex=getLeftChildIndex(index);
if(hasRightChild(index) && getRightChild(index).compareTo(heap.get(swapIndex))<0)
swapIndex=getRightChildIndex(index);
if(heap.get(swapIndex).compareTo(heap.get(index))<0)
{
swap(swapIndex,index);
index=swapIndex;
}
else
break;
}
}
public String toString()
{
return heap.toString();
}
}