堆-B-堆排序

来源:互联网 发布:淘宝销售额计算公式 编辑:程序博客网 时间:2024/05/04 11:04
package j_heap.B_heapSort;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


/**
 * 堆排序 
 * 数组显示的时候大小不能为数组个数,因为堆排序后,数组个数清0,将无法显示。而大小则不会
 * 缺点:没有屏蔽掉Heap类对堆的操作(insertAt()方法),数组和堆结构联系紧密
 * @author Administrator
 *
 */
public class HeapApp {
public static void main(String[] args) throws Exception {
int size, j;
System.out.print("请输入数组大小:");
size = getInt();
Heap theHeap = new Heap(size);


// 随机填入数字
for (j = 0; j < size; j++) {
int random = (int) (Math.random() * 100);
Node newNode = new Node(random);
theHeap.insert(j, newNode);
theHeap.incrementSize();
}


// 显示随机数组
System.out.print("Random: ");
theHeap.displayArray();


// 有子节点的节点放在对应的位置上
for (j = size / 2 - 1; j >= 0; j--) {
theHeap.trickleDown(j);
}


// 数组+树显示
System.out.print("Heap: ");
theHeap.displayArray();
theHeap.displayHeap();


for (j = size - 1; j >= 0; j--) {
Node biggestNode = theHeap.remove();
theHeap.insert(j, biggestNode);
}
System.out.print("Sorted: ");
theHeap.displayArray();
}


private static String getString() throws IOException {
return new BufferedReader(new InputStreamReader(System.in)).readLine();
}


private static int getInt() throws NumberFormatException, IOException {
return Integer.parseInt(getString());
}
}package j_heap.B_heapSort;


public class Heap {
private Node[] heapArray;
private int maxSize;
private int currentSize;


public Heap(int size) {
maxSize = size;
heapArray = new Node[maxSize];
currentSize = 0;
}


public Node remove() {
Node root = heapArray[0];
heapArray[0] = heapArray[--currentSize];
trickleDown(0);
return root;
}


/**
* 向下筛选 把index索引对应的数组值放在对应的位置上

* @param index
*/
public void trickleDown(int index) {
int largerChild;
Node top = heapArray[index];
while (index < currentSize / 2) {
int leftChild = 2 * index + 1;
int rightChild = leftChild + 1;


if (rightChild < currentSize
&& heapArray[rightChild].getiData() > heapArray[leftChild]
.getiData()) {
largerChild = rightChild;
} else {
largerChild = leftChild;
}
if (top.getiData() >= heapArray[largerChild].getiData())
break;
heapArray[index] = heapArray[largerChild];
index = largerChild;
}
heapArray[index] = top;
}


/**
* 二叉树显示
*/
public void displayHeap() {
int nBlanks = 32;
int itemsPerRow = 1;
int column = 0;
int j = -1;
while (j++ < currentSize - 1) {// 1:currentSize>0
// 外层空白
if (column == 0) {
for (int i = 0; i < nBlanks; i++) {
System.out.print(' ');
}
}
System.out.print(heapArray[j].getiData());
// 2:if(++j==currentSize)break;


// 控制换行和中间空白
if (++column == itemsPerRow) {
nBlanks /= 2;
itemsPerRow *= 2;
column = 0;
// 换第二层
System.out.println();
} else {// 中间空白
for (int i = 0; i < nBlanks * 2 - 2; i++) {
System.out.print(' ');
}
}
}
System.out.println();
}


/**
* 数组显示
*/
public void displayArray() {
//大小不能为数组个数,因为堆排序后,数组个数清0,将无法显示。而大小则不会
for (int i = 0; i < maxSize; i++) {
System.out.print(heapArray[i].getiData() + " ");
}
System.out.println();
}


/**
* 对应索引插入节点

* @param index
* @param newNode
*/
public void insert(int index, Node newNode) {
heapArray[index] = newNode;


}


/**
* 增长个数
*/
public void incrementSize() {
currentSize++;
}
}package j_heap.B_heapSort;


public class Node {
private int iData;


public Node(int iData) {
this.iData = iData;
}


public int getiData() {
return iData;
}


}
0 0
原创粉丝点击