1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: How to display a Heap using Applet

  1. Mar 11, 2012 #1
    1. The problem statement, all variables and given/known data
    (Heap visualization) Write a Java applet that displays a heap graphically, as
    shown in Figure 24.7. The applet lets you insert and delete an element from the

    2. Relevant equations
    Ok so I have the heap part written but I dont understand how to do the Applet at all. I have figured out there is a special way an applet must be written and I wrote a simple applet that displayed a blue box just to test but I am wondering how I would get it to display a heap in the way it wants me too?

    3. The attempt at a solution

    public class MinHeap<E extends Comparable>{
    private java.util.ArrayList<E> list = new java.util.ArrayList<E>();

    public MinHeap(){

    public MinHeap(E[] objects){
    for (int i = 0; i < objects.length; i++)
    public void add(E newObject){
    int currentindex = list.size() - 1;

    while (currentindex > 0){
    int parentIndex = (currentindex-1)/2;
    E p = list.get(parentIndex);

    if (newObject.compareTo(p) >= 0){
    list.set(currentindex, p);
    currentindex = parentIndex;

    list.set(currentindex, newObject);
    public E remove(){
    if(list.size() == 0) return null;

    E removedobject = list.get(0);
    list.set(0, list.get(list.size() - 1));
    list.remove(list.size() - 1);

    int currentindex = 0;
    while(currentindex < list.size()){
    int left = 2*currentindex+1;
    int right = 2*currentindex+2;

    if(left >= list.size()) break;
    int max = left;
    if(right < list.size()){
    if(list.get(max).compareTo(list.get(right)) < 0){
    if (list.get(currentindex).compareTo(list.get(max)) < 0){
    E temp = list.get(max);
    list.set(max, list.get(currentindex));
    list.set(currentindex, temp);
    currentindex = max;
    return removedobject;
    public int getSize(){
    return list.size();
  2. jcsd
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Can you offer guidance or do you also need help?
Draft saved Draft deleted