コード例 #1
0
 public void RebuildHeap <T>(BinaryHeap <T> heap) where T : IBinaryHeapElement
 {
     heap.RebuildAtIndex(mHeapIndex);
 }
コード例 #2
0
        public override void StartTest()
        {
            //测试范围:1创建,2,数组创建,3.插入,4,弹出,重建,指定位置重建,指定位置删除
            BinaryHeap <IntWrap> heap1 = new BinaryHeap <IntWrap>(10, BinaryHeapSortMode.kMin);
            BinaryHeap <IntWrap> heap2 = new BinaryHeap <IntWrap>(10, BinaryHeapSortMode.kMin);

            IntWrap[] mTestData = BuildRandomIntWrapArray(20);//BuildIntWrapArray(new int[]{ 10, 121, 11, 15, 18, 21, 7, 99, 182, 0, 87, 26, 21 });
            //TimeDebugger.S.Begin("Insert:NLog");
            heap1.Insert(mTestData, BinaryHeapBuildMode.kNLog);
            //TimeDebugger.S.End();

            //TimeDebugger.S.Begin("Insert:N");
            heap2.Insert(mTestData, BinaryHeapBuildMode.kN);
            //TimeDebugger.S.End();
            //TimeDebugger.S.Dump();

            WriteBegin("BinaryHeap");

            IntWrap insertItem = new IntWrap(39);

            heap1.Insert(insertItem);
            int     oldIndex   = insertItem.HeapIndex;
            IntWrap findResult = heap1.GetElement(oldIndex);

            if (insertItem.Equals(findResult))
            {
                WriteLine("## Success: Find Old Element In Index");
            }
            insertItem.SortScore = 7;
            insertItem.RebuildHeap(heap1);
            int newIndex = insertItem.HeapIndex;

            findResult = heap1.GetElement(newIndex);
            if (insertItem.Equals(findResult))
            {
                WriteLine("## Success: Find New Element In Index");
            }
            WriteLine("## InserTest: OldIndex:{0}, newIndex:{1}", oldIndex, insertItem.HeapIndex);

            IntWrap element      = null;
            int     processCount = 0;

            heap1.Sort(BinaryHeapSortMode.kMax);
            newIndex = insertItem.HeapIndex;
            WriteLine("## InsertTest: NewIndex After Sort:{0}", newIndex);
            heap1.RemoveAt(insertItem.HeapIndex);
            while (heap1.HasValue() && ++processCount < 100)
            {
                element = heap1.Pop();
                if (element != null)
                {
                    Write("     " + element.SortScore);
                }
                else
                {
                    Write("     NULL");
                }
            }
            WriteLine("");
            WriteEnd("BinaryHeap");
        }