Results 1 to 5 of 5
Thread: Max Heap in Java?
- 06-15-2012, 07:17 AM #1
Member
- Join Date
- Jun 2012
- Posts
- 6
- Rep Power
- 0
Max Heap in Java?
Is MaxHeap available is Java?
I need a data structure in java which will take insertion time not more than O(lg n), deletion time not more than O(lg n) and access the maximum element not more than O(lg n), so I chose to go with MaxHeap, now I cant find any libraries which can help me.. or any good code for the same, that will also help - as i don't want to invent the wheel again. If there is any other datastructure predefined in JAVA meeting my need, then that could also be of great help to me.
- 06-15-2012, 07:39 AM #2
Member
- Join Date
- Jun 2012
- Posts
- 6
- Rep Power
- 0
Re: Max Heap in Java?
I think PriorityQue is the answer defined in Java, but i am not getting how to define the priority of the priority que, for example can I get example code to implement MaxHeap with Priority Que and MinHeap with a PriorityQue.
- 06-15-2012, 07:46 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
Re: Max Heap in Java?
For a min-heap change the sign of the priorities of the elements to be stored in the heap.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 06-15-2012, 08:30 AM #4
Member
- Join Date
- Jun 2012
- Posts
- 6
- Rep Power
- 0
Re: Max Heap in Java?
I think min heap is given by the default priority que but I need a max heap.. which uses a comparator, Collection.reverseOrder look promising, but I dont know how to use it, kindly do not use very java-sevvy words as I am a newbee here.
I cant understand meaning of the sign of priorities of the elements, and how to change it? If I am not wrong I think you are telling me to change the sign of each element if its a heap of integers/floats but that will again need to change sign when we retrieve, I am sure there must be some better solution.
Need to specify again... I need a MaxHeap of Integers.
- 06-15-2012, 09:04 AM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
Re: Max Heap in Java?
Have a look at this snippet:
The values 3, 2, 1 are printed; if you use the no-arg constructor for the PriorityQueue the sequence 1, 2, 3 will be printed.Java Code:PriorityQueue<Integer> pq= new PriorityQueue<Integer>(1, Collections.reverseOrder()); pq.add(3); pq.add(1); pq.add(2); while (!pq.isEmpty()) System.out.println(pq.poll());
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Performance tuning to eliminate java.lang.OutOfMemoryError: Java heap space - JTable
By Redefine12 in forum New To JavaReplies: 1Last Post: 05-04-2012, 07:21 PM -
java.lang.OutOfMemoryError: Java heap space Workaround
By hudrv in forum Advanced JavaReplies: 4Last Post: 09-18-2011, 08:17 PM -
Java heap
By ultras in forum New To JavaReplies: 1Last Post: 01-15-2011, 06:46 PM -
how to solve this ERROR --java.lang.OutOfMemoryError: Java heap space
By krunalpatel1410 in forum New To JavaReplies: 5Last Post: 08-13-2010, 10:04 AM -
Java.lang.ExceptionOutOfMemory: Java heap space. lulzwut?!
By Addez in forum New To JavaReplies: 4Last Post: 11-03-2009, 04:01 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks