2011-08-15 143 views
1

我有這個堆樹的代碼,我堅持使用迭代器。
我需要按順序,預訂和後序迭代器,但我不知道如何去做。堆迭代器java

如果有人有想法或示例請幫忙。

class Numbers implements Comparable<Numbers> { 
     private int value; 

     public Numbers(int value) { 
      this.value = value; 
     } 

     public String toString() { 
      return Integer.toString(value); 
     } 

     public int getValue() { 
      return this.value; 

    } 

    public int compareTo(Numbers o) { 
     int tmp = o.getValue(); 
     if (value > tmp) 
     return 1; 
     if (value < tmp) 
     return -1; 
     return 0; 
    } 
} 

class BinaryHeapIsFull extends Exception { 
    BinaryHeapIsFull() { 
     super("There is no more place in the heap!"); 
    } 
} 

public class BinaryHeap<E extends Comparable> { 
    E[] elements; 
    int count; 

    public BinaryHeap(int maxSize) { 
     elements = (E[]) new Comparable[maxSize];          
     this.count = 0; 
    } 

    public void enqueue(E elem) throws BinaryHeapIsFull { 
     if (count == elements.length) 
     throw new BinaryHeapIsFull(); 

     int i = count++; 
     while (i > 0 && elements[(i - 1)/2].compareTo(elem) == 1) { 
     elements[i] = elements[(i - 1)/2]; 
     i = (i - 1)/2; 
     } 
     elements[i] = elem; 
    } 

    public E findMin() { 
     return elements[0]; 
    } 

    public E dequeueMin() { 
     if (count == 0) 
     return null; 
     E result = elements[0]; 

     E last = elements[--count]; 

     int i = 0; 
     while (2 * i + 1 <= count) { 
     int child = 2 * i + 1; 
     if (child < count 
       && elements[child + 1].compareTo(elements[child]) == -1) 
      child++; 
     if (last.compareTo(elements[child]) == -1 
       || last.compareTo(elements[child]) == 0) 
      break; 
     elements[i] = elements[child]; 
     i = child; 
     } 
     elements[i] = last; 
     return result; 
    } 

    public String toString() { 
     String print = ""; 
     for (int i = 0; i < count; i++) 
     print += elements[i].toString() + " "; 
     return print; 
    } 

    public void sort() { 
     int a = count; 
     for (int i = 0; i < a; i++) { 
     System.out.print(findMin() + " "); 
     dequeueMin(); 
     } 
    } 

    public static void main(String[] args) throws BinaryHeapIsFull { 
     BinaryHeap<Numbers> b = new BinaryHeap<Numbers>(10); 
     b.enqueue(new Numbers(6)); 
     System.out.println(b.toString()); 
     b.enqueue(new Numbers(3)); 
     System.out.println(b.toString()); 
     b.enqueue(new Numbers(4)); 
     System.out.println(b.toString()); 
     b.enqueue(new Numbers(1)); 
     System.out.println(b.toString()); 
     b.enqueue(new Numbers(5)); 
     System.out.println(b.toString()); 
     b.enqueue(new Numbers(0)); 
     System.out.println(b.toString()); 
     b.enqueue(new Numbers(2)); 
     System.out.println(b.toString()); 
     b.dequeueMin(); 
     System.out.println(b.toString()); 
     b.dequeueMin(); 
     System.out.println(b.toString()); 
     System.out.println(b.findMin()); 
     b.sort(); 

    } 
} 
+1

這功課嗎? –

+1

你明白這些迭代器的作用嗎?你知道元素N,N * 2和N * 2 + 1之間的關係嗎? – parsifal

回答

1

我會從三個類開始,每個類都有一個實現Iterator接口的類。爲這些迭代器提供一個二進制堆的實例,並讓它們完成它們的工作。

public class BinaryHeapPreOrderIterator implements Iterator { 
    // constructor and methods for Iterator here. 
}