二叉堆数据结构讲解: 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. git 技巧

    将某个文件回退到某个版本 git co d359624286d9c1f022b8b3b6f2d3fe3b6524188b build.sh 查看某个文件在某个版本时的内容 git show d3596 ...

  2. [AngularJS + RxJS] Search with RxJS

    When doing search function, you always need to consider about the concurrent requests. AEvent ----(6 ...

  3. io系统

    一.浅谈io系统 io系统的结构化思想是:输入-转换流-装饰器-输出. 对于字节流来说,常见的结构类为: package com.handchina.yunmart.middleware.servic ...

  4. Jquery与DOM对象

    在第一次学习jquery中,常常会不能分辨DOM对象和Jquery对象,下面我们就简诉一下它们之间的关系和区别 1.DOM对象(Document Object Model) 文档对象模型,每一份DOM ...

  5. 数据库分库分表(sharding)系列(四) 多数据源的事务处理

    系统经sharding改造之后,原来单一的数据库会演变成多个数据库,如何确保多数据源同时操作的原子性和一致性是不得不考虑的一个问题.总体上看,目前对于一个分布式系统的事务处理有三种方式:分布式事务.基 ...

  6. IOS — 关于Socket传输文件需要自定义延时或者包大小的情况

    1. 首先导入头文件 #include <stdio.h> #include <errno.h> #include <string.h> #include < ...

  7. UISearchDisplayController简单使用

    最近在做一个简单的app入门,中间有一个页面用到了搜索框,本来以为很简单的控件,没想到用到的时候才发现很麻烦. 搜索框使用过程大约有以下几个状态:不活跃-活跃-输入关键词-根据关键词动态列出相关结果- ...

  8. 8 Hbase get方式获取数据

    package com.hikvision.hbase.vertify.test; import org.apache.hadoop.conf.Configuration; import org.ap ...

  9. 重写String类,也有些区别,供参考

    头文件如下: #pragma once #include <string> #include <string.h> #include <stdlib.h> #inc ...

  10. C# ITextSharp pdf 自动打印

    PDF生成后直接进入打印预览不用下载 using iTextSharp.text; using iTextSharp.text.pdf; Document pdfDoc = new Document( ...