有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

oop如何直接访问minheap中的对象,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;
    }
}

我这里有一个min heap类,它根据权重属性存储链接对象。我的目标是能够直接访问和更改存储在最小堆中的对象的属性。我需要能够访问这些对象仅基于那里的'loc'属性
例如,我可能希望访问loc值为6的链接对象,并更改其父属性或权重属性;然而,我只知道它在访问时的loc属性值
我的理解是,我应该使用指向这些对象的指针数组,但我不确定如何实现这一点

谢谢


共 (1) 个答案

  1. # 1 楼答案

    根据你的代码,你知道每个链接都知道它的父元素以及列表中的下一个元素

    你应该按照下面的算法更新必要的字段

    1. 首先将链接对象和loc值传递给一个方法,该方法将更新权重/父对象。您可以编写两个方法,一个用于更新权重,另一个用于更新权重,或者您可以使用一个方法和一个标志来分隔活动

    2. 如果你想更新重量,这很简单

    3. 如果要更新父对象,则必须将新的父对象也传递给该方法,因为还应该更新父对象的下一个对象,但请确保正确处理代码,因为可能会丢失父对象的现有下一个对象

    在传递实际对象时,jvm将保持不变。确保不要传递列表的权重/父级的值