数据结构之堆(java实现,有排序功能)
2012-12-24 16:04
465 查看
/* * 文 件 名: HeapNode.java * 修改时间: 2012-12-24 */ package heap; /** * 堆节点 * @version [版本号, 2012-12-24] */ public class HeapNode { /** * 用于排序的关键字 */ int key; /** * 节点的数据 */ double dData; /** * <默认构造函数> */ public HeapNode(int key, double dData) { this.key = key; this.dData = dData; } /** {@inheritDoc} */ @Override public String toString() { return "HeapNode [dData=" + dData + ", key=" + key + "]"; } }
/* * 文 件 名: Heap.java * 修改时间: 2012-12-24 */ package heap; /** * 数据结构之堆 * @version [版本号, 2012-12-24] */ public class Heap { /** * * 堆类型枚举 * @version [版本号, 2012-12-24] */ enum HeapType { MAX, MIN } /** * 堆节点数组 */ private HeapNode[] data; /** * 堆得类型 */ private HeapType heapType; /** * 元素个数,如果大于数组长度,需要扩容 */ private int elementN; /** * <默认构造函数> */ public Heap(HeapType heapType) { this(heapType, 20); } /** * <默认构造函数> */ public Heap(HeapType heapType, int capacity) { this.heapType = heapType; data = new HeapNode[capacity]; elementN = 0; } /** * 判断堆是否为空 * @return * @see [类、类#方法、类#成员] */ public boolean isEmpty() { return elementN == 0; } /** * 向堆中添加元素 * @see [类、类#方法、类#成员] */ public void addElement(int key, double dData) { HeapNode heapNode = new HeapNode(key, dData); //如果数组容量已满,则扩大一倍 if (elementN >= data.length) { HeapNode[] newArray = new HeapNode[data.length * 2]; System.arraycopy(data, 0, newArray, 0, data.length); data = newArray; } int currentIndex = elementN; data[elementN++] = heapNode; //下面从currentIndex索引开始回朔看看是否符合堆得特性 while (currentIndex > 0) { //这里需要判断堆得类型,是最大堆还是最小堆 if (heapType == HeapType.MAX && data[currentIndex].key <= data[(currentIndex - 1) / 2].key) { break; } else if (heapType == HeapType.MIN && data[currentIndex].key >= data[(currentIndex - 1) / 2].key) { break; } else { swapNode(currentIndex, (currentIndex - 1) / 2); currentIndex = (currentIndex - 1) / 2; } } } /** * 删除堆顶元素 * @return * @see [类、类#方法、类#成员] */ public HeapNode removeRoot() { HeapNode temp = null; if (!isEmpty()) { temp = data[0]; if (elementN > 1) { data[0] = data[elementN - 1]; //下面向下遍历各节点保持堆特性,从0开始遍历到elementN-2 reHeap(elementN - 2); } else { data[0] = null; } elementN--; } return temp; } /** * 将0到lastIndex的堆重新排列以符合堆得特性 * @param lastIndex * @see [类、类#方法、类#成员] */ private void reHeap(int lastIndex) { int currentIndex = 0; while (currentIndex * 2 + 1 <= lastIndex) { //如果有右节点 if (currentIndex * 2 + 2 <= lastIndex) { //如果右节点的值大于左节点的 if (data[currentIndex * 2 + 2].key >= data[currentIndex * 2 + 1].key) { if (heapType == HeapType.MAX && data[currentIndex].key <= data[currentIndex * 2 + 2].key) { swapNode(currentIndex, currentIndex * 2 + 2); currentIndex = currentIndex * 2 + 2; } else if (heapType == HeapType.MIN && data[currentIndex].key >= data[currentIndex * 2 + 1].key) { swapNode(currentIndex, currentIndex * 2 + 1); currentIndex = currentIndex * 2 + 1; } else { break; } } else { if (heapType == HeapType.MAX && data[currentIndex].key <= data[currentIndex * 2 + 1].key) { swapNode(currentIndex, currentIndex * 2 + 1); currentIndex = currentIndex * 2 + 1; } else if (heapType == HeapType.MIN && data[currentIndex].key >= data[currentIndex * 2 + 2].key) { swapNode(currentIndex, currentIndex * 2 + 2); currentIndex = currentIndex * 2 + 2; } else { break; } } } //如果没有右节点 else { if (heapType == HeapType.MAX && data[currentIndex].key <= data[currentIndex * 2 + 1].key) { swapNode(currentIndex, currentIndex * 2 + 1); currentIndex = currentIndex * 2 + 1; } else if (heapType == HeapType.MIN && data[currentIndex].key >= data[currentIndex * 2 + 1].key) { swapNode(currentIndex, currentIndex * 2 + 1); currentIndex = currentIndex * 2 + 1; } else { break; } } } } /** * 交换两个节点 * @see [类、类#方法、类#成员] */ private void swapNode(int index1, int index2) { HeapNode node = data[index1]; data[index1] = data[index2]; data[index2] = node; } /** * 返回堆中得元素个数 * @return * @see [类、类#方法、类#成员] */ public int getSize() { return elementN; } /** * 查看根元素 * @return * @see [类、类#方法、类#成员] */ public HeapNode getRoot() { HeapNode root = null; if (!isEmpty()) { root = data[0]; } return root; } /** * 遍历堆 * @see [类、类#方法、类#成员] */ public void displayHeap() { if (!isEmpty()) { for (int i = 0; i <= elementN - 1; i++) { System.out.println(data[i].toString()); } } } /** * 将堆中的元素排序 * @see [类、类#方法、类#成员] */ public void sort() { if (elementN > 1) { for (int i = elementN; i > 1; i--) { swapNode(0, i - 1); reHeap(i - 2); } } } }
相关文章推荐
- 数据结构-Java实现-排序
- Java实现对中文字符串的排序功能实例代码
- java实现ArrayList根据存储对象排序功能示例
- 【数据结构】-直接插入排序、希尔排序Java实现
- 数据结构(JAVA)---二叉树的简单实现及排序
- 一步步学习数据结构和算法之常用排序效率分析及java实现
- 算法和数据结构-选择排序、插入排序及希尔排序 java实现
- 数据结构各种排序java实现个人总结
- 数据结构:java创建串并实现增删改查功能
- Java中使用Comparable和Comparator实现字段排序功能
- 【数据结构】-快速排序Java实现
- 【数据结构】Java实现各类经典排序算法——选择排序、冒泡排序
- 数据结构复习之–“选择排序”-JAVA实现
- 一步步学习数据结构和算法之选择排序效率分析及java实现
- java实现数据结构-线性表-顺序表,实现插入,查找,删除,合并功能
- java中List的排序功能的实现
- java 顺序表实现增删改查,合并,排序功能
- 一步步学习数据结构和算法之直接插入排序效率分析及java实现
- java数据结构排序之冒泡实现
- 【java】数组/列表排序功能的两种实现