如何修复insertThenNext插入树中最小值时的返回错误?
PriorityQueue的insertThenNext方法测试失败
我实现了Java的PriorityQueue类,需要通过7项测试,目前前6项都已通过,但第7项「验证insertThenNext在插入值为树中所有元素的最小值时返回该插入值」测试失败。当前测试返回值为2,而预期应为1,队列输出也存在差异;我尝试过多种修改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() + ", "); } } }
内容的提问来源于stack exchange,提问作者Liberosis
相关产品推荐
相关产品推荐

