堆-A-基于完全二叉树实现,并且由数组存储的堆

package j_heap.A_CompleteTree;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
 * 基于完全二叉树实现,并且由数组存储的堆
 * @author Administrator
 *
 */
public class HeapApp {
public static void main(String[] args) throws IOException {
int value, value2;
Heap theHeap = new Heap(31);
boolean success;
theHeap.insert(70);
theHeap.insert(40);
theHeap.insert(30);
theHeap.insert(50);
theHeap.insert(80);
theHeap.insert(100);
theHeap.insert(20);
theHeap.insert(58);
theHeap.insert(60);


while (true) {
System.out.println("请输入操作的首字母");
System.out.println("show insert remove or change: ");
char choice = getChar();
switch (choice) {
case 's':
theHeap.displayHeap();
break;
case 'i':
System.out.print("请输入要插入的值:");
value = getInt();
success = theHeap.insert(value);
if (!success)
System.out.println("无法插入,堆已满");
break;
case 'r':
if (!theHeap.isEmpty()) {
theHeap.remove();
} else {
System.out.println("移除失败");
}
break;
case 'c':
System.out.println("分别写出修改的下标和值:");
value = getInt();
value2 = getInt();
success = theHeap.change(value, value2);
if (!success) {
System.out.println("无效索引");
}
break;
default:
System.out.println("无效操作");
}
}


}


private static char getChar() throws IOException {
return getString().charAt(0);
}


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.A_CompleteTree;


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


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


public boolean isEmpty() {
return currentSize == 0;
}


public boolean insert(int key) {
if (currentSize == maxSize)// 判断是否超过数组容量
return false;


Node newNode = new Node(key);
heapArray[currentSize] = newNode;
trickleUp(currentSize++);
return true;
}


/**
* 向上筛选

* @param index
*/
private void trickleUp(int index) {
int parent = (index - 1) / 2;// 父节点索引
Node bottom = heapArray[index];// 临时存储index节点
while (index > 0 && heapArray[parent].getiData() < bottom.getiData()) {
heapArray[index] = heapArray[parent];
index = parent;
parent = (index - 1) / 2;
}
heapArray[index] = bottom;
}


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


/**
* 向下筛选

* @param index
*/
private void trickleDown(int index) {
int largerChild;
Node top = heapArray[index];
while (index < currentSize / 2) {// 倒数第二层叶子节点
// 获取左右子节点
int childLeft = index * 2 + 1;
int childRight = childLeft + 1;


// 获取最大子节点, childRight<currentSize防止过界
if (childRight < currentSize
&& heapArray[childRight].getiData() > heapArray[childLeft]
.getiData()) {
largerChild = childRight;
} else {
largerChild = childLeft;
}
// 判断是否大于子节点
if (top.getiData() >= heapArray[largerChild].getiData()) {
break;
}


heapArray[index] = heapArray[largerChild];
index = largerChild;
}
heapArray[index] = top;
}


/**
* 修改

* @param index
*            索引
* @param newValue
*            值
* @return
*/
public boolean change(int index, int newValue) {
// 索引超出范围返回false
if (index < 0 || index >= currentSize)
return false;
int oldVal = heapArray[index].getiData();
heapArray[index].setiData(newValue);
if (oldVal > newValue)
trickleDown(index);
else if (oldVal < newValue)
trickleUp(index);
return true;
}


public void displayHeap() {
System.out.println("heapArray: ");
for (int i = 0; i < currentSize; i++) {
if (heapArray[i] != null) {
System.out.print(heapArray[i].getiData() + " ");
} else {
System.out.print("-- ");
}
}
System.out.println();


int nBlanks = 32;
int itemsPerRow = 1;
int column = 0;
int j = 0;
String dots = "..............";
System.out.println(dots + dots);
while (currentSize > 0) {
// 最外层空白显示
if (column == 0) {
for (int k = 0; k < nBlanks; k++) {
System.out.print(' ');
}
}
System.out.print(heapArray[j].getiData());
// 超过数组个数,退出
if (++j == currentSize)
break;
// 中间层显示结束后,跳到第二阶,控制最外层空白显示,列数清零,每行项乘2
if (++column == itemsPerRow) {
nBlanks /= 2;
itemsPerRow *= 2;
column = 0;
System.out.println();
} else {
// 中间层加显示
for (int k = 0; k < nBlanks * 2 - 2; k++)
System.out.print(' ');
}
}
System.out.println("\n" + dots + dots);
}
}package j_heap.A_CompleteTree;


public class Node {
private int iData;


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


public int getiData() {
return iData;
}


public void setiData(int iData) {
this.iData = iData;
}


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值