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

明輝手游網中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

數據結構與算法(C#完成)---二叉堆(數組完成)

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

using System;
using System.Collections;

namespace DataStructure
{
    /// <summary>
    /// BinaryHeap 的摘要說明。-------二叉堆(基于數組的實現)
    /// </summary>
    public class BinaryHeap:IPriorityQueue
    {
         protected ArrayList array;
         //建立一個最多容納_length個對象的空二叉堆
         public BinaryHeap(uint _length)
         {
              //
              // TODO: 在此處添加構造函數邏輯
              //
              array=new ArrayList((int)_length);
              array.Capacity=(int)_length;
         }         //堆中對象個數
         public virtual int Count{get{return this.array.Count;}
    }    //將成員數組變成用1為基數表達的形式
    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 成員    //先將空洞放在數組的下一個位置上,也就是i(注:基數是1),然后和[i/2]位置上的數比較,如果小于則將空洞上移到[i/2]位置,而原先[i/2]位置上的對象則移到[i]上,否則就將空洞變為_obj----如此遞歸
    public void Enqueue(Object _obj)
    {
       // TODO: 添加 BinaryHeap.Enqueue 實現
       if( this.array.Count==this.array.Capacity )
           throw new Exception("My:priority queue is full");//如果優先隊列已滿,則拋出異常
       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 實現
         if( this.array.Count==0 )
             throw new Exception("My:priority queue is empty");//如果隊列是空的,則拋出異常
         return this.array[0];
    }    public object DequeueMin()
    {
         // TODO: 添加 BinaryHeap.DequeueMin 實現
         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];//暫時儲存要刪去的元素         if(i!=this.array.Count)//如果搜索到的對象就是數組的最后一個對象,則什么都不要做
         {
             this.array[i-1]=this.array[this.array.Count-1];//添補空洞
         }
         this.array.RemoveAt(this.array.Count-1);//將最后一個對象刪除
         return delObj;
     }     #endregion
 }
}


主站蜘蛛池模板: 亚洲经典在线观看 | 四虎综合网 | 日韩经典欧美精品一区 | 欧美一a一片一级一片 | 亚洲欧美综合一区二区三区四区 | 婷婷丁香在线视频 | 日本高清观看视频 | 日本大蕉香蕉大视频在线观看 | 日韩中文字幕在线亚洲一区 | 在线bt天堂网www在线下载 | 亚洲最新网站 | 日本三级在线播放线观看2021 | 欧美一区在线播放 | 日本骚片| 欧美呦呦在线 | 永久毛片| 亚洲男人在线 | 亚洲国产天堂久久综合226 | 亚洲综合网在线观看 | 日韩我不卡 | 青青草原视频在线 | 欧美午夜精品久久久久免费视 | 午夜手机视频 | 日韩夜夜操 | 日韩高清成人毛片不卡 | 日本成人免费观看 | 午夜福利国产一级毛片 | 啪啪导航 | 欧美在线国产 | 天天干天天射天天插 | 亚洲国产精品一区二区久久hs | 中文字幕第30页 | 欧美亚洲啪啪 | 亚州一二区 | 啪啪色视频| 欧美亚洲国产成人不卡 | 日韩欧美在线综合网 | 欧美伊人久久 | 外国毛片视频 | 中文字幕一二三四 | 图片区偷拍区小说区 |