|
@@ -127,6 +127,41 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
|
127
|
127
|
public int size() {
|
128
|
128
|
return this.currentSize;
|
129
|
129
|
}
|
|
130
|
+
|
|
131
|
+ public boolean isValid() {
|
|
132
|
+
|
|
133
|
+ if(this.isEmpty()) {
|
|
134
|
+ return true;
|
|
135
|
+ }
|
|
136
|
+
|
|
137
|
+ return isValid(0);
|
|
138
|
+ }
|
|
139
|
+
|
|
140
|
+ private boolean isValid(int indexStart) {
|
|
141
|
+ //on doit vérifier que l'index est plus petit que ses deux parents
|
|
142
|
+
|
|
143
|
+ int ileft = indexLeft(indexStart);
|
|
144
|
+ int iright = ileft + 1;
|
|
145
|
+ boolean hasRight = iright < this.currentSize;
|
|
146
|
+ boolean hasLeft = ileft < this.currentSize;
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+ if(hasRight) {
|
|
150
|
+ //si on a un point à droite on vérifie que le point est valide, puis que tous les points en dessous à droite le sont.
|
|
151
|
+ if(this.array.get(indexStart).compareTo(this.array.get(iright)) > 0)return false;
|
|
152
|
+ if(!isValid(iright))return false;
|
|
153
|
+ }
|
|
154
|
+ if(hasLeft) {
|
|
155
|
+ //si on a un point à gauche on vérifie que le point est valide, puis que tous les points en dessous à gauche le sont aussi.
|
|
156
|
+ if(this.array.get(indexStart).compareTo(this.array.get(ileft)) > 0)return false;
|
|
157
|
+ if(!isValid(ileft))return false;
|
|
158
|
+ }
|
|
159
|
+
|
|
160
|
+ //ici, on a vérifié une branche ou les deux ou zéro, et on a pas fail donc c'est tout bon !
|
|
161
|
+ return true;
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+ }
|
130
|
165
|
|
131
|
166
|
@Override
|
132
|
167
|
public void insert(E x) {
|