Esempio n. 1
0
 public HeapNode()
 {
     value    = default(T);
     right    = null;
     left     = null;
     height   = 1;
     complete = true;
     priority = default(T);
 }
Esempio n. 2
0
        HeapNode <T> InsertSub(HeapNode <T> root, HeapNode <T> new_node)
        {
            if (root == null)
            {
                countT++;
                return(new_node);
            }
            else
            {
                if (!isComplete(root.left) || root.complete)
                {
                    //Ir a la izquierda
                    root.left   = InsertSub(root.left, new_node);
                    root.height = Max(Height(root.left), Height(root.right)) + 1;

                    //Verifica si el subárbol ya está completo viendo si su factor de equilibrio es cero
                    int balance = GetBalance(root);
                    if (balance == 0)
                    {
                        root.complete = true;
                    }
                    else
                    {
                        root.complete = false;
                    }

                    if (new_node.priority.CompareTo(root.priority) == -1)
                    {
                        root = SwitchLeft(root);
                    }
                }
                else if (!isComplete(root.right) || root.complete == false && root.left.complete)
                {
                    //Ir a la derecha
                    root.right  = InsertSub(root.right, new_node);
                    root.height = Max(Height(root.left), Height(root.right)) + 1;
                    //Verifica si el subárbol ya está completo viendo si ambos hijos ya están completos
                    if (root.left.complete && root.right.complete)
                    {
                        root.complete = true;
                    }
                    else
                    {
                        root.complete = false;
                    }

                    if (new_node.priority.CompareTo(root.priority) == -1)
                    {
                        root = SwitchRight(root);
                    }
                }
            }

            return(root);
        }
Esempio n. 3
0
 int GetBalance(HeapNode <T> N)
 {
     if (N == null)
     {
         return(0);
     }
     else
     {
         return(Height(N.left) - Height(N.right));
     }
 }
Esempio n. 4
0
 int Height(HeapNode <T> root)
 {
     if (root == null)
     {
         return(0);
     }
     else
     {
         return(root.height);
     }
 }
Esempio n. 5
0
 bool isComplete(HeapNode <T> N)
 {
     if (N == null)
     {
         return(false);
     }
     else
     {
         return(N.complete);
     }
 }
Esempio n. 6
0
        public void HeapInsert(T value, T priority)
        {
            HeapNode <T> new_node = new HeapNode <T>();

            new_node.value    = value;
            new_node.priority = priority;
            if (root == null)
            {
                root = new_node;
                countT++;
            }
            else
            {
                root = InsertSub(root, new_node);
            }
        }
Esempio n. 7
0
        HeapNode <T> SwitchLeft(HeapNode <T> X)
        {
            HeapNode <T> Y     = X.left;
            HeapNode <T> temp  = Y.right;
            bool         temp2 = X.complete;

            X.left  = Y.left;
            Y.right = X.right;
            X.right = temp;
            Y.left  = X;

            X.height   = Max(Height(X.left), Height(X.right)) + 1;
            Y.height   = Max(Height(Y.left), Height(Y.right)) + 1;
            X.complete = Y.complete;
            Y.complete = temp2;

            return(Y);
        }
Esempio n. 8
0
 HeapNode <T> Reorder(HeapNode <T> node)
 {
     if (node.left != null && node.right != null)//Si tiene sus dos hijos
     {
         if (node.priority.CompareTo(node.left.priority) == 1 && node.priority.CompareTo(node.right.priority) == 1)
         {
             //Si es mayor que sus dos hijos, ver cuál tiene la prioridad mayor
             if (node.left.priority.CompareTo(node.right.priority) == -1)
             {
                 //Ir a la izquierda
                 node      = SwitchLeft(node);
                 node.left = Reorder(node.left);
             }
             else
             {
                 //Ir a la derecha
                 node       = SwitchRight(node);
                 node.right = Reorder(node.right);
             }
         }
         else if (node.priority.CompareTo(node.left.priority) == 1)
         {
             //Si solo es mayor que su hijo izquierdo, ir a la izquierda
             node      = SwitchLeft(node);
             node.left = Reorder(node.left);
         }
         else if (node.priority.CompareTo(node.right.priority) == 1)
         {
             //Si solo es mayor que su hijo derecho, ir a la derecha
             node       = SwitchRight(node);
             node.right = Reorder(node.right);
         }
     }
     else if (node.left != null)
     {
         //Si solo tiene su hijo izquierdo
         if (node.priority.CompareTo(node.left.priority) == 1)
         {
             node = SwitchLeft(node);
         }
     }
     return(node);
 }
Esempio n. 9
0
        public HeapNode <T> HeapDelete()
        {
            HeapNode <T> prevroot = root;//Variable que guardará la raíz a eliminar

            if (root.right == null && root.left == null)
            {
                prevroot = root;
                root     = null;
            }
            else
            {
                HeapNode <T> last = SearchLast(root);
                root           = last;
                last.right     = prevroot.right;
                last.left      = prevroot.left;
                last.height    = prevroot.height;
                last.complete  = prevroot.complete;
                prevroot.right = null;
                prevroot.left  = null;
                root           = Reorder(root);
            }
            countT--;
            return(prevroot);
        }
Esempio n. 10
0
        HeapNode <T> SearchLast(HeapNode <T> node)
        {
            HeapNode <T> lastnode = node;

            if (node.right != null && node.left != null)
            {
                if (!isComplete(node.left))
                {
                    lastnode = SearchLast(node.left);

                    int balance = GetBalance(node);
                    if (balance == 0)
                    {
                        node.complete = true;
                    }
                    else
                    {
                        node.complete = false;
                    }
                }
                else if (!isComplete(node.right))
                {
                    lastnode = SearchLast(node.right);

                    int balance = GetBalance(node);
                    if (node.left.complete && node.right.complete && balance == 0)
                    {
                        node.complete = true;
                    }
                    else
                    {
                        node.complete = false;
                    }
                }
                else if (node.complete == false && node.left.complete)
                {
                    lastnode = SearchLast(node.left);

                    int balance = GetBalance(node);
                    if (balance == 0)
                    {
                        node.complete = true;
                    }
                    else
                    {
                        node.complete = false;
                    }
                }
                else if (node.complete)
                {
                    if (node.right.right == null && node.right.left == null)
                    {
                        lastnode      = node.right;
                        node.right    = null;
                        node.complete = false;
                        return(lastnode);
                    }
                    else
                    {
                        lastnode = SearchLast(node.right);

                        int balance = GetBalance(node);
                        if (node.left.complete && node.right.complete && balance == 0)
                        {
                            node.complete = true;
                        }
                        else
                        {
                            node.complete = false;
                        }
                    }
                }
            }
            else if (node.left != null)
            {
                lastnode      = node.left;
                node.left     = null;
                node.complete = true;
                node.height   = 1;
                return(lastnode);
            }
            node.height = Max(Height(node.left), Height(node.right)) + 1;
            return(lastnode);
        }
Esempio n. 11
0
 public Heap()
 {
     root   = null;
     countT = 0;
 }