示例#1
0
        void BubbleUpMin(int index)
        {
            int p = HeapUtils.Parent(index);

            //swap, until Heap property isn't violated anymore
            while (p > 0 && values[p] > values[index])
            {
                Swap(p, index);
                index = p;
                p     = HeapUtils.Parent(index);
            }
        }
示例#2
0
        void BubbleDownMin(int index)
        {
            int l = HeapUtils.Left(index);
            int r = HeapUtils.Right(index);

            // bubbling down, 2 kids
            while (r <= Count)
            {
                // if heap property is violated between index and Left child
                if (values[index] > values[l])
                {
                    if (values[l] > values[r])
                    {
                        Swap(index, r);                         // right has smaller priority
                        index = r;
                    }
                    else
                    {
                        Swap(index, l);                         // left has smaller priority
                        index = l;
                    }
                }
                else
                {
                    // if heap property is violated between index and R
                    if (values[index] > values[r])
                    {
                        Swap(index, r);
                        index = r;
                    }
                    else
                    {
                        index = l;
                        l     = HeapUtils.Left(index);
                        break;
                    }
                }

                l = HeapUtils.Left(index);
                r = HeapUtils.Right(index);
            }

            // only left & last children available to test and swap
            if (l <= Count && values[index] > values[l])
            {
                Swap(index, l);
            }
        }