堆迭代器java

时间:2011-08-15 10:27:31

标签: java iterator

我有一个堆树的代码,我坚持使用迭代器 我需要有序,预订和后期迭代器,但我不知道该怎么做。

如果某人有想法或示例,请提供帮助。

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 个答案:

答案 0 :(得分:1)

我从三个类开始,每个类一个,实现Iterator接口。为这些迭代器提供二进制堆的实例,让它们完成它们的工作。

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