Здравствуйте нужна помощь надо добавить в этот код метод Майн для демонстрации работы других методов помогите пожалуйста как должен выглядит метод public dz6() { elements = new ArrayList<>(); } public void add(int element) {
elements.add(element); int currentIndex = elements.size() - 1;
while (currentIndex > 0) { int parentIndex = (currentIndex - 1) / 2;
if (elements.get(currentIndex) <= elements.get(parentIndex)) {
break;
} swap(currentIndex, parentIndex);
currentIndex = parentIndex;
}
}
public int remove() {
if (elements.isEmpty()) {
throw new IllegalStateException("Priority queue is empty.");
}
int root = elements.get(0);
int lastIndex = elements.size() - 1;
elements.set(0, elements.get(lastIndex));
elements.remove(lastIndex);
lastIndex--;
int currentIndex = 0;
while (true) {
int leftChildIndex = 2 * currentIndex + 1;
int rightChildIndex = 2 * currentIndex + 2;
int maxIndex = currentIndex;
if (leftChildIndex <= lastIndex && elements.get(leftChildIndex) > elements.get(maxIndex)) {
maxIndex = leftChildIndex;
}
if (rightChildIndex <= lastIndex && elements.get(rightChildIndex) > elements.get(maxIndex)) {
elements.add(element); int currentIndex = elements.size() - 1;
while (currentIndex > 0) { int parentIndex = (currentIndex - 1) / 2;
if (elements.get(currentIndex) <= elements.get(parentIndex)) {
break;
} swap(currentIndex, parentIndex);
currentIndex = parentIndex;
}
}
public int remove() {
if (elements.isEmpty()) {
throw new IllegalStateException("Priority queue is empty.");
}
int root = elements.get(0);
int lastIndex = elements.size() - 1;
elements.set(0, elements.get(lastIndex));
elements.remove(lastIndex);
lastIndex--;
int currentIndex = 0;
while (true) {
int leftChildIndex = 2 * currentIndex + 1;
int rightChildIndex = 2 * currentIndex + 2;
int maxIndex = currentIndex;
if (leftChildIndex <= lastIndex && elements.get(leftChildIndex) > elements.get(maxIndex)) {
maxIndex = leftChildIndex;
}
if (rightChildIndex <= lastIndex && elements.get(rightChildIndex) > elements.get(maxIndex)) {
maxIndex = rightChildIndex;
}
if (maxIndex == currentIndex) {
break;
} swap(currentIndex, maxIndex);
currentIndex = maxIndex;
}
return root;
}
private void swap(int i, int j)
{ int temp = elements.get(i);
elements.set(i, elements.get(j));
elements.set(j, temp);
}
}