代码拉取完成,页面将自动刷新
package second_term.tweflth_chapter;
import second_term.first_week.EmptyCollectionException;
public class BigArraryHeap<T> extends ArrayBinaryTree<T> implements BigHeapADT<T> {
int a =1;
public BigArraryHeap(){
super();
}
@Override
public void addElement(T objc) {
if (count == tree.length)
expandCapacity();
tree[count] = objc;
count=count+1;
modCount++;
if (count > 1)
heapifyAdd();
}
private void heapifyAdd() {
T temp;
int next = count - 1;
temp = tree[next];
while ((next != 0) &&
(((Comparable)temp).compareTo(tree[(next-1)/2]) > 0))
{
tree[next] = tree[(next-1)/2];
next = (next-1)/2;
}
tree[next] = temp;
// System.out.println("第"+a+"次排序:");
// for (int i =0;i<tree.length;i++){
// if (tree[i]!=null)
// System.out.print(tree[i]+" ");}
// System.out.println();
// a++;
}
@Override
public T removeMax() throws EmptyCollectionException {
if (isEmpty())
throw new EmptyCollectionException("ArrayHeap");
T maxElement = tree[0];
tree[0] = tree[count-1];
heapifyRemove();
count--;
modCount--;
return maxElement;
}
private void heapifyRemove() {
T temp;
int node = 0;
int left = 1;
int right = 2;
int next;
if ((tree[left] == null) && (tree[right] == null))
next = count;
else if (tree[right] == null)
next = left;
else if (((Comparable)tree[left]).compareTo(tree[right]) > 0)
next = left;
else
next = right;
temp = tree[node];
while ((next < count) &&
(((Comparable)tree[next]).compareTo(temp) > 0))
{
tree[node] = tree[next];
node = next;
left = 2 * node + 1;
right = 2 * (node + 1);
if ((tree[left] == null) && (tree[right] == null))
next = count;
else if (tree[right] == null)
next = left;
else if (((Comparable)tree[left]).compareTo(tree[right]) > 0)
next = left;
else
next = right;
}
tree[node] = temp;
}
@Override
public T findMax() {
if (isEmpty())
throw new EmptyCollectionException("ArrayHeap");
return tree[0];
}
public void sorting()
{
int i = count;
while (i > 0)
{
removeMax();
i--;
System.out.println(toString());
}
}
public String toString()
{
String result = "";
for (int i = 0; i < tree.length; i++)
{
if (tree[i] != null)
result += tree[i] + " ";
}
return result;
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。