六月婷婷综合激情-六月婷婷综合-六月婷婷在线观看-六月婷婷在线-亚洲黄色在线网站-亚洲黄色在线观看网站

明輝手游網(wǎng)中心:是一個(gè)免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺(tái)!

數(shù)據(jù)結(jié)構(gòu)與算法(C#完成)---二叉堆(數(shù)組完成)

[摘要]using System;using System.Collections;namespace DataStructure /// <summary> /// BinaryHeap 的摘要說明。-------二叉堆(基于數(shù)組的實(shí)現(xiàn)) /// </summary&...

using System;
using System.Collections;

namespace DataStructure
{
    /// <summary>
    /// BinaryHeap 的摘要說明。-------二叉堆(基于數(shù)組的實(shí)現(xiàn))
    /// </summary>
    public class BinaryHeap:IPriorityQueue
    {
         protected ArrayList array;
         //建立一個(gè)最多容納_length個(gè)對(duì)象的空二叉堆
         public BinaryHeap(uint _length)
         {
              //
              // TODO: 在此處添加構(gòu)造函數(shù)邏輯
              //
              array=new ArrayList((int)_length);
              array.Capacity=(int)_length;
         }         //堆中對(duì)象個(gè)數(shù)
         public virtual int Count{get{return this.array.Count;}
    }    //將成員數(shù)組變成用1為基數(shù)表達(dá)的形式
    public virtual object Item(int _i)
    {
         if(_i&gt;=this.array.Capacity)
         throw new Exception("My:out of index");//不能出界
         return this.array[_i-1];
    }
 
    #region IPriorityQueue 成員    //先將空洞放在數(shù)組的下一個(gè)位置上,也就是i(注:基數(shù)是1),然后和[i/2]位置上的數(shù)比較,如果小于則將空洞上移到[i/2]位置,而原先[i/2]位置上的對(duì)象則移到[i]上,否則就將空洞變?yōu)開obj----如此遞歸
    public void Enqueue(Object _obj)
    {
       // TODO: 添加 BinaryHeap.Enqueue 實(shí)現(xiàn)
       if( this.array.Count==this.array.Capacity )
           throw new Exception("My:priority queue is full");//如果優(yōu)先隊(duì)列已滿,則拋出異常
       this.array.Add(new object());
       int i=this.array.Count;
       while(i&gt;1&amp;&amp;Comparer.Default.Compare(this.array[i/2-1],_obj )&gt;0)
       {
           //this.Item(i)=this.Item(i/2);
           this.array[i-1]=this.array[i/2-1];
           i/=2;
       }
       this.array[i-1]=_obj;
    }    public object FindMin()
    {
         // TODO: 添加 BinaryHeap.FindMin 實(shí)現(xiàn)
         if( this.array.Count==0 )
             throw new Exception("My:priority queue is empty");//如果隊(duì)列是空的,則拋出異常
         return this.array[0];
    }    public object DequeueMin()
    {
         // TODO: 添加 BinaryHeap.DequeueMin 實(shí)現(xiàn)
         object tmpObj=this.FindMin();
         int i=1;
         while( (2*i+1)&lt;=this.array.Count)
         {
             if( Comparer.Default.Compare(this.array[2*i-1],this.array[2*i])&lt;=0 )
             {
                 this.array[i-1]=this.array[2*i-1];
                 this.array[2*i-1]=tmpObj;
                 i=2*i;
             }
             else
             {
                 this.array[i-1]=this.array[2*i];
                 this.array[2*i]=tmpObj;
                 i=2*i+1;
             }
         }         object delObj=this.array[i-1];//暫時(shí)儲(chǔ)存要?jiǎng)h去的元素         if(i!=this.array.Count)//如果搜索到的對(duì)象就是數(shù)組的最后一個(gè)對(duì)象,則什么都不要做
         {
             this.array[i-1]=this.array[this.array.Count-1];//添補(bǔ)空洞
         }
         this.array.RemoveAt(this.array.Count-1);//將最后一個(gè)對(duì)象刪除
         return delObj;
     }     #endregion
 }
}


主站蜘蛛池模板: 日韩在线国产精品 | 四虎永久在线精品 | 自拍视频免费 | 婷婷综合缴情亚洲狠狠图片 | 日本免费一区二区三区视频 | 欧美一区2区三区4区贰佰公司 | 天堂tv亚洲tv日本tv不卡 | 欧美特黄一级视频 | 在线欧美不卡 | 欧洲精品视频在线观看 | 小xav导航福利网址 香蕉中文在线 | 天天干夜夜骑 | 天堂网在线看 | 伊人久久大香 | 一级黄片一级毛片 | 婷婷综合久久狠狠色99h | 色天使亚洲综合在线观看 | 自拍一区在线观看 | 最新精品亚洲成a人在线观看 | 五月天色网址 | 天天躁天天狠天天透 | 亚洲色欲色欲www在线观看 | 亚洲免费观看在线视频 | 先锋资源色| 天堂一码二码专区 | 亚洲免费网 | 日韩高清不卡在线 | 四虎www成人影院观看 | 青青草 久久久 | 青青国产成人精品视频 | 欧美一级黄视频 | 日韩 欧美 综合 在线 制服 | 日韩a在线 | 亚洲欧洲eeea在线观看 | 伊人影院在线观看 | 亚洲视频日韩视频 | 亚洲精品一级毛片 | 青草影院在线观看 | 中文字幕成人免费高清在线 | 日韩视频精品在线 | 无码精品一区二区三区免费视频 |