2015-04-04 34 views
1
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; 
    } 
} 

我這裏有一個最小堆類,它存儲鏈接對象基於權重屬性。我的目標是能夠直接訪問和更改存儲在最小堆中的對象的屬性。我需要能夠僅基於'loc'屬性訪問這些對象。
因此,例如,我可能想要訪問一個loc值爲6的Link對象並更改其父元素或權重屬性;但是我只知道它是訪問時的loc屬性值。
我的理解是,我應該使用這些對象的指針數組,但我不確定如何實現這一點。如何直接訪問一個最小堆的對象,JAVA

謝謝!

+0

你可以使用HashMap ; 「loc」將作爲關鍵字工作 – Prashant 2015-04-04 05:00:55

+0

你能解釋一下你在這裏想達到什麼嗎?堆是用於獲取排序輸出的特殊數據結構。您不能添加或更改堆的屬性中間。爲什麼不使用二叉樹? – 2015-04-04 05:01:34

回答

0

根據你的代碼,你的每一個環節都知道它在列表中的父,以及下一個元素。

您應該遵循下面的算法中,更新必要的字段。

  1. 首先將鏈接對象和loc值傳遞給將更新權重/父級的方法。你可以寫兩種方法來更新體重和其他來更新體重,或者你可以有一種方法和一個標記來分離活動。

  2. 如果要更新的權重,其簡單。

  3. 如果要更新父還必須通過新的父對象的方法,你應該更新下一個父爲好,但要確保你正確地處理你的代碼,你可能會失去存在的下一爲父母。

當你正在傳遞的實際對象,JVM將堅持不變。確保不要傳遞列表的重量/父母的值。