コード例 #1
0
ファイル: Program.cs プロジェクト: Miltt/Console
        public static Node Create(int key)
        {
            Node node = new Node();
            node.key = key;

            return node;
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: Miltt/Console
 // Linking nodes      
 public void Link(Node child, Node parent)
 {
     child.parent = parent;
     child.sibling = parent.child;
     parent.child = child;
     parent.degree = parent.degree + 1;
 }
コード例 #3
0
ファイル: Program.cs プロジェクト: Miltt/Console
        // Insert node       
        public Node Insert(Node h, Node insertNode)
        {                        
            Node node = Init();
            node = insertNode;

            h = Union(h, node);

            return h;
        }
コード例 #4
0
ファイル: Program.cs プロジェクト: Miltt/Console
        // Union nodes       
        public Node Union(Node h1, Node h2)
        {
            Node node = Init();
            node = Merge(h1, h2);

            if (node == null)
            {
                return node;
            }

            Node h = node;
            Node prevH = null;
            Node nextH = h.sibling;                       
            
            while (nextH != null)
            {
                if ((h.degree != nextH.degree) || ((nextH.sibling != null) && (nextH.sibling).degree == h.degree))
                {
                    prevH = h;
                    h = nextH;
                }
                else
                {
                    if (h.key <= nextH.key)
                    {
                        h.sibling = nextH.sibling;

                        Link(nextH, h);
                    }
                    else
                    {
                        if (prevH == null)
                        {
                            node = nextH;
                        }
                        else
                        {
                            prevH.sibling = nextH;
                        }

                        Link(h, nextH);

                        h = nextH;
                    }
                }

                nextH = h.sibling;
            }

            return node;
        }
コード例 #5
0
ファイル: Program.cs プロジェクト: Miltt/Console
 public BinomialHeap()
 {
     heap = Init();
     heapR = Init();
 }
コード例 #6
0
ファイル: Program.cs プロジェクト: Miltt/Console
        // Delete Nodes
        public void Delete(Node h, int key)
        {         
            if (h == null)
            {
                Console.WriteLine("Heap is empty");
                return;
            }

            DecreaseKey(h, key, int.MinValue);
            ExtractMinimum(h);
        }
コード例 #7
0
ファイル: Program.cs プロジェクト: Miltt/Console
        // Decrease key of a node        
        public void DecreaseKey(Node h, int curKey, int newKey)
        {
            if (newKey > curKey)
            {
                Console.WriteLine("The new key cannot exceed the current");
                return;               
            }           

            Node foundNode = Search(h, curKey);

            if (foundNode == null)
            {
                Console.WriteLine("Node not found");
                return;               
            }                                         

            foundNode.key = newKey;
            Node a = foundNode;
            Node b = foundNode.parent;

            while (b != null && a.key < b.key)
            {
                int temp = a.key;
                a.key = b.key;
                b.key = temp;

                a = b;
                b = b.parent;
            }            
        }
コード例 #8
0
ファイル: Program.cs プロジェクト: Miltt/Console
        // Search
        private Node Search(Node h, int key)
        {            
            Node result = Init();

            if (h.key == key)
            {
                result = h;
                return result;
            }

            if (h.child != null && result == null)
            {
                result = Search(h.child, key);
            }

            if (h.sibling != null && result == null)
            {
                result = Search(h.sibling, key);
            }

            return result;
        }
コード例 #9
0
ファイル: Program.cs プロジェクト: Miltt/Console
 // Reverse list        
 private void Reverse(Node h)
 {
     if (h.sibling != null)
     {
         Reverse(h.sibling);
         (h.sibling).sibling = h;
     }
     else
     {
         heapR = h;
     }
 }
コード例 #10
0
ファイル: Program.cs プロジェクト: Miltt/Console
        // Extract minimum        
        public Node ExtractMinimum(Node h)
        {
            if (h == null)
            {
                Console.WriteLine("Heap is empty");
                return h;
            }

            heapR = Init();
            Node a = Init();
            Node node = h;            
            Node temp = node;

            int min = node.key;            

            while (temp.sibling != null)
            {
                if ((temp.sibling).key < min)
                {
                    min = (temp.sibling).key;
                    a = temp;
                    node = temp.sibling;
                }

                temp = temp.sibling;
            }

            if (a == null && node.sibling == null)
            {
                h = null;
            }
            else if (a == null)
            {
                h = node.sibling;
            }
            else if (a.sibling == null)
            {
                a = null;
            }
            else
            {
                a.sibling = node.sibling;
            }

            if (node.child != null)
            {
                Reverse(node.child);
                (node.child).sibling = null;
            }

            heap = Union(h, heapR);

            return node;
        }
コード例 #11
0
ファイル: Program.cs プロジェクト: Miltt/Console
        // Merge nodes       
        public Node Merge(Node h1, Node h2)
        {
            Node node = Init();
            Node tmp = Init();            

            if (h1 != null)
            {
                if (h2 != null)
                {
                    if (h1.degree <= h2.degree)
                    {
                        node = h1;
                    }
                    else if (h1.degree > h2.degree)
                    {
                        node = h2;
                    }
                }
                else
                {
                    node = h1;
                }
            }
            else
            {
                node = h2;
            }

            while (h1 != null && h2 != null)
            {
                if (h1.degree < h2.degree)
                {
                    h1 = h1.sibling;
                }
                else if (h1.degree == h2.degree)
                {
                    tmp = h1.sibling;
                    h1.sibling = h2;
                    h1 = tmp;
                }
                else
                {
                    tmp = h2.sibling;
                    h2.sibling = h1;
                    h2 = tmp;
                }
            }

            return node;
        }