惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Secure Thoughts
S
Securelist
P
Proofpoint News Feed
D
DataBreaches.Net
Cisco Talos Blog
Cisco Talos Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
Project Zero
Project Zero
A
About on SuperTechFans
罗磊的独立博客
WordPress大学
WordPress大学
月光博客
月光博客
Latest news
Latest news
C
Cyber Attacks, Cyber Crime and Cyber Security
GbyAI
GbyAI
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
博客园 - 三生石上(FineUI控件)
F
Fortinet All Blogs
W
WeLiveSecurity
Attack and Defense Labs
Attack and Defense Labs
V
Visual Studio Blog
Blog — PlanetScale
Blog — PlanetScale
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
P
Privacy International News Feed
AI
AI
博客园 - 司徒正美
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Stack Overflow Blog
Stack Overflow Blog
M
MIT News - Artificial intelligence
Help Net Security
Help Net Security
T
Tor Project blog
V
Vulnerabilities – Threatpost
C
Cisco Blogs
I
Intezer
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
MyScale Blog
MyScale Blog
雷峰网
雷峰网
MongoDB | Blog
MongoDB | Blog
Forbes - Security
Forbes - Security
V
V2EX
Apple Machine Learning Research
Apple Machine Learning Research
T
Threat Research - Cisco Blogs
B
Blog RSS Feed
博客园 - 叶小钗
N
News and Events Feed by Topic
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Simon Willison's Weblog
Simon Willison's Weblog
C
CERT Recently Published Vulnerability Notes
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
N
News and Events Feed by Topic

博客园 - 大天使泰瑞尔

通过反射机制控制前台的数据项的显示和隐藏 由partition看窗口函数 IE 6下CSS浮动样式的问题 转载一篇拼SQL字符串的语句 ASP.NET 2.0 中控件的简单异步回调 新手基础知识专用 今天终于学会了从客户端调用Web Service 学会了ASP.NET 2.0中的数据批量更新 - 大天使泰瑞尔 - 博客园 数据结构学习(6):队列 数据结构学习(5):链表 数据结构学习(4):栈 数据结构学习(2):汉诺塔问题 数据结构学习(1):搜索二叉树 冒泡算法的三种JavaScript表示 YUI学习(1):ToolTip的用法 XML学习失误系列(2):分清节点性质,使用nodeValue JavaScript&DHTML特效学习(1):MSN提示框 YUI的Drap&Drop对IE7不支持 Gmail邮箱可以直接注册了
数据结构学习(3):堆化优先队列
大天使泰瑞尔 · 2006-12-14 · via 博客园 - 大天使泰瑞尔

using System;

namespace DataStructure.HeapPQ
{
    
/// <summary>
    
/// Class1 的摘要说明。
    
/// </summary>

    class PriorityQueueApplet
    
{
        
/// <summary>
        
/// 应用程序的主入口点。
        
/// </summary>

        [STAThread]
        
static void Main(string[] args)
        
{
            
            
int n=10;
            ComparisonKey[] A
=new ComparisonKey[n];//A为需要进行排序的数组,10个
            
//将数组A初始化为进行10个整数的排序并对其进行打印
            for(int i=0;i<n;i++)
            
{
                A[i]
=new PQItem(squareOf(3*i-13));
                Console.Write(A[i]
+",");
            }

            Console.WriteLine();
            
//            本排序的整数输出为:169.100.49.16.1.4.25.64.121.196
            
//利用priorityQueue sorting对数组进行排序
            priorityQueueSort(A);
            
//排序之后打印数组A中的值
            for(int i=0;i<n;i++)
            
{
                Console.WriteLine(A[i]
+",");
            }

            Console.WriteLine();
        }

        
public static int squareOf(int x) return x*x; }
        
public static void priorityQueueSort(ComparisonKey[] A)
        
{
            
int i;//i为一个整数数组的索引变量
            int n=A.Length;//N为要排序数组A的长度
            PriorityQueue PQ=new PriorityQueue();//PQ初始为空
            for(i=0;i<n;i++) PQ.insert(A[i]);//将A的元素插入PQ中
            for(i=n-1;i>=0;i--) PQ.remove();//删除PQ的元素并将其插入A中
        }

    }



    
public class ListNode
    
{
        
public ComparisonKey item;
        
public ListNode link;
    }


    
class PriorityQueue
    
{
        
private int count;//优先队列中的元素数目
        private int capacity;//可用数组位置的数量
        private int capacityIncrement;//在数组扩展过程中容量的增幅

        
private ComparisonKey[] itemArray;//存放PQ元素的数组

        
/*---------------------*/

        
//定义构造函数
        public PriorityQueue()
        
{
            count
=0;
            capacity
=16;
            capacityIncrement
=8;
            itemArray
=new ComparisonKey[capacity];
        }


        
public int Size()
        
{
            
return count;
        }


        
        
/*insert()方法在链表中插入一个新的元素*/

        
public void insert(ComparisonKey newItem)
        
{
            
//如果itemArray容量不够
            
//可以通过capacityIncrement
            if(count==capacity-1)
            
{
                capacity
+=capacityIncrement;
                ComparisonKey[] tempArray
=new ComparisonKey[capacity];
                
for(int i=1;i<=count;i++)
                
{
                    tempArray[i]
=itemArray[i];
                }

                itemArray
=tempArray;
            }


            
//优先队列数量递增1 并在当前优先对联元素顺序的末端插入newItem
            count++;
            
int childLoc=count;
            
int parentLoc=childLoc/2;

            
while(parentLoc!=0)
            
{
                
if(newItem.compareTo(itemArray[parentLoc])<=0)
                
{
                    itemArray[childLoc]
=newItem;//存储newItem并返回
                    return;
                }

                
else
                
{
                    itemArray[childLoc]
=itemArray[parentLoc];
                    childLoc
=parentLoc;
                    parentLoc
=childLoc/2;//变换位置
                }

            }

            itemArray[childLoc]
=newItem;//将newItem放入最终剩下的位置

        }


        
public ComparisonKey remove()
        
{
            
if(count==0)//如果优先队列为空,返回true;
            {
                
return null;
            }

            
else//否则,返回根元素重新堆化
            {
                
//声明
                int currentLoc;//当前查看的位置
                int childLoc;//currentLoc的孩子
                ComparisonKey itemToPlace;//重新定位的元素值
                ComparisonKey itemToReturn;//返回的删除元素值
                
//初始化
                itemToReturn=itemArray[1];//存储稍后返回的根元素
                itemToPlace=itemArray[count--];//末端的叶子元素
                currentLoc=1;//从根开始的currentLoc
                childLoc=2*currentLoc;//从根的左孩子开始childLoc

                
while(childLoc<=count)//当仍然存在孩子的时候
                {
                    
//设childLoc为currentLoc的较大的孩子
                  if(childLoc<count)
                  
{//如果存在右孩子
                    if(itemArray[childLoc+1].compareTo(itemArray[childLoc])>0)
                        childLoc
++;
                  }

                    
//如果childLoc的元素大于itemToPlace
                    
//那么将这个较大元素移动到currentLoc;
                    
//并将currentLoc下移
        
                    
if(itemArray[childLoc].compareTo(itemToPlace)>0)
                    
{
                        itemArray[currentLoc]
=itemArray[childLoc];
                        currentLoc
=childLoc;
                        childLoc
=2*currentLoc;
                    }

                    
else
                    
{
                        itemArray[currentLoc]
=itemToPlace;
                        
return itemToReturn;
                    }

                }

                
//itemToPlace的最终替换
                itemArray[currentLoc]=itemToPlace;
                
//返回最初在根中的元素
                return itemToReturn;
            }


        }

    }


    
interface ComparisonKey
    
{
        
//        如果K1和K2均为ComparisonKey,那么K1.compareTo(k2)就会有三个值0,1,-1,
        
//        就是K1==K2,K1>K2,K1<K2顺序是compareTo方法定义的优先级别顺序
        int compareTo(ComparisonKey value);
        
//将ComparisonKey转换成可以打印的字符串
        string toOurString();
    }


    
public class PQItem:ComparisonKey
    
{

        
private int key;//key数据包括给出元素优先级别的整数关键字
        public PQItem(int value)
        
{
            key
=value;
        }

        
ComparisonKey 成员

    }

}