Results 1 to 5 of 5
  1. #1
    proggrammer is offline Member
    Join Date
    Jun 2012
    Posts
    6
    Rep Power
    0

    Question 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.

  2. #2
    proggrammer is offline Member
    Join Date
    Jun 2012
    Posts
    6
    Rep Power
    0

    Default 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.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default 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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    proggrammer is offline Member
    Join Date
    Jun 2012
    Posts
    6
    Rep Power
    0

    Default 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.
    Quote Originally Posted by JosAH View Post
    For a min-heap change the sign of the priorities of the elements to be stored in the heap.
    Jos
    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.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default Re: Max Heap in Java?

    Quote Originally Posted by proggrammer View Post
    Need to specify again... I need a MaxHeap of Integers.
    Have a look at this snippet:

    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());
    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.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 1
    Last Post: 05-04-2012, 07:21 PM
  2. Replies: 4
    Last Post: 09-18-2011, 08:17 PM
  3. Java heap
    By ultras in forum New To Java
    Replies: 1
    Last Post: 01-15-2011, 06:46 PM
  4. Replies: 5
    Last Post: 08-13-2010, 10:04 AM
  5. Replies: 4
    Last Post: 11-03-2009, 04:01 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •