Results 1 to 6 of 6
Thread: Help with Max Heap remove method
- 04-03-2012, 03:14 AM #1
Member
- Join Date
- Mar 2012
- Posts
- 8
- Rep Power
- 0
Help with Max Heap remove method
So I am not sure where I am going wrong, but my remove method is not displaying the correct output. any ideas where the fault is. below is my class for the maxHeap and following is the tester. I believe the correct output should be "9 6 8 5 1 2 3 4".
import java.util.*;
public class MaxHeap<T extends Comparable<? super T>> {
private ArrayList<T> theHeap;
private int size;
public MaxHeap()
{
theHeap = new ArrayList<T>();
size = 0;
}
public int compare(int parentIndex, int childIndex, ArrayList <T> theHeap)
{
int result = (theHeap.get(parentIndex)).compareTo(theHeap.get(c hildIndex));
return result;
}
public void add(T newElement)
{
theHeap.add(size, newElement);
int swapIndex = size;
while(swapIndex > 0 && compare(swapIndex, parent(swapIndex), theHeap) > 0)
{
swap(parent(swapIndex), swapIndex, theHeap);
swapIndex = parent(swapIndex);
}
size++;
}
public T remove()
{
T result = theHeap.get(0);
theHeap.set(0, theHeap.get(size-1));
System.out.print(theHeap.get(0));
size --;
int i = size-1;
int j = 0;
while(true)
{
if((leftChild(j) == i) && (compare(leftChild(j), j, theHeap) > 0))
{
swap(j, leftChild(j), theHeap);
j = leftChild(j); continue;
}
if((rightChild(j) <= i) && compare(leftChild(j), rightChild(j), theHeap) >= 0 && (compare(leftChild(j), j, theHeap) >0))
{
swap(j, leftChild(j), theHeap);
j = leftChild(j);
continue;
}
if((rightChild(j) <= i) && (compare(rightChild(j), leftChild(j), theHeap) >0) && (compare(rightChild(j), j, theHeap) > 0))
{
swap(j, rightChild(j), theHeap);
j = rightChild(j);
continue;
}
if((rightChild(j) >= i) || (compare(rightChild(j), j, theHeap) <=0))
{
swap(j, leftChild(j), theHeap);
j = rightChild(j);
break;
}
}
return result;
}
public void swap(int parentIndex, int childIndex, ArrayList<T> theHeap)
{
T temp = theHeap.get(parentIndex);
theHeap.set(parentIndex, theHeap.get(childIndex));
theHeap.set(childIndex, temp);
}
public int parent(int childIndex)
{
return (childIndex-1)/2;
}
public int leftChild(int parentIndex)
{
return 2*parentIndex + 1;
}
public int rightChild(int parentIndex)
{
return 2*parentIndex + 2;
}
public String toString()
{
StringBuilder S = new StringBuilder();
for(int i = 0; i<size; i++)
{
S.append(theHeap.get(i) + " ");
}
return S.toString();
}
}
Tester
public class MaxHeapTester {
public static void main(String [] args)
{
System.out.println("This will test a maxHeap");
MaxHeap<Integer> mh = new MaxHeap<Integer>();
mh.add(10);
mh.add(9);
mh.add(8);
mh.add(6);
mh.add(1);
mh.add(2);
mh.add(3);
mh.add(4);
mh.add(5);
System.out.println(" ");
System.out.println("This is before anything is removed");
System.out.println(mh.toString());
mh.remove();
System.out.println("This is after remove");
System.out.println(mh.toString());
}
}
- 04-03-2012, 06:48 AM #2
Re: Help with Max Heap remove method
A moderator added the code tags for you in your last thread. Don't you think it's time you look around the FAQs and learn how to do this for yourself?
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 04-03-2012, 07:43 AM #3
Member
- Join Date
- Mar 2012
- Posts
- 8
- Rep Power
- 0
Re: Help with Max Heap remove method
Sorry, I'm new at this.
Updated:
Here is the tester:Java Code:import java.util.*; public class MaxHeap<T extends Comparable<? super T>> { private ArrayList<T> theHeap; private int size; public MaxHeap() { theHeap = new ArrayList<T>(); size = 0; } public int compare(int parentIndex, int childIndex, ArrayList <T> theHeap) { int result = (theHeap.get(parentIndex)).compareTo(theHeap.get(childIndex)); return result; } public void add(T newElement) { theHeap.add(size, newElement); int swapIndex = size; while(swapIndex > 0 && compare(swapIndex, parent(swapIndex), theHeap) > 0) { swap(parent(swapIndex), swapIndex, theHeap); swapIndex = parent(swapIndex); } size++; } public T remove() { T result = theHeap.get(0); theHeap.set(0, theHeap.get(size-1)); System.out.print(theHeap.get(0)); size --; int i = size-1; int j = 0; while(true) { if((leftChild(j) == i) && (compare(leftChild(j), j, theHeap) > 0)) { swap(j, leftChild(j), theHeap); j = leftChild(j); continue; } if((rightChild(j) <= i) && compare(leftChild(j), rightChild(j), theHeap) >= 0 && (compare(leftChild(j), j, theHeap) >0)) { swap(j, leftChild(j), theHeap); j = leftChild(j); continue; } if((rightChild(j) <= i) && (compare(rightChild(j), leftChild(j), theHeap) >0) && (compare(rightChild(j), j, theHeap) > 0)) { swap(j, rightChild(j), theHeap); j = rightChild(j); continue; } if((rightChild(j) >= i) || (compare(rightChild(j), j, theHeap) <=0)) { swap(j, leftChild(j), theHeap); j = rightChild(j); break; } } return result; } public void swap(int parentIndex, int childIndex, ArrayList<T> theHeap) { T temp = theHeap.get(parentIndex); theHeap.set(parentIndex, theHeap.get(childIndex)); theHeap.set(childIndex, temp); } public int parent(int childIndex) { return (childIndex-1)/2; } public int leftChild(int parentIndex) { return 2*parentIndex + 1; } public int rightChild(int parentIndex) { return 2*parentIndex + 2; } public String toString() { StringBuilder S = new StringBuilder(); for(int i = 0; i<size; i++) { S.append(theHeap.get(i) + " "); } return S.toString(); } }
again, I apologize for the ignorance.Java Code:public class MaxHeapTester { public static void main(String [] args) { System.out.println("This will test a maxHeap"); MaxHeap<Integer> mh = new MaxHeap<Integer>(); mh.add(10); mh.add(9); mh.add(8); mh.add(6); mh.add(1); mh.add(2); mh.add(3); mh.add(4); mh.add(5); System.out.println(" "); System.out.println("This is before anything is removed"); System.out.println(mh.toString()); mh.remove(); System.out.println("This is after remove"); System.out.println(mh.toString()); } }
Thanks!
- 04-03-2012, 10:25 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Re: Help with Max Heap remove method
I wrote a blog article for this forum once (see the button near the top of this page) on the heap sort algorithm. It has the method(s) you're looking for.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-03-2012, 06:18 PM #5
Member
- Join Date
- Mar 2012
- Posts
- 8
- Rep Power
- 0
- 04-03-2012, 07:20 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Re: Help with Max Heap remove method
When people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
need help with the remove method on arrayList
By ShinTec in forum New To JavaReplies: 5Last Post: 02-16-2010, 09:38 AM -
no heap space... need more heap
By anupam.kumar in forum Advanced JavaReplies: 3Last Post: 02-08-2010, 04:42 PM -
How to remove “Input method” submenu selection from StyledText context menu?
By Mitja in forum SWT / JFaceReplies: 0Last Post: 09-21-2009, 11:33 AM -
how does the remove method work for sets and hashsets
By haridharna in forum Advanced JavaReplies: 4Last Post: 08-06-2007, 12:48 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks