D堆的实现
实现上一篇博客(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;
}
}
}
}
}
D堆的实现的更多相关文章
- Java 堆内存与栈内存异同(Java Heap Memory vs Stack Memory Difference)
--reference Java Heap Memory vs Stack Memory Difference 在数据结构中,堆和栈可以说是两种最基础的数据结构,而Java中的栈内存空间和堆内存空间有 ...
- [数据结构]——堆(Heap)、堆排序和TopK
堆(heap),是一种特殊的数据结构.之所以特殊,因为堆的形象化是一个棵完全二叉树,并且满足任意节点始终不大于(或者不小于)左右子节点(有别于二叉搜索树Binary Search Tree).其中,前 ...
- 《徐徐道来话Java》:PriorityQueue和最小堆
在讲解PriorityQueue之前,需要先熟悉一个有序数据结构:最小堆. 最小堆是一种经过排序的完全二叉树,其中任一非终端节点数值均不大于其左孩子和右孩子节点的值. 可以得出结论,如果一棵二叉树满足 ...
- 计算机程序的思维逻辑 (47) - 堆和PriorityQueue的应用
45节介绍了堆的概念和算法,上节介绍了Java中堆的实现类PriorityQueue,PriorityQueue除了用作优先级队列,还可以用来解决一些别的问题,45节提到了如下两个应用: 求前K个最大 ...
- JVM学习(2)——技术文章里常说的堆,栈,堆栈到底是什么,从os的角度总结
俗话说,自己写的代码,6个月后也是别人的代码……复习!复习!复习!涉及到的知识点总结如下: 堆栈是栈 JVM栈和本地方法栈划分 Java中的堆,栈和c/c++中的堆,栈 数据结构层面的堆,栈 os层面 ...
- 数据结构:优先队列 基于堆实现(python版)
#!/usr/bin/env python # -*- coding:utf-8 -*- ''' Author: Minion-Xu ''' #异常类 class HeapPriQueueError( ...
- 数据结构:堆排序 (python版) 小顶堆实现从大到小排序 | 大顶堆实现从小到大排序
#!/usr/bin/env python # -*- coding:utf-8 -*- ''' Author: Minion-Xu 小堆序实现从大到小排序,大堆序实现从小到大排序 重点的地方:小堆序 ...
- Java中堆内存和栈内存详解2
Java中堆内存和栈内存详解 Java把内存分成两种,一种叫做栈内存,一种叫做堆内存 在函数中定义的一些基本类型的变量和对象的引用变量都是在函数的栈内存中分配.当在一段代码块中定义一个变量时,ja ...
- AC日记——二叉堆练习3 codevs 3110
3110 二叉堆练习3 时间限制: 3 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 给定N(N≤500,000)和N个整 ...
- codevs 2879 堆的判断
codevs 2879 堆的判断 http://codevs.cn/problem/2879/ 题目描述 Description 堆是一种常用的数据结构.二叉堆是一个特殊的二叉树,他的父亲节点比两个儿 ...
随机推荐
- POJ 2355 Railway tickets
Railway tickets Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2472 Accepted: 865 De ...
- dom方法insertAfter的实现
在dom的原生api中,只用insertBefore,没有insertAfter,借助原有的api,可以模拟一个insterAfter. function insterAfter(newElement ...
- 刷题总结——解方程(NOIP2014)
题目: 题目描述 已知多项式方程: a0+a1x+a2x2+…+anxn=0 求这个方程在[1,m]内的整数解(n 和 m 均为正整数). 输入格式 输入共 n+2 行. 第一行包含 2 个整数 n. ...
- [BZOJ3261] 最大异或和 (异或前缀和,可持久化Trie)
Description 给定一个非负整数序列{a},初始长度为N. 有M个操作,有以下两种操作类型: 1.Ax:添加操作,表示在序列末尾添加一个数x,序列的长度N+1. 2.Q l r x:询问操作, ...
- [CQOI2010] 扑克牌 (二分答案,巧解)
Description 你有n种牌,第i种牌的数目为ci.另外有一种特殊的牌:joker,它的数目是m.你可以用每种牌各一张来组成一套牌,也可以用一张joker和除了某一种牌以外的其他牌各一张组成1套 ...
- [暑假集训--数论]poj2657 Comfort
Description A game-board consists of N fields placed around a circle. Fields are successively number ...
- 【NOIP2016练习】T2 跑跑步 (数论)
: 这场的难度是从高到低的 ..]of longint; n,m,i,ans:longint; function gcd(x,y:longint):longint; var r:longint; be ...
- LeetCode OJ——Unique Binary Search Trees II
http://oj.leetcode.com/problems/unique-binary-search-trees-ii/ 一题要求得出所有树的种类数,二题要求得出所有树. 在一题的基础上修改代码, ...
- Codeforces Gym101502 K.Malek and Summer Semester
K. Malek and Summer Semester time limit per test 1.0 s memory limit per test 256 MB input standard ...
- tcp异常断开的重连解决方法
1.select超时重连 http://bbs.chinaunix.net/thread-4162149-1-1.html 2.http://bbs.csdn.net/topics/350074818 ...