人人做人人澡人人爽欧美,国产主播一区二区,久久久精品五月天,羞羞视频在线观看免费

當前位置:蘿卜系統(tǒng)下載站 > 技術(shù)開發(fā)教程 > 詳細頁面

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

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

更新時間:2022-08-03 文章作者:未知 信息來源:網(wǎng)絡 閱讀次數(shù):


using System;
using System.Collections;

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

溫馨提示:喜歡本站的話,請收藏一下本站!

本類教程下載

系統(tǒng)下載排行

網(wǎng)站地圖xml | 網(wǎng)站地圖html
主站蜘蛛池模板: 吉隆县| 平江县| 大埔区| 蕲春县| 观塘区| 正阳县| 卓尼县| 镇雄县| 宜阳县| 潢川县| 万荣县| 宁阳县| 伽师县| 阳信县| 天镇县| 内丘县| 乌兰县| 江川县| 张家口市| 舒兰市| 桑日县| 玉树县| 锦屏县| 东莞市| 饶阳县| 陇川县| 马边| 永福县| 巴南区| 塔河县| 大足县| 仙游县| 洛扎县| 阳西县| 黄山市| 理塘县| 镇巴县| 东乡| 武冈市| 凉城县| 天水市|