示例#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;
        }