using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsolePractice
{
public class Node
{
public int data;
public Node(int key)
{
data = key;
}
} class Heap
{
Node[] heapArray = null;
private int maxSize = ;
private int currSize = ;
public Heap(int maxSize)
{
this.maxSize = maxSize;
heapArray = new Node[maxSize];
} public bool InsertAt(int pos, Node nd)
{
heapArray[pos] = nd;
return true;
} public void ShowArray()
{
for (int i = ; i < maxSize; i++)
{
if (heapArray[i] != null)
Console.Write(heapArray[i].data + " ");
}
Console.WriteLine();
} public void ShiftUp(int index)
{
int parent = (index - ) / ;
Node bottom = heapArray[index];
while ((index > ) && (heapArray[parent].data < bottom.data))
{
heapArray[index] = heapArray[parent];
index = parent;
parent = (parent - ) / ;
}
heapArray[index] = bottom;
} public bool Insert(int key)
{
if (currSize == maxSize)
return false;
heapArray[currSize] = new Node(key);
currSize++;
return true;
} public Node Remove()
{
Node root = heapArray[];
currSize--;
heapArray[] = heapArray[currSize];
ShiftDown();
return root;
} public void ShiftDown(int index)
{
int largerChild;
Node top = heapArray[index];
while (index < (int)(currSize / ))
{
int leftChild = * index + ;
int rightChild = leftChild + ;
if ((rightChild < currSize) && heapArray[leftChild].data < heapArray[rightChild].data)
largerChild = rightChild;
else
largerChild = leftChild;
if (top.data >= heapArray[largerChild].data)
break;
heapArray[index] = heapArray[largerChild];
index = largerChild;
}
heapArray[index] = top;
}
} class C_shape
{
static void Main()
{
const int SIZE = ;
Heap aHeap = new Heap(SIZE);
Random RandomClass = new Random();
for (int i = ; i < SIZE; i++)
{
int rn = RandomClass.Next(, );
aHeap.Insert(rn);
} Console.WriteLine("Random:");
aHeap.ShowArray(); Console.WriteLine("Heap:");
for (int i = (int)SIZE / - ; i >= ; i--)
{
aHeap.ShiftDown(i);
}
aHeap.ShowArray(); Console.WriteLine("Sorted:");
for (int i = SIZE - ; i >= ; i--)
{
Node bigNode = aHeap.Remove();
aHeap.InsertAt(i, bigNode);
}
aHeap.ShowArray();
Console.ReadKey();
}
}
}

最新文章

  1. php函数parse_url
  2. .net MVC 中枚举类型Enum 转化成 下拉列表的数据源
  3. C# iis错误配置信息( 500.21 - Internal Server Error )
  4. &lt;Oracle Database&gt;数据库启动与关闭
  5. 基础笔记3(一)(String StringBuilder StringBuffer 数组)
  6. 解决SSH无密码登陆后又需要密码登陆
  7. Javascript 中的false、0、null、undefined和空字符串对象
  8. NSS_11 Server Error in &#39;/&#39; Application
  9. centos6.5下逻辑卷操作
  10. 目录重定向的源代码工程( linux平台利用VFS实现目录重定向驱动)虚拟磁盘MINIPORT驱动代码(雨中风华)
  11. 【.NetRemoting-3】2015.09.18
  12. crawler_java应用集锦9:httpclient4.2.2的几个常用方法,登录之后访问页面问题,下载文件_设置代理
  13. js获取当前url
  14. c语言之gdb调试。
  15. 黑色半透明镂空遮罩指引效果实现jQuery小插件
  16. 解决谷歌浏览器频繁出现adobe flash player因过期而遭到阻止的问题(转自知乎)
  17. q.size()
  18. 手机站CSS
  19. 【BZOJ4803】逆欧拉函数
  20. 控制终端tcgetattr函数与tcsetattr函数

热门文章

  1. 决定如何开发你的WordPress主题框架
  2. iOS类别(Category)
  3. mysql CASE WHEN的基础和多种用法
  4. 教你50招提升ASP.NET性能(十五):解决性能问题时不要低估UI的价值
  5. 学习JSONP
  6. 理解TCP可靠的通信
  7. 2015南阳CCPC C - The Battle of Chibi DP
  8. BZOJ 4247 挂饰 背包DP
  9. Delphi7 第三方控件1stClass4000的TfcImageBtn按钮控件动态加载jpg图片例子
  10. 判断IE中iframe完美加载完毕的方法