D堆的实现
来源:互联网 发布:lindsey stirling 知乎 编辑:程序博客网 时间:2024/05/18 18:01
实现上一篇博客(http://blog.csdn.net/buleriver/article/details/38469977)说的D堆,如果把mD设置成2,D堆就退化成二叉堆,也就是说,二叉堆是D堆的一种情况。
public class DHeap { public static final int INIT_CAPACITY = 10; private int[] mArray; private int mLength; private final int mD; public DHeap(int d) { mArray = new int[INIT_CAPACITY + 1]; mLength = 0; mD = d; } public int getParentIndex(int index) { return (index - 2 + mD) / mD; } public int getChildIndex(int pIndex, int childIndex) { return mD * (pIndex - 1) + 2 + childIndex; } private void expandArray(int length) { int[] arr = new int[length]; System.arraycopy(mArray, 1, arr, 1, mLength); mArray = arr; } /** * 自下而上,把value放到最后一个,然后一直和其父节点交换,找到合适的位置 */ public void insert(int value) { if (mLength >= mArray.length - 1) { expandArray(mArray.length * 2); } mArray[++mLength] = value; int index = mLength; int parentIndex = getParentIndex(index); while (parentIndex > 0) { int currentValue = mArray[index]; int parentValue = mArray[parentIndex]; if (currentValue < parentValue) { mArray[parentIndex] = currentValue; mArray[index] = parentValue; index = parentIndex; parentIndex = getParentIndex(index); } else { break; } } } public void deleteMin() { if (mLength <= 0) { return; } else if (mLength == 1) { mLength--; } else { mArray[1] = mArray[mLength]; int index = 1; mLength--; while (true) { int value = mArray[index]; int minIndex = -1; // 最小孩子的数组索引 boolean lastLevel = false; // 是否已经到了最底层 int firstChildIndex = getChildIndex(index, 0); int lastChildIndex = firstChildIndex + mD; for (int childIndex = firstChildIndex; childIndex < lastChildIndex; childIndex++) { // 找到最小的孩子 if (childIndex > mLength) { // 已经到了最后一个 lastLevel = true; break; } int childValue = mArray[childIndex]; if (value > childValue) { value = childValue; minIndex = childIndex; } } if (minIndex < 0) { // 已经符合d堆的性质,不需要置换了 break; } else { // 需要置换 mArray[minIndex] = mArray[index]; mArray[index] = value; index = minIndex; } if (lastLevel) { // 已经到了最底层 break; } } } } }
2 0
- D堆的实现
- [灌水]d堆的实现与测试文件
- d-堆实现文件C语言
- d堆
- d-堆
- C++实现优先队列——最小堆,d路堆及配对堆
- 堆结构的实现
- 最小堆的实现
- 最大堆的实现
- 堆排序的实现
- 最大堆的实现
- 最小堆的实现
- 堆排序的实现
- 最小堆的实现
- 堆排序的实现
- 堆排序的实现
- 堆排序的实现
- 堆的实现
- Python函数的各种参数(含星号参数)
- 小猪的Android入门之路 Day 5
- poj 1979 Red and Black(DFS||BFS)
- TYVJ 1729 - 文艺平衡树
- Gradle-user guide-第7章 Java 快速开始
- D堆的实现
- 数据结构学习之堆栈
- 黑马程序员-泛型
- HDU3790 最短路径问题 【Dijkstra】
- no declaration can be found for element mvc resources
- eclipse导出jar包步骤及注意点
- Linux程序设计学习笔记----System V进程通信(共享内存)
- 用yum安装ceph
- POJ2240Arbitrage SPFA+邻接矩阵