Пример #1
0
        private void RemoveRoot(Node root, Node prev_root)
        {
            if (root == head)
            {
                head = root.sibling;
            }
            else
            {
                prev_root.sibling = root.sibling;
            }

            Node new_head = null;
            Node child    = root.child;

            while (child != null)
            {
                Node other_child = child.sibling;
                child.sibling = new_head;
                child.parent  = null;
                new_head      = child;
                child         = other_child;
            }

            BinomialHeap new_heap = new BinomialHeap(new_head);

            head = UnionTree(new_heap);
        }
Пример #2
0
        public void Add(int key)
        {
            Node         node     = new Node(key);
            BinomialHeap tempHeap = new BinomialHeap(node);

            head = UnionTree(tempHeap);
        }
Пример #3
0
        private Node Merge(BinomialHeap Heap1, BinomialHeap Heap2)
        {
            if (Heap1.head == null)
            {
                return(Heap2.head);
            }
            else if (Heap2.head == null)
            {
                return(Heap1.head);
            }
            else
            {
                Node new_head;
                Node heap1_next = Heap1.head;
                Node heap2_next = Heap2.head;

                if (Heap1.head.degree <= Heap2.head.degree)
                {
                    new_head   = Heap1.head;
                    heap1_next = heap1_next.sibling;
                }
                else
                {
                    new_head   = Heap2.head;
                    heap2_next = heap2_next.sibling;
                }

                Node end = new_head;
                while (heap1_next != null && heap2_next != null)
                {
                    if (heap1_next.degree <= heap2_next.degree)
                    {
                        end.sibling = heap1_next;
                        heap1_next  = heap1_next.sibling;
                    }
                    else
                    {
                        end.sibling = heap2_next;
                        heap2_next  = heap2_next.sibling;
                    }
                    end = end.sibling;
                }

                if (heap1_next != null)
                {
                    end.sibling = heap1_next;
                }
                else
                {
                    end.sibling = heap2_next;
                }

                return(new_head);
            }
        }
Пример #4
0
        private Node UnionTree(BinomialHeap Heap)
        {
            Node new_head = Merge(this, Heap);

            head      = null;
            Heap.head = null;
            if (new_head == null)
            {
                return(null);
            }

            Node prev    = null;
            Node current = new_head;
            Node next    = new_head.sibling;

            while (next != null)
            {
                if (current.degree != next.degree ||
                    (next.sibling != null && next.sibling.degree == current.degree))
                {
                    prev    = current;
                    current = next;
                }
                else if (current.key < next.key)
                {
                    current.sibling = next.sibling;
                    BuildTree(current, next);
                }
                else
                {
                    if (prev == null)
                    {
                        new_head = next;
                    }
                    else
                    {
                        prev.sibling = next;
                    }

                    BuildTree(next, current);

                    current = next;
                }
                next = current.sibling;
            }
            return(new_head);
        }
Пример #5
0
        static void Main(string[] args)
        {
            BinomialHeap heap  = new BinomialHeap();
            bool         check = false;

            do
            {
                Console.WriteLine("Выберите действие с биноминальной кучей:\n1 - добавить элемент\n2 - удалить минимальный элемент\n3 - вывод кучи\n4 - выход ");
                string p = Console.ReadLine();
                switch (p)
                {
                case "1":
                    Console.WriteLine("Какой элемент добавить?");
                    heap.Add(Convert.ToInt32(Console.ReadLine()));
                    check = false;
                    break;

                case "2":
                    heap.DeleteMin();
                    heap.Print();
                    check = false;
                    break;

                case "3":
                    heap.Print();
                    check = false;
                    break;

                case "4":
                    check = true;
                    break;

                default:
                    Console.WriteLine("Неверный ввод.Попробуйте ещё раз.");
                    break;
                }
            } while (check == false);
        }