I used the Queue Queue interface to create a heap. Can you tell me how to implement an iterator at the top of this? call me some suitable tutorial, I am new to java and for a very short time. Actually, I need a method to find and modify an object from a heap based on Object.id. I don't care if it's O (n).
public interface PriorityQueue { public interface Position { Comparable getValue(); } Position insert(Comparable x); Comparable findMin(); Comparable deleteMin(); boolean isEmpty(); int size(); void decreaseKey(Position p, Comparable newVal); }
// class BinaryHeap
public class OpenList implements PriorityQueue { public OpenList() { currentSize = 0; array = new Comparable[DEFAULT_CAPACITY + 1]; } public OpenList(int size) { currentSize = 0; array = new Comparable[DEFAULT_CAPACITY + 1]; justtocheck = new int[size]; } public OpenList(Comparable[] items) { currentSize = items.length; array = new Comparable[items.length + 1]; for (int i = 0; i < items.length; i++) { array[i + 1] = items[i]; } buildHeap(); } public int check(Comparable item) { for (int i = 0; i < array.length; i++) { if (array[1] == item) { return 1; } } return array.length; } public PriorityQueue.Position insert(Comparable x) { if (currentSize + 1 == array.length) { doubleArray(); }
source share