优先队列

  1 package queue;
2
3 import java.lang.reflect.Array;
4 import java.util.Arrays;
5 import java.util.Comparator;
6
7
8 public class PriorityQueue<T>
9 {
10
11 private static final int DEFAULT_SIZE = 32;
12 private int size = 0;
13 private Object[] queue;
14 private final Comparator<? super T> comparator;
15
16
17 public PriorityQueue()
18 {
19 this(DEFAULT_SIZE, null);
20 }
21
22
23 public PriorityQueue(int initialCapacity, Comparator<? super T> comparator)
24 {
25 if (initialCapacity < 0)
26 throw new IllegalArgumentException("");
27
28 queue = new Object[initialCapacity];
29 this.comparator = comparator;
30 }
31
32
33 public void add(T object)
34 {
35 this.add(size++, object);
36 if (queue.length == size)
37 resize();
38 }
39
40
41 private void add(int index, T object)
42 {
43 int k = index;
44
45 while (k > 0) {
46 int parent = (k - 1) >>> 1;
47 Object t = queue[parent];
48
49 if (comparator.compare(object, (T) t) < 0)
50 break;
51
52 queue[k] = t;
53 k = parent;
54 }
55 queue[k] = object;
56 }
57
58
59 public T remove() throws Exception
60 {
61 if (size < 0)
62 throw new Exception("");
63
64 T object = (T) queue[0];
65 size--;
66 queue[0] = queue[size];
67
68 //this.printQueue();
69 this.remove(0, (T) queue[0]);
70
71 return object;
72 }
73
74
75 private void remove(int index, T object)
76 {
77 int half = size >>> 1;
78
79 while (index < half) {
80 int child = (index << 1) + 1;
81 T t = (T) queue[child]; int right = child + 1;
82
83 if (right < size && comparator.compare((T) queue[right], t) > 0)
84 t = (T) queue[child = right];
85
86 if (comparator.compare(object, t) > 0) break;
87
88 queue[index] = t;
89 index = child;
90 }
91 queue[index] = object;
92 }
93
94
95 private void resize()
96 {
97 queue = Arrays.copyOf(queue, size * 2);
98 }
99
100
101 public int size()
102 {
103 return this.size;
104 }
105
106
107 public void printQueue()
108 {
109 for (int i = 0; i < size; i++) {
110 System.out.println(queue[i].toString());
111 }
112 }
113
114
115 public static void main(String[] args) throws Exception
116 {
117 PriorityQueue<TestItem> queue = new PriorityQueue<TestItem>(5,
118 new TestItemComparator());
119
120 for (int i = 1; i <= 100; i++) {
121 String content = "this is test " + String.valueOf(i);
122 queue.add(new TestItem(i, content));
123 }
124
125 while (queue.size() > 0) {
126 System.out.println(queue.remove().getContent());
127 }
128 }
129 }
130
131
132 class TestItem
133 {
134
135 public static final int DEFAULT_PRIORITY = 5;
136 private int priority;
137 private String content;
138
139 public TestItem(String content) {
140 this(DEFAULT_PRIORITY, content);
141 }
142
143 public TestItem(int priority, String content) {
144 this.priority = priority;
145 this.content = content;
146 }
147
148 public int getPriority() {
149 return priority;
150 }
151
152 public void setPriority(int priority) {
153 this.priority = priority;
154 }
155
156 public String getContent() {
157 return content;
158 }
159
160 public void setContent(String content) {
161 this.content = content;
162 }
163
164 public String toString() {
165 return "[priority]" + priority + " " + content;
166 }
167
168 }
169
170 class TestItemComparator implements Comparator
171 {
172 public int compare(Object o1, Object o2) {
173 TestItem t1 = (TestItem) o1;
174 TestItem t2 = (TestItem) o2;
175
176 return t1.getPriority() > t2.getPriority() ? 1 : t1.getPriority() < t2
177 .getPriority() ? -1 : 0;
178 }
179 }

 

先放上以后在整理

posted @ 2012-03-21 10:53  rilley  阅读(232)  评论(0编辑  收藏  举报