Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 598503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:23:43+00:00 2026-05-13T16:23:43+00:00

I have implemented Priority Queue interface for making heap. Can you tell me how

  • 0

I have implemented Priority Queue interface for making heap. Can you tell me how to implement an iterator on the top of that? point me to some apropriate tutorial,i am new to java and on a very short deadline here.
Actually i need a method to find and modify an object from heap on the basis of Object.id. I dont care if it is O(n).

public interface PriorityQueue {

    /**
     * The Position interface represents a type that can
     * be used for the decreaseKey operation.
     */
    public interface Position {

        /**
         * Returns the value stored at this position.
         * @return the value stored at this position.
         */
        Comparable getValue();
    }

    Position insert(Comparable x);

    Comparable findMin();

    Comparable deleteMin();

    boolean isEmpty();

    int size();

    void decreaseKey(Position p, Comparable newVal);
}

// BinaryHeap class

public class OpenList implements PriorityQueue {

    public OpenList() {
        currentSize = 0;
        array = new Comparable[DEFAULT_CAPACITY + 1];
    }

    public OpenList(int size) {
        currentSize = 0;
        array = new Comparable[DEFAULT_CAPACITY + 1];
        justtocheck = new int[size];
    }

    public OpenList(Comparable[] items) {
        currentSize = items.length;
        array = new Comparable[items.length + 1];

        for (int i = 0; i < items.length; i++) {
            array[i + 1] = items[i];
        }
        buildHeap();
    }

    public int check(Comparable item) {
        for (int i = 0; i < array.length; i++) {
            if (array[1] == item) {
                return 1;
            }
        }
        return array.length;
    }

    public PriorityQueue.Position insert(Comparable x) {
        if (currentSize + 1 == array.length) {
            doubleArray();
        }

        // Percolate up
        int hole = ++currentSize;
        array[ 0] = x;

        for (; x.compareTo(array[hole / 2]) < 0; hole /= 2) {
            array[hole] = array[hole / 2];
        }
        array[hole] = x;

        return null;
    }

    public void decreaseKey(PriorityQueue.Position p, Comparable newVal) {
        throw new UnsupportedOperationException(
            "Cannot use decreaseKey for binary heap");
    }

    public Comparable findMin() {
        if (isEmpty()) {
            throw new UnderflowException("Empty binary heap");
        }
        return array[ 1];
    }

    public Comparable deleteMin() {
        Comparable minItem = findMin();
        array[ 1] = array[currentSize--];
        percolateDown(1);

        return minItem;
    }

    private void buildHeap() {
        for (int i = currentSize / 2; i > 0; i--) {
            percolateDown(i);
        }
    }

    public boolean isEmpty() {
        return currentSize == 0;
    }

    public int size() {
        return currentSize;
    }

    public void makeEmpty() {
        currentSize = 0;
    }
    private static final int DEFAULT_CAPACITY = 100;
    private int currentSize;      // Number of elements in heap
    private Comparable[] array; // The heap array
    public int[] justtocheck;

    private void percolateDown(int hole) {
        int child;
        Comparable tmp = array[hole];

        for (; hole * 2 <= currentSize; hole = child) {
            child = hole * 2;
            if (child != currentSize &&
                array[child + 1].compareTo(array[child]) < 0) {
                child++;
            }
            if (array[child].compareTo(tmp) < 0) {
                array[hole] = array[child];
            } else {
                break;
            }
        }
        array[hole] = tmp;
    }

    private void doubleArray() {
        Comparable[] newArray;

        newArray = new Comparable[array.length * 2];
        for (int i = 0; i < array.length; i++) {
            newArray[i] = array[i];
        }
        array = newArray;

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T16:23:43+00:00Added an answer on May 13, 2026 at 4:23 pm

    You might look at java.util.PriorityQueue. If you’re in a hurry, Arrays.sort() may suffice. Once sorted, Arrays.binarySearch() becomes possible.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 298k
  • Answers 299k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Closures are a lexical feature, not a semantic one. If… May 13, 2026 at 7:38 pm
  • Editorial Team
    Editorial Team added an answer Really you should be using a vector with an offset.… May 13, 2026 at 7:38 pm
  • Editorial Team
    Editorial Team added an answer If you want to re-use your existing code you would… May 13, 2026 at 7:38 pm

Related Questions

Possible Duplicate: Priority queue in .Net This question is similar, but i want to
How can I implement background processing queues in my ASP.NET MVC web app? While
I have a priority_queue of some object: typedef priority_queue<Object> Queue; Queue queue; From time
I am working on implementing a function that would execute another function a few
I need to use a priority queue in my Python code, and: am looking

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.