Exemplo n.º 1
0
        public void Merge(BinomialQueue <T> other)
        {
            int maxIndex = 0;

            if (other != null)
            {
                maxIndex = Math.Max(k, other.k);
                Node lastNode = null;
                for (int i = 0; i < maxIndex; i++)
                {
                    var    node1 = queue[i];
                    var    node2 = other.queue[i];
                    Node[] nodes = MergeTree(node1, node2);
                    if (nodes[0] == null)
                    {
                        nodes[0] = lastNode;
                    }
                    else
                    {
                        nodes = MergeTree(nodes[0], lastNode);
                    }
                    lastNode = nodes[1];
                    queue[i] = nodes[0];
                    CheckMin(queue[i]);
                }
                this.queue[maxIndex] = lastNode;
                maxIndex             = lastNode == null? maxIndex: maxIndex + 1;
                this.k = maxIndex;
                CheckMin(lastNode);
            }
        }
Exemplo n.º 2
0
        static void BinomialQueueTest()
        {
            int[] datas = { 13, 23, 51, 24, 65, 12, 21, 24, 65, 14, 26, 16, 18 };
            BinomialQueue <int> bQueue = new BinomialQueue <int>();

            foreach (var data in datas)
            {
                bQueue.Add(data);
            }
            bQueue.DeleteMin();
        }
Exemplo n.º 3
0
 public void Add(T value)
 {
     if (IsEmpty())
     {
         Node node = new Node(value);
         k        = 1;
         queue[0] = node;
         min      = value;
     }
     else
     {
         BinomialQueue <T> tempQueue = new BinomialQueue <T>(capacity);
         tempQueue.Add(value);
         this.Merge(tempQueue);
     }
 }
Exemplo n.º 4
0
        public void DeleteMin()
        {
            if (IsEmpty())
            {
                throw new Exception("is Empty");
            }
            int  index = -1;
            Node node  = null;

            for (int i = 0; i < k; i++)
            {
                node = queue[i];
                if (node != null)
                {
                    if (node.Value.CompareTo(min) <= 0)
                    {
                        index = i;
                        break;
                    }
                }
            }
            //remove Bk
            queue[index] = null;
            //Build B0-Bk-1
            BinomialQueue <T> other = new BinomialQueue <T>();
            int num     = 0;
            var sibling = node.FirstChild;

            while (sibling != null)
            {
                other.AddNode(sibling, num);
                var temp = sibling.Sibling;
                sibling.Sibling = null;
                sibling         = temp;
                num++;
            }
            //merge
            this.Merge(other);
        }