forked from techpanja/interviewproblems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractHeap.java
More file actions
82 lines (69 loc) · 2.06 KB
/
AbstractHeap.java
File metadata and controls
82 lines (69 loc) · 2.06 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
package heaps.heap;
import heaps.heap.model.Node;
/**
* Abstract Heap Class.
* User: rpanjrath
* Date: 10/24/13
* Time: 2:29 PM
*/
public abstract class AbstractHeap implements Heap {
Node[] heapArray;
private int maxSize;
private int currentSize;
protected AbstractHeap() {
}
@Override
public boolean isEmpty() {
return getCurrentSize() == 0;
}
@Override
public abstract boolean insert(int key);
@Override
public abstract Node remove();
protected abstract Node[] getHeapArray();
protected abstract int getCurrentSize();
@Override
public void displayHeap() {
String dots = "...............................";
System.out.println(dots + dots);
System.out.print("heapArray: ");
this.currentSize = getCurrentSize();
this.heapArray = getHeapArray();
for (int m = 0; m < this.currentSize; m++)
if (this.heapArray[m] != null) {
System.out.print(this.heapArray[m].getData() + " ");
}
else {
System.out.print("-- ");
}
System.out.println();
int nBlanks = 32;
int itemsPerRow = 1;
int column = 0;
int j = 0;
System.out.println(dots + dots);
while (this.currentSize > 0) {
if (column == 0)
for (int k = 0; k < nBlanks; k++)
System.out.print(" ");
System.out.print(this.heapArray[j].getData());
if (++j == this.currentSize)
break;
if (++column == itemsPerRow) {
nBlanks /= 2;
itemsPerRow *= 2;
column = 0;
System.out.println();
} else
for (int k = 0; k < nBlanks * 2 - 2; k++)
System.out.print(" ");
}
System.out.println("\n" + dots + dots);
}
public Node peek() {
this.heapArray = getHeapArray();
if (this.heapArray.length > 0)
return this.heapArray[0];
return null;
}
}