-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayHeapMin.java
executable file
·145 lines (127 loc) · 4.54 KB
/
ArrayHeapMin.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
package com.example.arrayBinaryTree;
import com.example.exceptions.EmptyCollectionException;
import com.example.interfaces.HeapMinADT;
/**
* ArrayHeap provides an array implementation of a minHeap.
*/
public class ArrayHeapMin<T extends Comparable<? super T>> extends ArrayBinaryTree<T> implements HeapMinADT<T> {
public ArrayHeapMin(int capacity) {
this.count = 0;
this.tree = createNewArray(capacity);
}
public ArrayHeapMin(T element) {
this.count = 1;
this.tree = createNewArray(DEFAULT_CAPACITY);
this.tree[0] = element;
}
public ArrayHeapMin() {
this(DEFAULT_CAPACITY);
}
/**
* {@inheritDoc }
*/
@Override
public void addElement(T obj) {
if (obj == null) {
throw new ClassCastException("The element to add most not be null");
}
if (this.count == this.tree.length) {
expandCapacity();
}
this.tree[this.count++] = obj;
if (this.count > 1) {
heapifyAdd();
}
}
private T[] createNewArray(int size) {
return (T[]) new Comparable[size];
}
/**
* Expand the capacity of the array Heap
*/
private void expandCapacity() {
T[] tempTree = createNewArray(this.tree.length * 2);
System.arraycopy(tree, 0, tempTree, 0, this.count);
this.tree = tempTree;
}
/**
* Adds the specified element to this heap in the appropriate position
* according to its key value. Note that equal elements are added to the
* right.
*/
protected void heapifyAdd() {
int currentIndex = this.count - 1;
int parentIndex = (currentIndex - 1) / 2;
while ((currentIndex != 0) && (this.tree[currentIndex].compareTo(this.tree[parentIndex]) < 0)) {
T temp = this.tree[currentIndex];
this.tree[currentIndex] = this.tree[parentIndex];
this.tree[parentIndex] = temp;
currentIndex = parentIndex;
parentIndex = (parentIndex - 1) / 2;
}
}
/**
* {@inheritDoc }
*/
@Override
public T removeMin() throws EmptyCollectionException {
if (isEmpty()) {
throw new EmptyCollectionException(EmptyCollectionException.EMPTY_COLLECTION);
}
T minElement = this.tree[0];
this.tree[0] = this.tree[count - 1];
heapifyRemove();
this.count--;
return minElement;
}
/**
* Reorders this heap to maintain the ordering property.
*/
private void heapifyRemove() {
int indexParent = 0;
int indexLeft = 1;
int indexRight = 2;
int indexNext;
if ((this.tree[indexLeft] == null) && (tree[indexRight] == null)) {
indexNext = this.count;
} else if (this.tree[indexLeft] == null) {
indexNext = indexRight;
} else if (this.tree[indexRight] == null) {
indexNext = indexLeft;
} else {
indexNext = this.tree[indexLeft].compareTo(this.tree[indexRight]) < 0 ? indexLeft : indexRight;
}
while ((indexNext < this.count) && indexLeft < this.count && indexRight < this.count && ((Comparable) this.tree[indexNext]).compareTo(this.tree[indexParent]) < 0) {
if (this.tree[indexNext].compareTo(this.tree[indexParent]) < 0) {
T temp = this.tree[indexNext];
this.tree[indexNext] = this.tree[indexParent];
this.tree[indexParent] = temp;
indexParent = indexNext;
indexLeft = (2 * indexParent) + 1;
indexRight = 2 * (indexParent + 1);
if (indexLeft < this.count && indexRight < this.count) {
if ((this.tree[indexLeft] == null) && (this.tree[indexRight] == null)) {
indexNext = this.count;
} else if (this.tree[indexLeft] == null) {
indexNext = indexRight;
} else if (this.tree[indexRight] == null) {
indexNext = indexLeft;
} else {
indexNext = this.tree[indexLeft].compareTo(this.tree[indexRight]) < 0 ? indexLeft : indexRight;
}
}
}
}
this.tree[this.count - 1] = null;
}
/**
* {@inheritDoc }
*/
@Override
public T findMin() throws EmptyCollectionException {
if (isEmpty()) {
throw new EmptyCollectionException(EmptyCollectionException.EMPTY_COLLECTION);
}
return this.tree[0];
}
}