-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBinaryHeap.m
240 lines (208 loc) · 8.59 KB
/
BinaryHeap.m
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
classdef BinaryHeap < handle
%%BINARYHEAP An implementation of a priority queue as a binary heap. A
% priority queue is a data structure that allows one to easily
% get/remove the maximum or minimum item in the queue.
%
%DEPENDENCIES: KeyVal.m
%
%The methods of the class are generally based on the implementation
%described in Chapter 6.4 of [1].
%
%The class makes use of the KeyValue class for storing keys associated with
%values. The entire heap is stored in an array, which can be preallocated
%to the maximum size and thus be efficient.
%
%REFERENCES:
%[1] M.A.Weiss, Data Structures and Algorithm Analysis in C++, 2nd ed.
% Reading, MA: Addison-Wesley, 1999.
%
%December 2013 David F. Crouse, Naval Research Laboratory, Washington D.C.
%(UNCLASSIFIED) DISTRIBUTION STATEMENT A. Approved for public release.
properties
numInHeap
heapArray
isMaxHeap
end
methods
function newHeap=BinaryHeap(initialMaxSize,isMaxHeap)
%BINARYHEAP Allocate space for a binary heap and specify whether it
% is a max heap.
%
%INPUT: initialMaxSize The amount of room preallocated for the
% binary heap. If omitted, then a size-1 heap
% is preallocated.
% isMaxHeap A boolean value indicating whether the top
% of the heap is a maximum or a minimum value.
% The default if not specified is maximum.
%
%OUTPUTS: newHeap The newly created heap.
%
%December 2013 David F. Crouse, Naval Research Laboratory,
%Washington D.C.
if(nargin<2)
isMaxHeap=true;
end
if(nargin<1)
initialMaxSize=1;
end
%This line is here, because the next line will fail if Matlab
%has not identified heapArray as being an array of instances of
%the KeyValue class.
newHeap.heapArray=KeyVal();
newHeap.heapArray(initialMaxSize,1)=KeyVal();
newHeap.numInHeap=0;
newHeap.isMaxHeap=isMaxHeap;
end
function val=heapSize(curHeap)
%HEAPSIZE Return the number of elements in the heap.
%
%December 2013 David F. Crouse, Naval Research Laboratory,
%Washington D.C.
val=curHeap.numInHeap;
end
function val=isEmpty(curHeap)
%ISEMPTY Return true if there are no nodes in the heap.
%
%December 2013 David F. Crouse, Naval Research Laboratory,
%Washington D.C.
val=curHeap.numInHeap==0;
end
function buildHeapFromKeysData(curHeap,keyArray,dataArray)
%BUILDHEAPFROMDATA Build the heap from given data. If the heap was
% already initialized, it will be destroyed.
%
%INPUTS: curHeap The implicitly passed object.
% keyArray A kX1 array of key values.
% dataArray A linear cell array or a vector containing the
% data associated with each key value.
%
%December 2013 David F. Crouse, Naval Research Laboratory,
%Washington D.C.
numKeys=length(keyArray);
curHeap.numInHeap=numKeys;
curHeap.heapArray(numKeys,1)=KeyVal;
if(isa(dataArray,'cell'))
for curKey=1:numKeys
curHeap.heapArray(curKey)=KeyVal(keyArray(curKey),dataArray{curKey});
end
else
for curKey=1:numKeys
curHeap.heapArray(curKey)=KeyVal(keyArray(curKey),dataArray(curKey));
end
end
idx=floor(curHeap.numInHeap/2);
while(idx>0)
curHeap.percolateDown(idx);
idx=idx-1;
end
end
function insert(curHeap,key,value)
%INSERT Insert an entry with a specific key and value into the
% heap.
%
%December 2013 David F. Crouse, Naval Research Laboratory,
%Washington D.C.
curHeap.numInHeap=curHeap.numInHeap+1;
hole=curHeap.numInHeap;
if(curHeap.isMaxHeap)
while(hole>1&&key>curHeap.heapArray(floor(hole/2)))
curHeap.heapArray(hole)=curHeap.heapArray(floor(hole/2));
hole=floor(hole/2);
end
else
while(hole>1&&key<curHeap.heapArray(floor(hole/2)))
curHeap.heapArray(hole)=curHeap.heapArray(floor(hole/2));
hole=floor(hole/2);
end
end
curHeap.heapArray(hole)=KeyVal(key,value);
end
function val=getTop(curHeap)
%GETTOP Return the key-value pair at the top of the heap as a
% KeyVal object. If the heap is a max heap, then it is the
% element with the largest key. Otherwise, it is the element
% with the smallest key. If the heap is empty, then an empty
% matrix is returned.
%
%The value returned is a shallow copy of the KeyVal object in the
%heap. Changes to it do not affect the values in the heap, unless
%those values are handle class objects. If the returned key is a
%handle class object, one should not modify it; it would invalidate
%the structure of the heap.
%
%December 2013 David F. Crouse, Naval Research Laboratory,
%Washington D.C.
if(curHeap.numInHeap>0)
val=copy(curHeap.heapArray(1));
else
val=[];
end
end
function val=deleteTop(curHeap)
%DELETETOP Remove the top element of heap heap (the one that getTop
% would return) and return the key and value of the
% element as a KeyVal object.
%
%December 2013 David F. Crouse, Naval Research Laboratory,
%Washington D.C.
if(curHeap.numInHeap==0)
val=[];
return;
end
val=curHeap.heapArray(1);
curHeap.heapArray(1)=curHeap.heapArray(curHeap.numInHeap);
curHeap.numInHeap=curHeap.numInHeap-1;
curHeap.percolateDown(1);
end
end
methods(Access=private)
function percolateDown(curHeap,hole)
temp=curHeap.heapArray(hole);
if(curHeap.isMaxHeap)
while(2*hole<=curHeap.numInHeap)
child=2*hole;
if(child~=curHeap.numInHeap&&curHeap.heapArray(child+1)>curHeap.heapArray(child))
child = child+1;
end
if(curHeap.heapArray(child)>temp)
curHeap.heapArray(hole)=curHeap.heapArray(child);
else
break;
end
hole=child;
end
else
while(2*hole<=curHeap.numInHeap)
child=2*hole;
if(child~=curHeap.numInHeap&&curHeap.heapArray(child+1)<curHeap.heapArray(child))
child = child+1;
end
if(curHeap.heapArray(child)<temp)
curHeap.heapArray(hole)=curHeap.heapArray(child);
else
break;
end
hole=child;
end
end
curHeap.heapArray(hole)=temp;
end
end
end
%LICENSE:
%
%The source code is in the public domain and not licensed or under
%copyright. The information and software may be used freely by the public.
%As required by 17 U.S.C. 403, third parties producing copyrighted works
%consisting predominantly of the material produced by U.S. government
%agencies must provide notice with such work(s) identifying the U.S.
%Government material incorporated and stating that such material is not
%subject to copyright protection.
%
%Derived works shall not identify themselves in a manner that implies an
%endorsement by or an affiliation with the Naval Research Laboratory.
%
%RECIPIENT BEARS ALL RISK RELATING TO QUALITY AND PERFORMANCE OF THE
%SOFTWARE AND ANY RELATED MATERIALS, AND AGREES TO INDEMNIFY THE NAVAL
%RESEARCH LABORATORY FOR ALL THIRD-PARTY CLAIMS RESULTING FROM THE ACTIONS
%OF RECIPIENT IN THE USE OF THE SOFTWARE.