C# 二叉堆
二叉堆数据结构讲解: http://www.cnblogs.com/yc_sunniwell/archive/2010/06/28/1766751.html
C#代码实现
using System;
using System.Collections.Generic; namespace 二叉堆
{
//从小到大
public class BinaryHeap
{
private int[] heap;
private int index = 1; public void Init(int count)
{
count++;
heap = new int[count]; } public void Enqueue(int value)
{
heap[index] = value; if (index == 1) {
index++;
return;
} //开始从最下层跟父节点对比,往上升级
int temp = 0;
int tempIndex = index;
int parentIndex = 0;
int tempI = tempIndex % 2 == 0 ? 0 : 1; //动态改变tempI=0?1,进入左右与父节点的值比较和交换
while (heap[tempIndex] < heap[(parentIndex = (tempIndex - tempI) / 2)])
{
temp = heap[tempIndex];
heap[tempIndex] = heap[parentIndex];
heap[parentIndex] = temp;
tempIndex = parentIndex; tempI = tempIndex % 2 == 0 ? 0 : 1; //判断我现在是左节点(i * 2),还是右节点(i - 1 * 2)
} index++;
} public int Dequeue()
{
if (index == 1)
return heap[index]; int result = heap[1];
heap[1] = 0; int temp = 0;
int tempIndex = 1;
int indexLeft = 0;
int indexRight = 0; //当堆里面有两个元素的时候,也就是index>=2的时候
while(tempIndex * 2 < index)
{
indexLeft = (tempIndex * 2 < index) ? tempIndex * 2 : 0;
indexRight = ((tempIndex) * 2 + 1 < index) ? (tempIndex) * 2 + 1 : 0; //两个节点都存在情况下
if(Convert.ToBoolean(indexLeft) && Convert.ToBoolean(indexRight))
{
if (heap[indexLeft] < heap[indexRight])
{
temp = heap[tempIndex];
heap[tempIndex] = heap[indexLeft];
heap[indexLeft] = temp; tempIndex = indexLeft;
}
else
{
temp = heap[tempIndex];
heap[tempIndex] = heap[indexRight];
heap[indexRight] = temp; tempIndex = indexRight;
}
}
else if (Convert.ToBoolean(indexLeft))
{ temp = heap[tempIndex];
heap[tempIndex] = heap[indexLeft];
heap[indexLeft] = temp; tempIndex = indexLeft;
}
else
{
break;
}
} if(tempIndex != index - 1)
{
heap[tempIndex] = heap[index - 1];
heap[index - 1] = 0; int tempI = tempIndex % 2 == 0 ? 0 : 1;
int parentIndex = 0; //动态改变tempI=0?1,进入左右与父节点的值比较和交换
while (heap[tempIndex] < heap[(parentIndex = (tempIndex - tempI) / 2)])
{
temp = heap[tempIndex];
heap[tempIndex] = heap[parentIndex];
heap[parentIndex] = temp;
tempIndex = parentIndex; tempI = tempIndex % 2 == 0 ? 0 : 1; //判断我现在是左节点(i * 2),还是右节点(i - 1 * 2)
}
} index--;
return result;
} public int GetMin()
{
if (index == 1)
return 0; return heap[1];
} public override string ToString()
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb = sb.Append("0-");
for (int i = 1; i < heap.Length; i++)
{
if (heap[i] == 0)
break;
sb = sb.Append(heap[i] + "-");
} sb = sb.Remove(sb.Length - 1, 1); return sb.ToString();
}
}
}
C# 二叉堆的更多相关文章
- AC日记——二叉堆练习3 codevs 3110
3110 二叉堆练习3 时间限制: 3 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 给定N(N≤500,000)和N个整 ...
- codevs 3110 二叉堆练习3
3110 二叉堆练习3 http://codevs.cn/problem/3110/ 题目描述 Description 给定N(N≤500,000)和N个整数(较有序),将其排序后输出. 输入描述 I ...
- 数据结构图文解析之:二叉堆详解及C++模板实现
0. 数据结构图文解析系列 数据结构系列文章 数据结构图文解析之:数组.单链表.双链表介绍及C++模板实现 数据结构图文解析之:栈的简介及C++模板实现 数据结构图文解析之:队列详解与C++模板实现 ...
- POJ 2010 - Moo University - Financial Aid 初探数据结构 二叉堆
考虑到数据结构短板严重,从计算几何换换口味= = 二叉堆 简介 堆总保持每个节点小于(大于)父亲节点.这样的堆被称作大根堆(小根堆). 顾名思义,大根堆的数根是堆内的最大元素. 堆的意义在于能快速O( ...
- 二叉堆(一)之 图文解析 和 C语言的实现
概要 本章介绍二叉堆,二叉堆就是通常我们所说的数据结构中"堆"中的一种.和以往一样,本文会先对二叉堆的理论知识进行简单介绍,然后给出C语言的实现.后续再分别给出C++和Java版本 ...
- 二叉堆(二)之 C++的实现
概要 上一章介绍了堆和二叉堆的基本概念,并通过C语言实现了二叉堆.本章是二叉堆的C++实现. 目录1. 二叉堆的介绍2. 二叉堆的图文解析3. 二叉堆的C++实现(完整源码)4. 二叉堆的C++测试程 ...
- 二叉堆(三)之 Java的实现
概要 前面分别通过C和C++实现了二叉堆,本章给出二叉堆的Java版本.还是那句话,它们的原理一样,择其一了解即可. 目录1. 二叉堆的介绍2. 二叉堆的图文解析3. 二叉堆的Java实现(完整源码) ...
- 二叉堆(binary heap)
堆(heap) 亦被称为:优先队列(priority queue),是计算机科学中一类特殊的数据结构的统称.堆通常是一个可以被看做一棵树的数组对象.在队列中,调度程序反复提取队列中第一个作业并运行,因 ...
- 在A*寻路中使用二叉堆
接上篇:A*寻路初探 GameDev.net 在A*寻路中使用二叉堆 作者:Patrick Lester(2003年4月11日更新) 译者:Panic 2005年3月28日 译者序 这一篇文章,是&q ...
- 《Algorithms算法》笔记:优先队列(2)——二叉堆
二叉堆 1 二叉堆的定义 堆是一个完全二叉树结构(除了最底下一层,其他层全是完全平衡的),如果每个结点都大于它的两个孩子,那么这个堆是有序的. 二叉堆是一组能够用堆有序的完全二叉树排序的元素,并在数组 ...
随机推荐
- Bootstrap--本地安装使用
1.到官网下载:http://v2.bootcss.com 2.下载后是一个压缩文件,把它放在相应的工作目录下,然后解压. 3.新建一个测试文件,然后导入两个文件.
- [置顶] 强制访问控制内核模块Smack
Smack(Simplified Mandatory Access Control Kernel)是Casey Schaufler[15]于2007年在LSM基础上实现的Linux强制访问控制安全模块 ...
- poj 2041 Unreliable Message 字符串处理
水的问题.直接附着到代码. //poj 2041 //sep9 #include <iostream> using namespace std; char mode[128]; char ...
- 【剑指offer】左旋转字符串
转载请注明出处:http://blog.csdn.net/ns_code/article/details/27366485 题目描写叙述: 汇编语言中有一种移位指令叫做循环左移(ROL),如今有个简单 ...
- 解决vsftpd 530 Permission denied报错
虚拟机装好RedHat后,准备使用filezilla连接,输入IP地址,root用户,密码,快速连接,报错: 故障排除: 1.首先检查系统是否开启了vsftp服务,如果没有开启,先开启该服务. 方法1 ...
- C# 操作 AppSettings节点
1.实例 //1.简单获取内容 string value = ConfigurationManager.AppSettings["one"] as string; Console. ...
- 为自己的Android应用添加广告
平时也写了不少Android小应用,但是都是做练习之用,从来没有想过添加广告赚钱这一说. 个人是非常反感在应用内添加广告这种行为的,非常影响用户体验,不小心点到广告的话,手机流量哗哗地就没了··· 但 ...
- Arc engine - Geodatabase.
Geodatabase以层次结构的数据对象来组织地理数据. 这些数据对象存储在要素类(Feature Classes).对象类(0bject classes)和数据集(Feature datasets ...
- EMCA常用命令 【weber整理必出精品】
EMCA常用命令 创建一个EM资料库 emca -repos create 重建一个EM资料库 emca -repos recreate 删除一个EM资料库 emca -repos drop 配置数据 ...
- C#通过生成ini文件,记住用户关闭程序之前的选择+忽略跨线程检查
1.在类的里面添加 //写配置文件 [DllImport("kernel32")] private static extern long WritePrivateProfileSt ...