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 二叉堆的定义 堆是一个完全二叉树结构(除了最底下一层,其他层全是完全平衡的),如果每个结点都大于它的两个孩子,那么这个堆是有序的. 二叉堆是一组能够用堆有序的完全二叉树排序的元素,并在数组 ...
随机推荐
- js+css实现模态层效果
在做web前端的时候,有些时候会涉及到模态层,在此提供一种实现思路.希望对大家实用.先贴效果吧: 模态层效果 以下说说在写模态层的时候的思路:通过可配置的參数width,height,title以及c ...
- 高性能MySql进化论【转】
高性能MySql进化论(十二):Mysql中分区表的使用总结 http://binary.duapp.com/category/sql 当数据量非常大时(表的容量到达GB或者是TB),如果仍然采用索引 ...
- hdu 4666 Hyperspace
曼哈顿距离,两个点设为(x1,y1),(x2,y2),其距离为|x1-x2|+|y1-y2| #include <cstdio> #include <set> #include ...
- 线程异常:undefined reference to 'pthread_create' 处理
源代码: #include <stdio.h> #include <pthread.h> #include <sched.h> void *producter_f ...
- Android模仿jquery异步请求
01 package com.xbl.task; 02 03 import java.io.BufferedReader; 04 import java.io.InputStream; 05 im ...
- 利用JS实现简单的瀑布流效果
哈哈, 我又来啦, 在这一段时间里, 我简单的学习了一下javascript(JS), 虽然不是很懂啦, 但是我也简单的尝试着做了点小东西, 就比如现在流行的瀑布流效果, 经过我的努力终于成功的完成了 ...
- Ubuntu 10.04下安装Qt
sudo apt-get install qt4-dev-tools qt4-doc qt4-qtconfig qt4-demos qt4-designer qt-creator 其中: qt4-de ...
- python - 面向对象(一)
python是一门面向对象的编程语言,python中的一切均是对象. 有对象就提到类,对象和类就像是儿子和老子的关系,是不可分的一对. 什么是类 类就是具有一些共同特性的事物的统称.好比人类, ...
- MySql查看表信息
SELECT TABLE_NAME, TABLE_COMMENT -- 指定信息列 FROM `information_schema`.`tables` A WHERE A.`TABLE_SCHEMA ...
- Java的优先级
序列号 符号 名称 结合性(与操作数) 目数 说明 1 . 点 从左到右 双目 ( ) 圆括号 从左到右 [ ] 方括号 从左到右 2 + 正号 从右到左 单目 - 负号 从右到左 单目 ++ 自增 ...