Beispiel #1
0
    private void SortDown(aNode node)
    {
        while (true)
        {
            int childIndexLeft  = node.HeapIndex * 2 + 1;
            int childIndexRight = node.HeapIndex * 2 + 2;
            int swapIndex       = 0;

            if (childIndexLeft < currentItemcount)
            {
                swapIndex = childIndexLeft;
                if (childIndexRight < currentItemcount && nodes[childIndexLeft].CompareTo(nodes[childIndexRight]) < 0) // if he has a child on the right and the child on the right has a higher priority
                {
                    swapIndex = childIndexRight;
                }

                if (node.CompareTo(nodes[swapIndex]) < 0)
                {
                    Swap(node, nodes[swapIndex]);
                }
                else // right place = exit
                {
                    break;
                }
            }

            else // no more childs = exit
            {
                break;
            }
        }
    }
Beispiel #2
0
    private void SortUp(aNode node)
    {
        int parentIndex = (node.HeapIndex - 1) / 2;

        while (true)
        {
            aNode parentNode = nodes[parentIndex];
            if (node.CompareTo(parentNode) > 0)
            {
                Swap(node, parentNode);
            }
            else
            {
                break;
            }
        }
    }