日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java 最小堆_Java最小堆实现

發布時間:2024/9/19 java 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 最小堆_Java最小堆实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

package boke.heap1;

/**

* 堆結點

*

* @since jdk1.5及其以上

* @author 毛正吉

* @version 1.0

* @date 2010.05.24

*

*/

public class Node {

private int iData; // 結點數據是整型

public Node(int key) {

iData = key;

}

/**

* setKey

*

* @param id

*/

public void setKey(int id) {

iData = id;

}

/**

* getKey

*

* @return

*/

public int getKey() {

return iData;

}

}

2. 最小堆

package boke.heap1;

import boke.heap.Node;

/**

* 最小堆

*

* @since jdk1.5及其以上

* @author 毛正吉

* @version 1.0

* @date 2010.05.24

*

*/

public class MinHeap {

private Node[] heapArray; // 堆容器

private int maxSize; // 堆得最大大小

private int currentSize; // 堆大小

public MinHeap(int _maxSize) {

maxSize = _maxSize;

heapArray = new Node[maxSize];

currentSize = 0;

}

/**

* 自上而下調整

*

* @param start

* @param endOfHeap

*/

public void filterDown(int start, int endOfHeap) {

int i = start;

int j = 2 * i + 1; // j是i的左子女位置

Node temp = heapArray[i];

while (j <= endOfHeap) { // 檢查是否到最后位置

if (j < endOfHeap // 讓j指向兩子女中的小者

&& heapArray[j].getKey() > heapArray[j + 1].getKey()) {

j++;

}

if (temp.getKey() <= heapArray[j].getKey()) { // 小則不做調整

break;

} else { // 否則小者上移,i,j下降

heapArray[i] = heapArray[j];

i = j;

j = 2 * j + 1;

}

}

heapArray[i] = temp;

}

/**

* 自下而上的調整:從結點start開始到0為止,自下向上比較,如果子女的值小于雙親結點的值則互相交換

*

* @param start

*/

public void filterUp(int start) {

int j = start;

int i = (j - 1) / 2;

Node temp = heapArray[j];

while (j > 0) { // 沿雙親結點路徑向上直達根節點

if (heapArray[i].getKey() <= temp.getKey()) {// 雙親結點值小,不調整

break;

} else {// 雙親結點值大,調整

heapArray[j] = heapArray[i];

j = i;

i = (i - 1) / 2;

}

heapArray[j] = temp; // 回送

}

}

/**

* 堆中插入結點

*

* @param key

* @return

* @throws MinHeapException

*/

public boolean insert(int key) throws MinHeapException {

boolean bool = true;

if (isFull()) {

bool = false;

throw new MinHeapException("MinHeap is full!");

} else {

Node newNode = new Node(key);

heapArray[currentSize] = newNode;

filterUp(currentSize);

currentSize++;

}

return bool;

}

/**

* 刪除堆中的最小值

*

* @return

* @throws MinHeapException

*/

public Node removeMin() throws MinHeapException {

if (isEmpty()) {

throw new MinHeapException("MinHeap is empty!");

}

Node root = heapArray[0];

heapArray[0] = heapArray[currentSize - 1];

currentSize--;

filterDown(0, currentSize - 1);

return root;

}

/**

* 按某種格式輸出堆

*/

public void displayHeap() {

System.out.print("heapArray: ");

for (int i = 0; i < currentSize; i++) {

if (heapArray[i] != null) {

System.out.print(heapArray[i].getKey() + " ");

} else {

System.out.print("-- ");

}

}

System.out.println();

int nBlanks = 32; // heap format

int itemsPerRow = 1;

int column = 0;

int j = 0; // current item

String dots = "...............................";

System.out.println(dots + dots); // dotted top line

while (currentSize > 0) { // for each heap item

if (column == 0) { // first item in row

for (int k = 0; k < nBlanks; k++) { // preceding blanks

System.out.print(" ");

}

}

System.out.print(heapArray[j].getKey()); // display item

if (++j == currentSize) { // done?

break;

}

if (++column == itemsPerRow) { // end of row?

nBlanks /= 2; // half the blanks

itemsPerRow *= 2; // twice the items

column = 0; // start over on

System.out.println(); // next row

} else { // next item on row

for (int k = 0; k < nBlanks * 2 - 2; k++) {

System.out.print(' '); // interim blanks

}

}

}

System.out.println("\n" + dots + dots);

}

public boolean isEmpty() {

return currentSize == 0;

}

public boolean isFull() {

return currentSize == maxSize;

}

public void makeEmpty() {

currentSize = 0;

}

}

3. 堆異常

package boke.heap1;

/**

* 堆異常

*

* @since jdk1.5及其以上

* @author 毛正吉

* @version 1.0

* @date 2010.05.24

*

*/

public class MinHeapException extends Exception {

public MinHeapException() {

super("MinHeapException");

}

public MinHeapException(String exMsg) {

super(exMsg);

}

}

4.? 最小堆應用測試

package boke.heap1;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

/**

* 最小堆應用測試

*

* @since jdk1.5及其以上

* @author 毛正吉

* @version 1.0

* @date 2010.05.24

*

*/

public class MinHeapApp {

/**

* @param args

* @throws IOException

* @throws MinHeapException

*/

public static void main(String[] args) throws IOException, MinHeapException {

int value, value2;

MinHeap hp = new MinHeap(31);

boolean success;

hp.insert(53);

hp.insert(17);

hp.insert(78);

hp.insert(9);

hp.insert(45);

hp.insert(65);

hp.insert(87);

hp.insert(23);

while (true) {

System.out.print("Enter first letter of ");

System.out.print("show, insert, remove: ");

int choice = getChar();

switch (choice) {

case 's':

hp.displayHeap();

break;

case 'i':

System.out.print("Enter value to insert: ");

value = getInt();

success = hp.insert(value);

if (!success) {

System.out.println("Can't insert; heap is full");

}

break;

case 'r':

if (!hp.isEmpty()) {

hp.removeMin();

} else {

System.out.println("Can't remove; heap is empty");

}

break;

default:

System.out.println("Invalid entry\n");

}

}

}

/**

* 獲得控制臺輸入流

*

* @return

* @throws IOException

*/

public static String getString() throws IOException {

return new BufferedReader(new InputStreamReader(System.in)).readLine();

}

/**

* 獲得控制臺輸入字符

*

* @return

* @throws IOException

*/

public static char getChar() throws IOException {

return getString().charAt(0);

}

/**

* 獲得控制臺輸入整型

*

* @return

* @throws NumberFormatException

* @throws IOException

*/

public static int getInt() throws NumberFormatException, IOException {

return Integer.parseInt(getString());

}

}

分享到:

2010-05-31 08:29

瀏覽 5419

論壇回復 / 瀏覽 (4 / 6422)

評論

4 樓

maozj

2010-06-01

zwhc 寫道

對 hashtable 做個封裝不行嗎?

呵呵 可試試

3 樓

zwhc

2010-05-31

對 hashtable 做個封裝不行嗎?

2 樓

maozj

2010-05-31

路在轉角處 寫道

看懂了,是數據結構

呵呵 程序可以運行測試一下,那樣更直觀

1 樓

路在轉角處

2010-05-31

看懂了,是數據結構

總結

以上是生活随笔為你收集整理的java 最小堆_Java最小堆实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。