10-binaryHeap, struktury danych, prirority queque

[ Pobierz całość w formacie PDF ]
//-->!# # ' ) )" "& ( ($%77F89 ) ) : < >((; =D " "&?E# # ?$%G " "&DE# #D $%:H? @ A BC: ? :: B@AI @AJGDA!# # ' ) )" "& ( ($%Queue q = new PriorityQueue();q.add(new Integer(23));q.add(new Integer(2));System.out.println( q.dequeue() + ", " );q.add(new Integer(3));q.add(new Integer(13));q.add(new Integer(10));while( !q.isEmpty() ) {System.out.println( q.dequeue() + ", " );}23, 13, 10, 3, 2,* + !-#("0 1, "./% (% -23 4 456!# # ' ) )" "& ( ($%77F!# # ' ) )" "& ( ($%? @ A BC: ? :: B@AI @AJGDAPriorityQueue q = new ArrayPQ();q.enqueue(new Integer(19));q.enqueue(new Integer(2));System.out.print(q.dequeue() + ", ");q.enqueue(new Integer(30));System.out.print(q.dequeue() + ", ");q.enqueue(new Integer(1));q.enqueue(new Integer(10));while (!q.isEmpty()) {System.out.print(q.dequeue() + ", ");}2, 19, 1, 10, 30,89 ) ) : < >((; =D " "&?E# # ?$%G " "&DE# #D $%:Hpublic interface PriorityQueue extends Queue {/*** remove the highest priority element in this queue* @return the highest priority element*/public Object dequeue();/*** get the highest priority element in this queue* @return the highest priority element*/public Object peek();}* + !-#("0 1, "./% (% -23 4 4 356K : JIntegerJ L* + !-#("0 1, "./% (% -2D GIpriorityG3 4 4 556!# # ' ) )M" "& ( ($%I N >M I C!# # ' ) ): PQ-&R -" "& ( ( A # " ) E$%7 PQ-&R -# " )EF D8S " % )M B :# &"Q- ):H7 Q$ ) U H :T@7 : I H U V B A@:public class ArrayPQ implements PriorityQueue {private ArrayList list = new ArrayList();public booleanisEmpty(){ return list.isEmpty(); }public intsize(){ return list.size(); }public Objectpeek(){ return list.get(minIndex()); }public voidenqueue(Objecte) { list.add(e); }public Objectdequeue(){int min = minIndex();Object result = list.get(min);list.remove(min);return result;}private int minIndex() {if (isEmpty()) throw new NoSuchElementException();int min = 0;for (int i = 1; i < list.size(); i++) {O(n)Comparable d = (Comparable) list.get(i);if (d.compareTo(list.get(min)) < 0) min = i;}return min;}}* + !-#("0 1 2, "./% (% -FE "Q% T D-) Q$ ) I5131522232521247065AIN501303 4 4 256* + !-#("0 1, "./% (% -23 4 4 O56PQ-&R - U# " )ELXPQ-& ) E# "R -public class BinaryHeap implements PriorityQueue {private Object[] elementData;private int size;public BinaryHeap() {elementData = new Object[10];}public boolean isEmpty() { return size == 0; }9967VQ$ ) D ) 0 I# ) 0ZT G Y#QTGD YQTQ$ ) D ) 0 I# ) 0ZT G Y#QTGD YQT7A7 ;IQ$ ) D ) 0 I# ) >0F C 4T G Y#QTGD YQT5131502153214655public int size() { return size; }public Object peek() {if (isEmpty()) throw new NoSuchElementException();return elementData[0];}...}3 4 4 W56* + !-#("0 1, "./% (% -23 4 4 656227123823259424105670117891011121312size* + !-#("0 1, "./% (% -51350 15216599222325 2470elementData2PQ-& ) E\ ) ( (# "R -Q9 ) )7 ) ( ( > YCQ9 ) )F K B I 8 ]B > ;YL @:G LCXF:Y^@B]^A Y BII131522236212215236132122152321136221523PQ-& ) E\ ) ( (# "R -Q9 ) )public class BinaryHeap implements PriorityQueue {private Object[] elementData;private int size;A;II6132113 15 21 22 2313 15 21 22 23613 15622 23 21615 13 22 23 21fixUp( 5 )* + !-#("0 1, "./% (% -23 4 4 [56public void enqueue(Object e) {ensureCapacity(size+1);elementData[size] = e;fixUp(size++);}private void fixUp(int k) {Comparable tmp = (Comparable) elementData[k];while (k > 0) {int p = (k-1)/2;if (tmp.compareTo(elementData[p]) >= 0) break;elementData[k] = elementData[p];k = p;}elementData[k] = tmp;Comparable ?}...* + !-#("0 1, "./% (% -23 4 456A \ _ `E "S$ -- 1)7aGDIM ?L )(1 D D9-.I I I -) ( 1 CB?,9 - >S.U:bc /dS)%h ) ) # 1 Q% _ `E "SQ%i "`E `) . $ -- 1))Xpublic class Integer implements Comparable {private int value;public Integer(int v) {this.value = v;}public boolean equals(Object that) {if (!(that instanceof Integer)) return false;return this.value == ((Integer)that).value;}public int compareTo(Object that) {int v = ((Integer)that).value;if (this.value == v) return 0;return (this.value < v) ? -1 : 1;}...JJ HInteger\L A VV@}F )(1 89-.7 ]DDMAIAIF L _ `E "S # )ed K @$ -- 1 Q%" ))-FG )D 1 Q% _ `E "S# `) . $ -- 1`E)D U:E SdQ% $ -) $ S)%g-C( 1 # d`E "f >c /d % %#777] % . I @% %g#g-I D A ] %.GIg#I D A ] %.GIg#A% %I g-A% %I g-A DI* + !-#("0 1, "./% (% -23 4 456* + !-#("0 1, "./% (% -23 4 456PQ-& ) E\ T 9 ) )# "R -)((7 T9 )) C)((>F UFF:@1315222321222315 21 22PQ-& ) E\ T 9 ) )# "R -)((public class BinaryHeap implements PriorityQueue {private Object[] elementData;private int size;B 8KALGD I A H:GD2315212223UDGA; B III15212322AI1521public Object dequeue() {Object result = peek();elementData[0] = elementData[--size];elementData[size] = null;if (size > 1) fixDown(0); // next slidereturn result;}...13 15 21 22 23152321 2215 22 2123fixDown( 0 )* + !-#("0 1, "./% (% -23 4 4563* + !-#("0 1, "./% (% -23 4 4565PQ-& ) E\ e j k# "R - # $ QYpublic class BinaryHeap implements PriorityQueue {...private void fixDown(int k) {int c;Comparable tmp = (Comparable) elementData[k];while ((c = 2 * k + 1) < size && (c > 0)) {if (c < size-1) {Comparable left = (Comparable) elementData[c];if (left.compareTo(elementData[c+1]) > 0) c++;}if (tmp.compareTo(elementData[c]) <= 0) break;elementData[k] = elementData[c];k = c;}elementData[k] = tmp;}...}* + !-#("0 1, "./% (% -23 4 4562A D @\ ) ( ( > O CQ9 ) )513257281332821297565665013013515241 55 35 26* + !-#("0 1, "./% (% -23 4 456OA D @\ T 9 ) ))((5132572813328212975656650130135152AK7 DQ @ S1 ) S " % )A - QdT # & "l-Q- )1 Q$i7 E) \ c C)0 >7 ) ( ( emQ9 ) )\ # EYnc nc $>gC>1iQC7 T 9 ) )\ e j k n c n c $)(( # $ QY>gC>1iQC41 55 35 26* + !-#("0 1, "./% (% -23 4 456W* + !-#("0 1, "./% (% -23 4 4566S " g) EL D# & - JQ-7 L) ((Q9 ) )7 KA:@F111091187811 96* + !-#("0 1, "./% (% -2Q @A7FFDS " g) EL D# & - JQ-Q @AD A@@ : emJ L # E I@Y10118911910710811 96575811 9 10673 4 456[D DU Ae"Q% n # p q r q ppC e j k CAA I$># 0 . ) 0 n 0 # $ Q>0oYQngZFA@ Bg l D @ @ : ej ks: J L # $ QY,,YgZ YgpZ YgpZ , Z g YhkZ > Z C Z > Z Z Z C Z , n Z Y Z Y Z 3Y Z ,,,56,,1111810log2nkk2kk2hk2hkk211k2n O(n)111197 61158107 6559876952nlog2n n11011 10 9 8 7 6 510119109117O(nlogn)11108* + !-#("0 1, "./% (% -1087 61197 6295810610811 9 107810587 6598710 611 9 1010 11 93 4 456 [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • marucha.opx.pl