/// <summary>Adds an item to the heap.</summary>
            public void Insert(TValue value)
            {
                // Create the entry based on the provided key and value
                // Add the item to the list, making sure to keep track of where it was added.
                Items.Add(value);
                var pos = Items.Count - 1;

                // If the new item is the only item, we're done.
                if (pos == 0)
                {
                    return;
                }

                // Otherwise, perform log(n) operations, walking up the tree, swapping
                // where necessary based on key values
                while (pos > 0)
                {
                    // Get the next position to check
                    var nextPos = (pos - 1) / 2;

                    // Extract the entry at the next position
                    var toCheck = Items[nextPos];

                    // Compare that entry to our new one.  If our entry has a smaller key, move it up.
                    // Otherwise, we're done.
                    if (value.CompareTo(toCheck) < 0)
                    {
                        Items[pos] = toCheck;
                        pos        = nextPos;
                    }
                    else
                    {
                        break;
                    }
                }

                // Make sure we put this entry back in, just in case
                Items[pos] = value;
            }
Пример #2
0
 public int CompareTo(Node other)
 {
     return(cost.CompareTo(other.cost));
 }
Пример #3
0
 public int CompareTo(Node other) => cost.CompareTo(other.cost);