Exemplo n.º 1
0
        public IPair <TKey, TValue> Push(TKey key, TValue value)
        {
            var node = new PairNode <TKey, TValue>
            {
                Key   = key,
                Value = value
            };

            _heap.Add(node);

            var i    = _heap.Count - 1;
            var flag = true;

            if (Type == HeapType.MaxHeap)
            {
                flag = false;
            }

            while (i > 0)
            {
                if ((_heap[i].CompareTo(_heap[(i - 1) / 2]) > 0) ^ flag)
                {
                    var temp = _heap[i];
                    _heap[i]           = _heap[(i - 1) / 2];
                    _heap[(i - 1) / 2] = temp;
                    i = (i - 1) / 2;
                }
                else
                {
                    break;
                }
            }

            return(node);
        }
Exemplo n.º 2
0
        public IPair <TKey, TValue> Push(TKey key, TValue value)
        {
            var node = new PairNode <TKey, TValue>
            {
                Key   = key,
                Value = value
            };

            return(Push(node));
        }
Exemplo n.º 3
0
        public IPair <TKey, TValue> Push(TKey key, TValue item)
        {
            var node = new PairNode <TKey, TValue>
            {
                Key   = key,
                Value = item
            };

            _heap.Add(node);

            var lastIndex = _heap.Count - 1;

            if (_heap[lastIndex].CompareTo(_heap[0]) < 0)
            {
                Swap(0, lastIndex);
            }

            BottomUpHeapfy();

            return(node);
        }