internal void AddChild(FibonacciNode <TK> node)
 {
     if (node == null)
     {
         throw new ArgumentNullException(nameof(node));
     }
     if (node.Parent == this)
     {
         throw new InvalidOperationException($"The node is already a child of this node.");
     }
     if (Child == null)
     {
         Child      = node;
         node.Left  = node;
         node.Right = node;
     }
     else if (Child.Right == Child)
     {
         Child.Right = node;
         Child.Left  = node;
         node.Right  = Child;
         node.Left   = Child;
     }
     else
     {
         Child.Right.Left = node;
         node.Right       = Child.Right;
         Child.Right      = node;
         node.Left        = Child;
     }
     node.Parent = this;
     Degree++;
 }
 internal void DeleteChild(FibonacciNode <TK> node)
 {
     if (node == null)
     {
         throw new ArgumentNullException(nameof(node));
     }
     if (node.Parent != this)
     {
         throw new InvalidOperationException($"{nameof(node)} is not a child node");
     }
     if (Child.Left == Child && Child == node)
     {
         Child = null;
     }
     else
     {
         node.Left.Right = node.Right;
         node.Right.Left = node.Left;
         if (Child == node)
         {
             Child = node.Right;
         }
     }
     node.Parent = null;
     node.Left   = null;
     node.Right  = null;
     Degree--;
 }
Esempio n. 3
0
 private void Cut(FibonacciNode <TK> x, FibonacciNode <TK> y)
 {
     y.DeleteChild(x);
     AddToRootList(x);
     x.Parent = null;
     x.Mark   = false;
 }
Esempio n. 4
0
 public void Delete(FibonacciNode <TK> x)
 {
     x.MinimumCausedByDeletion = true;
     DecreaseKey(x, x.Key);
     x.MinimumCausedByDeletion = false;
     ExtractMin();
 }
Esempio n. 5
0
        private void Consolidate()
        {
            FibonacciNode <TK>[] a = new FibonacciNode <TK> [Count + 1];
            for (int i = 0; i <= Count; i++)
            {
                a[i] = null;
            }
            //Assert Min!=null;
            var w        = Min;
            var initRoot = Min;

            do
            {
                var x = w;
                w = w.Right;
                Console.WriteLine($"Consolidating root {w.Key}, Degree={w.Degree}");
                var d = x.Degree;
                while (a[d] != null)
                {
                    var y = a[d];
                    if (x.CompareTo(y) > 0)
                    {
                        var t = x; x = y; y = t;
                    }
                    Console.WriteLine($"Linking node {y.Key} to {x.Key}");
                    Link(y, x);
                    a[d] = null;
                    d++;
                }
                a[d] = x;
            } while (w != initRoot);


            Min = null;
            for (int i = 0; i <= Count; i++)
            {
                if (a[i] != null)
                {
                    if (Min == null)
                    {
                        AddToRootList(a[i]);
                        Min = a[i];
                    }
                    else
                    {
                        AddToRootList(a[i]);
                        if (a[i].CompareTo(Min) < 0)
                        {
                            Min = a[i];
                        }
                    }
                }
            }
        }
Esempio n. 6
0
 public void Insert(FibonacciNode <TK> node)
 {
     if (Min == null)
     {
         AddToRootList(node);
         Min = node;
     }
     else
     {
         AddToRootList(node);
         if (node.CompareTo(Min) < 0)
         {
             Min = node;
         }
     }
     Count++;
 }
Esempio n. 7
0
        private void CascadingCut(FibonacciNode <TK> y)
        {
            var z = y.Parent;

            if (z != null)
            {
                if (!y.Mark)
                {
                    y.Mark = true;
                }
                else
                {
                    Cut(y, z);
                    CascadingCut(z);
                }
            }
        }
Esempio n. 8
0
        public void DecreaseKey(FibonacciNode <TK> x, TK k)
        {
            if (k.CompareTo(x.Key) > 0)
            {
                throw new InvalidOperationException($"new key {k} is greater than the current key {x.Key}");
            }
            x.Key = k;
            var y = x.Parent;

            if (y != null && x.CompareTo(y) < 0)
            {
                Cut(x, y);
                CascadingCut(y);
            }
            if (x.CompareTo(Min) < 0)
            {
                Min = x;
            }
        }
Esempio n. 9
0
 private void AddToRootList(FibonacciNode <TK> node)
 {
     if (node == null)
     {
         throw new ArgumentNullException(nameof(node));
     }
     if (Min == null)
     {
         Min       = node;
         node.Left = node.Right = node;
     }
     else
     {
         Min.Left.Right = node;
         node.Left      = Min.Left;
         Min.Left       = node;
         node.Right     = Min;
     }
     node.Parent = null;
 }
Esempio n. 10
0
 private void RemoveFromRootList(FibonacciNode <TK> node)
 {
     if (node == null)
     {
         throw new ArgumentNullException(nameof(node));
     }
     if (Min.Left == Min)
     {
         if (node != Min)
         {
             throw new Exception("The node you are removing from the root list is something wrong.");
         }
         Min = null;
     }
     else
     {
         node.Left.Right = node.Right;
         node.Right.Left = node.Left;
     }
     node.Parent = null;
 }
Esempio n. 11
0
 public int CompareTo(FibonacciNode <TK> node)
 {
     if (node == null)
     {
         throw new ArgumentNullException(nameof(node));
     }
     if (MinimumCausedByDeletion)
     {
         if (node.MinimumCausedByDeletion)
         {
             throw new Exception($"Both of the operators are minimum. Somthing wrong in the code.");
         }
         else
         {
             return(int.MinValue);
         }
     }
     else if (node.MinimumCausedByDeletion)
     {
         return(int.MaxValue);
     }
     return(Key.CompareTo(node.Key));
 }
Esempio n. 12
0
 private void Link(FibonacciNode <TK> y, FibonacciNode <TK> x)
 {
     RemoveFromRootList(y);
     x.AddChild(y);
     y.Mark = false;
 }