Skip to content

Commit 264e4cb

Browse files
author
phishman3579
committed
Made Heap, List, Queue, and Stack Iterable
git-svn-id: https://java-algorithms-implementation.googlecode.com/svn/trunk@401 032fbc0f-8cab-eb90-e552-f08422b9a96a
1 parent b4cf730 commit 264e4cb

File tree

4 files changed

+109
-1
lines changed

4 files changed

+109
-1
lines changed

src/com/jwetherell/algorithms/data_structures/BinaryHeap.java

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.ArrayList;
44
import java.util.Arrays;
5+
import java.util.Iterator;
56
import java.util.List;
67

78
/**
@@ -17,7 +18,7 @@
1718
*
1819
* @author Justin Wetherell <phishman3579@gmail.com>
1920
*/
20-
public abstract class BinaryHeap<T extends Comparable<T>> {
21+
public abstract class BinaryHeap<T extends Comparable<T>> implements Iterable<T> {
2122

2223
public enum HeapType {
2324
Tree, Array
@@ -428,6 +429,52 @@ private static <T extends Comparable<T>> String getString(BinaryHeapArray<T> tre
428429
return builder.toString();
429430
}
430431
}
432+
433+
/**
434+
* {@inheritDoc}
435+
*
436+
* This iterator will copy the entire data structure and sort it. Be aware!
437+
*/
438+
@Override
439+
public Iterator<T> iterator() {
440+
return (new BinaryHeapArrayIterator<T>(this));
441+
}
442+
443+
private static class BinaryHeapArrayIterator<T extends Comparable<T>> implements Iterator<T> {
444+
445+
private T[] array = null;
446+
private int index = 0;
447+
448+
private BinaryHeapArrayIterator(BinaryHeapArray<T> heap) {
449+
this.array = heap.getHeap();
450+
Arrays.sort(array);
451+
}
452+
453+
/**
454+
* {@inheritDoc}
455+
*/
456+
@Override
457+
public boolean hasNext() {
458+
return (index<array.length);
459+
}
460+
461+
/**
462+
* {@inheritDoc}
463+
*/
464+
@Override
465+
public T next() {
466+
if (index>=array.length) return null;
467+
return array[index++];
468+
}
469+
470+
/**
471+
* {@inheritDoc}
472+
*/
473+
@Override
474+
public void remove() {
475+
System.err.println("OperationNotSupported");
476+
}
477+
}
431478
}
432479

433480
public static class BinaryHeapTree<T extends Comparable<T>> extends BinaryHeap<T> {
@@ -949,5 +996,51 @@ public String toString() {
949996
+ ((left != null) ? left.value : "NULL") + " right=" + ((right != null) ? right.value : "NULL");
950997
}
951998
}
999+
1000+
/**
1001+
* {@inheritDoc}
1002+
*
1003+
* This iterator will copy the entire data structure and sort it. Be aware!
1004+
*/
1005+
@Override
1006+
public Iterator<T> iterator() {
1007+
return (new BinaryHeapTreeIterator<T>(this));
1008+
}
1009+
1010+
private static class BinaryHeapTreeIterator<T extends Comparable<T>> implements Iterator<T> {
1011+
1012+
private T[] array = null;
1013+
private int index = 0;
1014+
1015+
private BinaryHeapTreeIterator(BinaryHeapTree<T> heap) {
1016+
array = heap.getHeap();
1017+
Arrays.sort(array);
1018+
}
1019+
1020+
/**
1021+
* {@inheritDoc}
1022+
*/
1023+
@Override
1024+
public boolean hasNext() {
1025+
return (index<array.length);
1026+
}
1027+
1028+
/**
1029+
* {@inheritDoc}
1030+
*/
1031+
@Override
1032+
public T next() {
1033+
if (index>=array.length) return null;
1034+
return array[index++];
1035+
}
1036+
1037+
/**
1038+
* {@inheritDoc}
1039+
*/
1040+
@Override
1041+
public void remove() {
1042+
System.err.println("OperationNotSupported");
1043+
}
1044+
}
9521045
}
9531046
}

src/com/jwetherell/algorithms/data_structures/List.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ public String toString() {
165165

166166
/**
167167
* {@inheritDoc}
168+
*
169+
* This iterator is NOT thread safe and is invalid when the data structure is modified.
168170
*/
169171
@Override
170172
public Iterator<T> iterator() {
@@ -359,6 +361,8 @@ public String toString() {
359361

360362
/**
361363
* {@inheritDoc}
364+
*
365+
* This iterator is NOT thread safe and is invalid when the data structure is modified.
362366
*/
363367
@Override
364368
public Iterator<T> iterator() {

src/com/jwetherell/algorithms/data_structures/Queue.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,11 @@ public String toString() {
158158
return builder.toString();
159159
}
160160

161+
/**
162+
* {@inheritDoc}
163+
*
164+
* This iterator is NOT thread safe and is invalid when the data structure is modified.
165+
*/
161166
@Override
162167
public Iterator<T> iterator() {
163168
return (new ArrayQueueIterator<T>(this));
@@ -328,6 +333,8 @@ public String toString() {
328333

329334
/**
330335
* {@inheritDoc}
336+
*
337+
* This iterator is NOT thread safe and is invalid when the data structure is modified.
331338
*/
332339
@Override
333340
public Iterator<T> iterator() {

src/com/jwetherell/algorithms/data_structures/Stack.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ public String toString() {
139139

140140
/**
141141
* {@inheritDoc}
142+
*
143+
* This iterator is NOT thread safe and is invalid when the data structure is modified.
142144
*/
143145
@Override
144146
public Iterator<T> iterator() {
@@ -299,6 +301,8 @@ public String toString() {
299301

300302
/**
301303
* {@inheritDoc}
304+
*
305+
* This iterator is NOT thread safe and is invalid when the data structure is modified.
302306
*/
303307
@Override
304308
public Iterator<T> iterator() {

0 commit comments

Comments
 (0)