提问人:Liberosis 提问时间:11/3/2023 最后编辑:Liberosis 更新时间:11/3/2023 访问量:25
如何检查插入的值是否小于树中的其他所有值 insertThenNext 是否返回该值
How to check that insertThenNext returns the inserted value if it's less than everything else in the tree
问:
我尝试了多种解决方案,包括更改 insertThenNext 处理数字的方式,但我尝试过的所有可能的解决方案最终都会使代码无法通过它必须通过的其他测试。我已经问了很多人,并在网上寻找,没有任何帮助,这真的是我最后的手段,任何帮助都是值得赞赏的。
它有 7 项必须通过的检查,它们是
- 检查 changePriority 在不需要移动任何东西时是否正常工作。
- 检查 changePriority 在必须冒泡时是否正常工作。
- 检查 changePriority 在必须冒泡时是否正常工作。
- 检查删除在必须冒泡时是否正常工作。
- 检查删除在必须冒泡时是否正常工作。
- 检查 insertThenNext 是否正确冒泡
- 检查 insertThenNext 是否返回插入的值(如果该值小于树中的其他值)
除了我收到此输出的最终测试之外,我设法让它通过了所有检查:
2
1, 4, 6, 7, 10, 13, 14, 15, 16, 20,
我应该什么时候收到这个输出
1
2, 4, 6, 7, 10, 13, 14, 15, 16, 20,
public class PriorityQueue {
public class EmptyException extends RuntimeException {}
private int[] items = new int[10];
int size = 0;
private static int leftChild(int index) { return 2 * index + 1; }
private static int rightChild(int index) { return 2 * index + 2; }
private static int parent(int index) { return (index - 1) / 2; }
private void swap(int index1, int index2) {
int temp = items[index1];
items[index1] = items[index2];
items[index2] = temp;
}
public void insert(int p) {
if (size == items.length) {
int[] newItems = new int[size * 2];
System.arraycopy(items, 0, newItems, 0, size);
items = newItems;
}
int cur = size;
items[size++] = p;
while (cur > 0 && items[cur] < items[parent(cur)]) {
swap(cur, parent(cur));
cur = parent(cur);
}
}
public int next() throws EmptyException {
if (isEmpty()) {
throw new EmptyException();
}
int next = items[0];
size--;
items[0] = items[size];
int cur = 0;
while (true) {
if (smallestInFamily(cur) == rightChild(cur)) {
swap(cur, rightChild(cur));
cur = rightChild(cur);
} else if (smallestInFamily(cur) == leftChild(cur)) {
swap(cur, leftChild(cur));
cur = leftChild(cur);
} else {
break;
}
}
return next;
}
private int smallestInFamily(int index) {
int lc = leftChild(index);
int rc = rightChild(index);
if (rc < size && items[rc] < items[index] && items[rc] < items[lc]) {
return rc;
}
if (lc < size && items[lc] < items[index]) {
return lc;
} else {
return index;
}
}
public int size() { return size; }
public boolean isEmpty() { return size == 0; }
public void changePriority(int oldP, int newP) {
int index = findNodeIndex(oldP);
if (index != -1) {
items[index] = newP;
if (index > 0 && newP < items[parent(index)]) {
bubbleUp(index);
} else {
bubbleDown(index);
}
}
}
public void delete(int p) {
int index = findNodeIndex(p);
if (index != -1) {
items[index] = items[size - 1];
size--;
if (index < size) {
if (index > 0 && items[index] < items[parent(index)]) {
bubbleUp(index);
} else {
bubbleDown(index);
}
}
}
}
public int insertThenNext(int p) {
if (size == items.length) {
// Resize the array to accommodate more elements
int[] newItems = new int[size * 2];
System.arraycopy(items, 0, newItems, 0, size);
items = newItems;
}
int oldRoot = items[0];
items[0] = p;
bubbleDown(0);
return oldRoot;
}
private int findNodeIndex(int value) {
for (int i = 0; i < size; i++) {
if (items[i] == value) {
return i;
}
}
return -1;
}
private void bubbleUp(int index) {
while (index > 0 && items[index] < items[parent(index)]) {
swap(index, parent(index));
index = parent(index);
}
}
private void bubbleDown(int index) {
int maxIndex = index;
int leftChild = leftChild(index);
if (leftChild < size && items[leftChild] < items[maxIndex]) {
maxIndex = leftChild;
}
int rightChild = rightChild(index);
if (rightChild < size && items[rightChild] < items[maxIndex]) {
maxIndex = rightChild;
}
if (index != maxIndex) {
swap(index, maxIndex);
bubbleDown(maxIndex);
}
}
public static void main(String[] args) {
PriorityQueue pq = new PriorityQueue();
pq.insert(Integer.MAX_VALUE); // Initialize with a sentinel value
int[] elementsToInsert = new int[] {6, 2, 14, 4, 10, 16, 13, 7, 15, 20};
for (int i : elementsToInsert)
pq.insertThenNext(i);
while (!pq.isEmpty()) {
System.out.print(pq.next() + ", ");
}
}
}
答: 暂无答案
评论
20,