我是StackOverflow的新手,我真的很抱歉格式错误,因为我无法准确地安排它。
我的问题是当我运行TestHeaps类时,作为我的Heaps基础的ArrayLists在我尝试添加元素时不起作用,大小停留在0(零)。
有人可以建议帮助解决为什么会这样吗?
package HeapLab;
import java.util.ArrayList;
import java.util.NoSuchElementException;
/**
* This class implements a generic max heap.
*
* @author Sesh Venugopal
*
* @param <T> Type of data stored in the heap, must implement method compareTo of
* generic Comparable<T> interface, to compare priorities.
*/
public class MaxHeap<T extends Comparable<T>>
{
/**
* Stores the heap items.
*/
@SuppressWarnings("FieldMayBeFinal")
private ArrayList<T> items;
/**
* Maintains iteration point.
*/
int cursor;
/**
* Initializes a new instance by setting up storage of given capacity.
*
* @param cap Initial storage capacity.
*/
public MaxHeap(int cap)
{
items = new ArrayList<>(cap);
cursor = -1;
}
/**
* Initializes a new instance by setting up storage of default initial capacity.
*/
public MaxHeap()
{
items = new ArrayList<>();
cursor = -1;
}
/**
* Sifts up the item at the given index.
*
* @param index Index of item to be sifted up.
*/
void siftUp(int index)
{
T me = items.get(index);
while (index > 0)
{
int pindex = (index-1) / 2;
T myparent = items.get(pindex);
if (me.compareTo(myparent) > 0)
{
items.set(index, myparent);
index = pindex;
}
else break;
}
items.set(index, me);
}
/**
* Sifts down the item at the given item.
*
* @param index Index of item to be sifted down.
*/
void siftDown(int index)
{
T me = items.get(index);
int lindex = 2*index + 1;
while (lindex < items.size())
{
T maxChild = items.get(lindex);
int maxIndex = lindex;
int rindex = lindex + 1;
if (rindex < items.size())
{
T rightChild = items.get(rindex);
if (rightChild.compareTo(maxChild) > 0)
{
maxChild = rightChild;
maxIndex = rindex;
}
}
if (maxChild.compareTo(me) > 0)
{
items.set(index, maxChild);
index = maxIndex;
lindex = 2*index + 1;
}
else break;
}
items.set(index, me);
}
/**
* Adds the given item to this heap.
*
* @param item Item to be added.
*/
public void add(T item)
{
items.add(item);
siftUp(items.size()-1);
}
/**
* Removes the maximum-priority (top) item of this heap.
*
* @return Removed item.
* @throws NoSuchElementException If this heap is empty.
*/
public T deleteMax()
{
if (items.isEmpty())
{
throw new NoSuchElementException();
}
T maxItem = items.get(0);
T lastItem = items.remove(items.size() - 1);
if (items.isEmpty())
{
return maxItem;
}
// move last item to vacant spot at top,and sift down
items.set(0, lastItem);
siftDown(0);
return maxItem;
}
/**
* Empties this heap by removing all items.
*/
public void clear()
{
items.clear();
}
/**
* Returns the number of items in this heap.
*
* @return Number of items in this heap.
*/
public int size()
{
return items.size();
}
/**
* Tells whether this heap is empty or not.
*
* @return True if this heap is empty, false otherwise.
*/
public boolean isEmpty()
{
return items.isEmpty();
}
/**
* Returns the first item in this heap, and sets the iteration cursor to the
* first position.
*
* @return First item in this heap, null if list is empty.
*/
public T first()
{
if (items.isEmpty()) { return null; }
cursor = 0;
return items.get(cursor);
}
/**
* Sets the cursor to the next position, and returns the item in this heap at
* that position. <br> To iterate over this heap, there
* must be a call to first( ), followed by successive calls to next( ). Iteration
* is in level-order sequence.
*
* @return Next item in this heap. Null if heap is empty, or cursor is at the
* end of the heap at the time this method is called, i.e. end of heap was
* reached.
*/
public T next()
{
if (cursor < 0 || cursor == (items.size() - 1))
{
return null;
}
cursor++;
return items.get(cursor);
}
@Override
public String toString()
{
String forReturn = "";
forReturn += "Index: 0 Item: " + first();
for(int y = 1; y < this.size(); y++)
{
forReturn += "Index: " + y + " Item: " + next();
}
return forReturn;
}
}
package HeapLab;
import java.util.ArrayList;
import java.util.NoSuchElementException;
public class MinHeap<T extends Comparable<T>>
{
/**
* Stores the heap items.
*/
@SuppressWarnings("FieldMayBeFinal")
private ArrayList<T> items;
/**
* Maintains iteration point.
*/
int cursor;
/**
* Initializes a new instance by setting up storage of given capacity.
*
* @param cap Initial storage capacity.
*/
public MinHeap(int cap)
{
items = new ArrayList<>(cap);
cursor = -1;
}
/**
* Initializes a new instance by setting up storage of default initial capacity.
*/
public MinHeap()
{
items = new ArrayList<>();
cursor = -1;
}
/**
* Sifts up the item at the given index.
*
* @param index Index of item to be sifted up.
*/
void siftUp(int index)
{
T me = items.get(index);
while (index > 0)
{
int pindex = (index - 1) / 2;
T myparent = items.get(pindex);
if (me.compareTo(myparent) < 0)
{
items.set(index, myparent);
index = pindex;
}
else break;
}
items.set(index, me);
}
/**
* Sifts down the item at the given item.
*
* @param index Index of item to be sifted down.
*/
void siftDown(int index)
{
T me = items.get(index);
int lindex = 2 * index + 1;
while (lindex < items.size())
{
T minChild = items.get(lindex);
int minIndex = lindex;
int rindex = lindex + 1;
if (rindex < items.size())
{
T rightChild = items.get(rindex);
if (rightChild.compareTo(minChild) <= 0)
{
minChild = rightChild;
minIndex = rindex;
}
}
if (minChild.compareTo(me) < 0)
{
items.set(index, minChild);
index = minIndex;
lindex = 2 * index + 1;
}
else break;
}
items.set(index, me);
}
/**
* Adds the given item to this heap.
*
* @param item Item to be added.
*/
public void add(T item)
{
items.add(item);
siftUp(items.size() - 1);
}
/**
* Removes the minimum-priority (top) item of this heap.
*
* @return Removed item.
* @throws NoSuchElementException If this heap is empty.
*/
public T deleteMin() throws ClassCastException
{
if (items.isEmpty())
{
throw new NoSuchElementException();
}
T minItem = items.get(0);
T nextMinItem0 = items.get(1);
T nextMinItem1 = items.get(2);
if (nextMinItem0.compareTo(nextMinItem1) == -1 || nextMinItem0.compareTo(nextMinItem1) == 0)
{
nextMinItem0 = items.remove(1);
items.set(0, nextMinItem0);
siftDown(0);
}
else
{
nextMinItem1 = items.remove(2);
items.set(0, nextMinItem1);
siftDown(0);
}
return minItem;
}
/**
* Empties this heap by removing all items.
*/
public void clear()
{
items.clear();
}
/**
* Returns the number of items in this heap.
*
* @return Number of items in this heap.
*/
public int size()
{
return items.size();
}
/**
* Tells whether this heap is empty or not.
*
* @return True if this heap is empty, false otherwise.
*/
public boolean isEmpty()
{
return items.isEmpty();
}
/**
* Returns the first item in this heap, and sets the iteration cursor to the
* first position.
*
* @return First item in this heap, null if list is empty.
*/
public T first()
{
if (items.isEmpty()) { return null; }
cursor = 0;
return items.get(cursor);
}
/**
* Sets the cursor to the next position, and returns the item in this heap at
* that position. <br> To iterate over this heap, there
* must be a call to first( ), followed by successive calls to next( ). Iteration
* is in level-order sequence.
*
* @return Next item in this heap. Null if heap is empty, or cursor is at the
* end of the heap at the time this method is called, i.e. end of heap was
* reached.
*/
public T next()
{
if (cursor < 0 || cursor == (items.size() - 1))
{
return null;
}
cursor++;
return items.get(cursor);
}
@Override
public String toString()
{
String forReturn = "";
forReturn += "Index: 0 Item: " + first();
for(int y = 1; y < this.size(); y++)
{
forReturn += "Index: " + y + " Item: " + next();
}
return forReturn;
}
}
package HeapLab;
import java.util.Random;
public class TestHeaps
{
private static final int heapSize = 30;
private static MaxHeap<Integer> maxHeap;
private static MinHeap<Integer> minHeap;
private static Random generate;
public static void main(String[] args)
{
System.out.println("Program start");
minHeap = new MinHeap<>(heapSize);
maxHeap = new MaxHeap<>(heapSize);
minHeap = minHeapRandom(minHeap);
maxHeap = maxHeapRandom(maxHeap);
deleteMinToScreen(minHeap);
deleteMaxToScreen(maxHeap);
}
static MinHeap<Integer> minHeapRandom(MinHeap<Integer> heap)
{
generate = new Random(); int newRandom;
for(int y = 0; y < heap.size(); y++)
{
newRandom = generate.nextInt(100);
heap.add(newRandom);
}
return heap;
}
static MaxHeap<Integer> maxHeapRandom(MaxHeap<Integer> heap)
{
generate = new Random(); int newRandom;
for(int r = 0; r < heap.size(); r++)
{
newRandom = generate.nextInt(100);
heap.add(newRandom);
}
return heap;
}
static void deleteMinToScreen(MinHeap<Integer> heap)
{
for(int i = 0; i < heap.size(); i++)
{
System.out.println("Index: " + i + " Item: " + heap.deleteMin());
}
}
static void deleteMaxToScreen(MaxHeap<Integer> heap)
{
for(int j = 0; j < heap.size(); j++)
{
System.out.println("Index: " + j + " Item: " + heap.deleteMax());
}
}
}