最大スタック
最大の山は、親要素が子要素よりも大きく、完全にバイナリツリーであることです。
データ[1]は保存を開始し、データ[0]は空であり、使用されていません。データ[0]をサイズとして使用することもできます。
パブリッククラスmaxheap <tは比較可能<? Super t >> {private t [] data; private int size; private int capitious; public maxheap(int capacity){this.data =(t [])new comparable [capacity + 1]; size = 0; this.capacity = capuation;} public size(){return this.size;} public boolean isempty(){return size = = = = = 0;} this.capacity;}/** * @return最大ルートを表示(popmaxと比較して削除なしでのみ表示) {size ++; data [size] = item; shiftup(size);}/** * @return pop maximumroot(seekmaxと比較して、ポップアップは削除を意味します) */public t popmax(){swap(1、size-); shiftdown(1); return data [size+1];}/** * @parm conder node node node node node node cond void shiftup(int child){while(child> 1 && data [child] .compareto(data [chird / 2])> 0){swap(child、child / 2); child = child / 2;} / *** @param @param data array* @param b rurtion in in in nower mark on nower mark of reter of nower nower mar int b){if(data [a] .compareto(data [b])<0){// if data [b] big return b @return Return returnの要素は大きい*/private int max(int a、int b、int c){int besigt = max(a、b); maigd(beages、c); return gery;}/***@param父親の親ノードの下コーナーマークは父親であり、左の子供の父は父の父の父です。 (true){int lchild = father * 2; //左の子供int rchild =父 * 2 + 1; //右の子供int newfather =父; //ニューファーザーは更新されようとしています。親の下部コーナーマーク、左右のノードは誰ですか? if(lchild> size){//父ノードに左の子供も右の子供も戻っていない場合;} else if(rchild> size){//父ノードに左の子供のみがある場合、右の子供のnewfather = max(父、lchild); ==父){//父親は両方の子ノードよりも大きいことを意味し、テーブル名はすでに大きなルートヒープであるため、リターンを調整し続ける必要はありません; Shiftdown(NewFather)}}} public static void main(string [] args){//大きなルートヒープMaxheap <integer> maxheap = new maxheap <integer>(100); // save(int i = 0; i <100; i ++){maxheap.intersert(int)(int)(int)(100); integer [] arr = new Integer [100]; //ヒープから選択して、(int i = 0; i <100; i ++){arr [i] = maxheap.popmax(); out.print(arr [i]+"");} system.out.out.println();};}最大ヒープ:Shiftdown()関数は上記とは異なります
パブリッククラスmaxheap <tは比較可能<? Super t >> {private t [] data; private int size; private int capitiant; public maxheap(int capitiance){data =(t [])new comparable [capacity + 1]; this.capacity = capacity; size = 0;} public size(){return size;} public boolean isempty(){return size = 0;} public aid inters intems(t + 1] item; size ++; shiftup(size);}/** * @return pop max root(seekmaxと比較してポップアップは削除を意味する) data [1];} public void swap(int i、int j){if(i!= j){t temp = data [i]; data [i] = data [j]; data [j] = temp;}} public void shiftup(int k){while(k> 1 && data [k / 2]。 2;}} public void Shiftdown(int father){while(2 * father <= size){int newfather = 2 * father; if(newfather + 1 <= size && data [newfather + 1] .compareto(data [newfather])> 0){// data [j + 1] (data [父] .compareto(data [newfather])> = 0){break;} else {swap(father、newfather); //値は交換されますmaxheap <integer> maxheap = new maxheap <integer>(100); // save for(int i = 0; i <100; i ++){maxheap.insert((int)(math)(math.random() * 100) 100; i ++){arr [i] = maxheap.popmax(); system.out.print(arr [i]+"");} out.println();}}要約します
上記は、Java言語実装の最大ヒープコードの例に関するものであり、すべての人に役立つことを願っています。興味のある友人は、このサイトの他の関連トピックを引き続き参照できます。欠点がある場合は、それを指摘するためにメッセージを残してください。このサイトへのご支援をありがとうございました!