二叉堆数据结构讲解: 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# 二叉堆的更多相关文章

  1. AC日记——二叉堆练习3 codevs 3110

    3110 二叉堆练习3  时间限制: 3 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description 给定N(N≤500,000)和N个整 ...

  2. codevs 3110 二叉堆练习3

    3110 二叉堆练习3 http://codevs.cn/problem/3110/ 题目描述 Description 给定N(N≤500,000)和N个整数(较有序),将其排序后输出. 输入描述 I ...

  3. 数据结构图文解析之:二叉堆详解及C++模板实现

    0. 数据结构图文解析系列 数据结构系列文章 数据结构图文解析之:数组.单链表.双链表介绍及C++模板实现 数据结构图文解析之:栈的简介及C++模板实现 数据结构图文解析之:队列详解与C++模板实现 ...

  4. POJ 2010 - Moo University - Financial Aid 初探数据结构 二叉堆

    考虑到数据结构短板严重,从计算几何换换口味= = 二叉堆 简介 堆总保持每个节点小于(大于)父亲节点.这样的堆被称作大根堆(小根堆). 顾名思义,大根堆的数根是堆内的最大元素. 堆的意义在于能快速O( ...

  5. 二叉堆(一)之 图文解析 和 C语言的实现

    概要 本章介绍二叉堆,二叉堆就是通常我们所说的数据结构中"堆"中的一种.和以往一样,本文会先对二叉堆的理论知识进行简单介绍,然后给出C语言的实现.后续再分别给出C++和Java版本 ...

  6. 二叉堆(二)之 C++的实现

    概要 上一章介绍了堆和二叉堆的基本概念,并通过C语言实现了二叉堆.本章是二叉堆的C++实现. 目录1. 二叉堆的介绍2. 二叉堆的图文解析3. 二叉堆的C++实现(完整源码)4. 二叉堆的C++测试程 ...

  7. 二叉堆(三)之 Java的实现

    概要 前面分别通过C和C++实现了二叉堆,本章给出二叉堆的Java版本.还是那句话,它们的原理一样,择其一了解即可. 目录1. 二叉堆的介绍2. 二叉堆的图文解析3. 二叉堆的Java实现(完整源码) ...

  8. 二叉堆(binary heap)

    堆(heap) 亦被称为:优先队列(priority queue),是计算机科学中一类特殊的数据结构的统称.堆通常是一个可以被看做一棵树的数组对象.在队列中,调度程序反复提取队列中第一个作业并运行,因 ...

  9. 在A*寻路中使用二叉堆

    接上篇:A*寻路初探 GameDev.net 在A*寻路中使用二叉堆 作者:Patrick Lester(2003年4月11日更新) 译者:Panic 2005年3月28日 译者序 这一篇文章,是&q ...

  10. 《Algorithms算法》笔记:优先队列(2)——二叉堆

    二叉堆 1 二叉堆的定义 堆是一个完全二叉树结构(除了最底下一层,其他层全是完全平衡的),如果每个结点都大于它的两个孩子,那么这个堆是有序的. 二叉堆是一组能够用堆有序的完全二叉树排序的元素,并在数组 ...

随机推荐

  1. Html中版权符号的字体选择问题(如何让版权符号更美观)

    一.发现问题 ©是html的中版权的符号,但是字体选择的不对会带来一些问题.如果是宋体,这个符号显示的就是很奇怪的一个符号. 二.解决问题 复制代码 代码如下: <span style=&quo ...

  2. [TypeScript] Avoid any type

    To avoid using "any" type is a best pratice. The reason for that is it disable the power o ...

  3. TTB 基本

    中文名 ,线程构建模块 外文名 Thread Building Blocks 缩    写 TBB 开    发 intel 目录 1线程构建模块 2黑体亮温 3斜交载重轮胎 4串联球轴承     1 ...

  4. CodeSmith使用总结--下拉列表和文件夹对话框属性

    上一篇有点短了,因为实在没有什么可说的,这一篇会多一点.O(∩_∩)O~ 一.下拉列表 关于如何在CodeSmith中创建一个下拉列表的属性框其实很简单,是要使用C#中的枚举就行了,看操作. 首先定义 ...

  5. [MVC4-基礎] 使用DataAnnotations+jQuery進行表單驗證

    我目前有以下表單,Select部分因為必須上一層有選擇下層才有資料,因此使用jQuery驗證問題類型是否有選擇就好,而問題描述要驗證是否為空,這裡採用MVC內建的DataAnnotations來驗證. ...

  6. document 例子

    <!DOCTYPE html> <html> <head> <title></title> <script type="te ...

  7. C#图像处理(2):给图片加白边

    C#图片处理给图片添加白边: /// <summary> /// 在图片上方加入白边 /// </summary> /// <param name="Img&q ...

  8. js中window.print()去除页眉页脚

    //jsp打印时去除页眉页页脚 打印前加入下面代码即可 var HKEY_Root,HKEY_Path,HKEY_Key; HKEY_Root="HKEY_CURRENT_USER" ...

  9. visifire 图表双坐标轴 silverlight

    public void CreateChart(Grid oGrid, ObservableCollection<ListItem> lBaseOilBar)        {       ...

  10. 0301——Notification 通知

    注册消息 [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(方法) name:@"消息名字&q ...