-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeap.java
More file actions
270 lines (240 loc) · 8.61 KB
/
Heap.java
File metadata and controls
270 lines (240 loc) · 8.61 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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/**
* Created by Ward Bradt on 4/9/17.
* Revised on 06/08/17.
*
* A <code>Heap</code> with some elements of a <code>PriorityQueue</code>. This data structure does not automatically
* balance when elements are removed/ added. Each <code>Heap</code> has two child <code>Heap</code>s.
* <br>
* This data structure follows the following convention:
* For any <code>Heap n</code>, <code>n.contents</code> > <code>n.right</code>'s contents and all of
* <code>n.right</code>'s children's contents > <code>n.left</code>'s contents and all of <code>n.left</code>'s
* children's contents.
*
*/
public class Heap<T extends Comparable<T>>{
private T contents;
private Heap<T> left, right;
public Heap() {
contents = null;
left = null;
right = null;
}
public Heap(T item) {
contents = item;
left = null;
right = null;
}
/**
* Copy constructor for <code>Heap</code>
*
* @param rootHeap the Heap that is being copied.
*/
public Heap(Heap<T> rootHeap) {
contents = rootHeap.contents;
left = rootHeap.left;
right = rootHeap.right;
}
public Heap<T> getLeft() {
return left;
}
public Heap<T> getRight() {
return right;
}
public T getContents() {
return contents;
}
// We want the contents of the left heap to always be smaller.
/**
* Add T item to the <code>heap</code>. This add method follows the convention that
* <code>n.contents</code> > <code>n.right</code> and all of its children > <code>n.left</code> and all of its children.
* Uses recursive helper method <code>addHelper(Heap<T> parent, T item)</code>.
*
* @param item the item that is to be added.
* @return true if item is added
*/
public boolean add(T item) {
if (item == null) {
throw new NullPointerException("param item cannot be null!");
}
if (contents == null) {
contents = item;
return true;
}
if (item.compareTo(contents) > 0) {
right = new Heap<T>(this);
left = null;
contents = item;
return true;
}
// parent is the node that will be compared.
return addHelper(this, item);
}
/**
* Recursive helper method for <code>add(T item)</code>. This method is only reached if
* neither <code>contents</code>, <code>left</code>, or <code>right == null</code>.
*
* @param parent The "root" <code>Heap</code> of the item that is to be added. On the first
* iteration parent is <code>this</code> - the root <code>Heap</code>.
* @param item the item that it to be added.
* @return if <code>item</code> is added.
*/
public boolean addHelper(Heap<T> parent, T item) {
// If either of the parent's right or left branches are null
if (parent.left == null && parent.right == null) {
parent.right = new Heap<T>(item);
return true;
}
// If it gets here, parent.left != null and parent.right == null
if (parent.right == null) {
// If item is smaller than left's contents, then we need to swap them
// because bigger items are always on the right.
if (item.compareTo(parent.left.contents) < 0) {
parent.right = new Heap<T>(parent.left);
parent.left = new Heap<T>(item);
return true;
}
parent.right = new Heap<T>(item);
return true;
}
// If it gets here, left == null and right != null
if (parent.left == null) {
// If item is larger than right's contents, need to swap.
if (item.compareTo(parent.right.contents) > 0) {
parent.left = new Heap<T>(parent.right);
parent.right = new Heap<T>(item);
return true;
}
parent.left = new Heap<T>(item);
return true;
}
// parent.left is always smaller than parent.right (contents)
// If it is smaller than the parent's left branch
if (item.compareTo(parent.left.contents) < 0) {
return addHelper(parent.left, item);
}
// If item is bigger than parent.left and smaller than parent.right
else if (item.compareTo(parent.right.contents) < 0) {
// This is keeping mind that with a Heap x, x.contents > right.contents > left.contents
Heap<T> newMid = new Heap<T>(item);
// Set the new top branch's left to the right of whatever it is replacing.
if (parent.left.right != null) newMid.left = parent.left.right;
if (parent.left.left != null) newMid.left.right = parent.left.left;
// Set the new top branch's right as a new Heap with the contents of parent's left.
newMid.right = new Heap<T>(parent.left.contents);
parent.left = newMid;
return true;
}
// If item is bigger than both the left and right branches
else if (item.compareTo(parent.right.contents) > 0) {
Heap<T> newMid = new Heap<T>(item);
newMid.left = parent.left;
newMid.right = parent.right;
// Put newMid on the right branch of parent
parent.right = newMid;
parent.left = null;
return true;
}
// Only if item == both the parent's left and right branches
return true;
}
/**
* Return the <code>contents</code>, or the greatest object, of this <code>Heap</code> and reformat
* the <code>Heap</code> with <code>contents</code> removed.
* Returns <code>null</code> if <code>contents == null</code>.
*
* @return the greatest object in the <code>Heap</code>
*/
public T extractMax() {
T tempCon = contents;
if (left == null || right == null) {
if (right != null) setRoot(right);
// Only if left != null or both are null
else setRoot(left);
}
// Only gets here if neither the left or right branch is null.
else {
contents = right.contents;
right = new Heap<T>(right.extractMax());
}
return tempCon;
}
/**
* Only to be used if user would like to completely clear the heap.
* Sort of a copy constructor, setting <code>this</code> to <code>newRoot</code>.
*/
public void setRoot(Heap<T> newRoot) {
if (newRoot != null) {
contents = newRoot.contents;
left = newRoot.left;
right = newRoot.right;
}
}
/**
* Removes all elements from the <code>Heap</code>.
*/
public void clear() {
contents = null;
right = null;
left = null;
}
/**
* Returns an iterable object of the <code>Heap</code> in descending order
*
* @return a Que
*/
public Queue<T> iterate() {
Stack<Heap<T>> nodes = new Stack<Heap<T>>(this);
Queue<T> depth = new Queue<T>(contents);
if (nodes.peek().getRight() != null) {
depth.addQueue(nodes.peek().getRight().iterate());
}
if (nodes.peek().getLeft() != null) {
depth.addQueue(nodes.peek().getLeft().iterate());
}
nodes.pop();
return depth;
}
/**
* Returns the maximum Object
* @return the highest-valued <code>Object</code> in this <code>Heap</code>
*/
public T max() {
return contents;
}
public boolean empty() { return contents == null; }
/**
* Tests if an item is in this <code>Heap</code>.
* @param item an Object
* @return if <code>item</code> is in this <code>Heap</code>
*/
public boolean contains (T item) {
int cmp = item.compareTo(contents);
if (cmp == 0) return true;
if (cmp > 0) {
return false;
}
if (left != null) {
if (left.contains(item)) return true;
}
if (right != null) {
if (right.contains(item)) return true;
}
return false;
}
/**
* Recursively count how many <code>Heap</code>s, or <code>Object</code>s, are in the root <code>Heap</code>.
* This is done through a depth-first recursive iteration.
* @return how many <code>Heap</code>s, or <code>Object</code>s, are in the root <code>Heap</code>
*/
public int size() {
int count = 0;
if (contents != null) count++;
if (right != null) {
count+= right.size();
}
if (left != null) {
count += left.size();
}
return count;
}
}