简体   繁体   中英

How to access objects in a min-heap directly, java

public class Link {
public int weight;
public int loc;
public Link next;
public boolean visited;
public Link parent;

public Link(int d, int loc){
    this.weight = d;
    this.loc = loc;
    this.visited = false;
}
public Link(Link p, int d, int loc){
    this.parent = p;
    this.weight = d;
    this.loc = loc;
}

public void printLink(){
    System.out.println(weight);
}
}

class LinkList{


public Link first;

public LinkList(){
    first = null;
}
public void add(int d, int loc){
    Link link = new Link(d, loc);
    if (first == null){
        first = link;
    }
    else{
        Link curr = first;
        while (curr.next!=null){
            curr = curr.next;
        }
        curr.next = link;
    }

}
public void printList(){
    Link currentLink = first;
    System.out.println("List: ");
    while(currentLink != null) {
        currentLink.printLink();
        currentLink = currentLink.next;
    }
    System.out.println("");
}
public boolean isEmpty(){
    return first == null;
}
public Link delete(){
    Link temp = first;
    first = first.next;
    return temp;
}

}

 public class MinHeap {
    public Link[] Heap;
    public int size;
    public int maxsize;

    private static final int FRONT = 0;

    public MinHeap(int maxsize, Link x)
    {
        this.maxsize = maxsize;
        this.size = 0;
        Heap = new Link[this.maxsize + 1];
        Heap[0] = x;
    }

    private int parent(int pos)
    {
        return pos / 2;
    }

    private int leftChild(int pos)
    {
        return (2 * pos);
    }

    private int rightChild(int pos)
    {
        return (2 * pos) + 1;
    }

    private boolean isLeaf(int pos)
    {
        if (pos >=  (size / 2)  &&  pos <= size)
        { 
            return true;
        }
        return false;
    }

    private void swap(int fpos, int spos)
    {
        Link tmp;
        tmp = Heap[fpos];
        Heap[fpos] = Heap[spos];
        Heap[spos] = tmp;
    }

    private void minHeapify(int pos)
    {
        if (!isLeaf(pos))
        { 
            if ( Heap[pos].weight > Heap[leftChild(pos)].weight  || Heap[pos].weight > Heap[rightChild(pos)].weight)
            {
                if (Heap[leftChild(pos)].weight < Heap[rightChild(pos)].weight)
                {
                    swap(pos, leftChild(pos));
                    minHeapify(leftChild(pos));
                }else
                {
                    swap(pos, rightChild(pos));
                    minHeapify(rightChild(pos));
                }
            }
        }
    }

    public void add(Link element)
    {
        Heap[++size] = element;
        int current = size;

        while (Heap[current].weight < Heap[parent(current)].weight)
        {
            swap(current,parent(current));
            current = parent(current);
        }   
    }

    public void print()
    {
        for (int i = 1; i <= size / 2; i++ )
        {
            System.out.print(" PARENT : " + Heap[i] + " LEFT CHILD : " + Heap[2*i] 
                + " RIGHT CHILD :" + Heap[2 * i  + 1]);
            System.out.println();
        } 
    }

    public void minHeap()
    {
        for (int pos = (size / 2); pos >= 1 ; pos--)
        {
            minHeapify(pos);
        }
    }
    public boolean inQ(Link d){
        int x = d.weight;
        for (int i = 0; i<size;i++){
            if (Heap[i].weight == x){
                return true;
            }
        }
        return false;
    }

    public Link remove()
    {
        Link popped = Heap[FRONT];
        Heap[FRONT] = Heap[size--]; 
        minHeapify(FRONT);
        return popped;
    }
}

I have a min heap class here, which stores Link objects based on weight attributes. My objective is to be able to access and change attributes of the objects stored in the min heap directly. I need to be able to access these objects based only on there 'loc' attribute.
So for example, I might want to access a Link object with loc value 6 and change its parent or weight attributes; however I only know it's loc attribute value at access time.
My understanding is that I should be using an array of pointers to these objects, but I'm unsure how I would implement this.

Thanks!

As per your code, you are every link knows its parent as well as next element in the list.

You should follow the below algo and update the requisite field.

  1. First pass the link object and the loc value to a method which would update the weight / parent. You can write two methods one to update the weight and other to update the weight or you can have one method and with a flag to segregate the activity.

  2. If you want to update the weight, its simple.

  3. If you want to update the parent the you must pass the new parent object also to the method as you should update the next for the parent as well, but make sure you handle your code properly as you might lose the existing next for the parent.

As you are passing the actual objects, jvm would persist the same. Make sure not to pass the value of list's weight / parent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM