二叉堆数据结构讲解: 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. ffmpeg + sdl -03 简单音频播放器实现

    没办法,工作中遇到了问题. 目前NEC EMMA的架构如下: 从USB读入文件 -> 文件分析并提取Packet中的Payload Data   -> NEC HANDLE AVTrans ...

  2. leetcode:Minimum Path Sum(路线上元素和的最小值)【面试算法题】

    题目: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right w ...

  3. [Node.js] Use "prestart" in scripts

    Usually we run : npm start to start an app, then we we might call other script to do something: npm ...

  4. IEquatable(T) interface in .Net

    原文:http://weblogs.asp.net/pawanmishra/iequatable-t-interface-in-net 泛型方法: public static bool AreEqua ...

  5. windows程序消息机制(Winform界面更新有关)--转

    1. Windows程序消息机制 Windows GUI程序是基于消息机制的,有个主线程维护着消息泵.这个消息泵让windows程序生生不息. Windows程序有个消息队列,窗体上的所有消息是这个队 ...

  6. (转)CommandArgument用法

    1.绑定数据库中一个主键前台代码:<ItemTemplate>                        <asp:ImageButton ID="ibtnUpdate ...

  7. mysqlbinlog详解

    mysqlbinlog用于处理二进制日志文件的实用工具详解mysqlbinlog从二进制日志读取语句的工具.在二进制日志文件中包含的执行过的语句的日志可用来帮助从崩溃中恢复. binlog日志打开方法 ...

  8. Objective-C 协议(接口)

    Objective-C 协议类似于java语言中的接口 新建文件步骤:Objective-C File ---> File Type = "Protocol" @protoc ...

  9. css 文本域textarea显示成label标签

    <html> <head>     <title>textarea显示为label</title> <style type="text/ ...

  10. String类的使用说明

    (1)Length()取一个字符串的长度:public int length(); public calss StringLength1{ public static void main(String ...