Exemple #1
0
    public HeapNode <T> addHeapNode(T node)
    {
        if (root == null)
        {
            root = new HeapNode <T>(node, null);
            return(root);
        }
        else
        {
            Queue <HeapNode <T> > q = new Queue <HeapNode <T> >();
            q.Enqueue(root);
            HeapNode <T> curr = root;
            while (curr != null)
            {
                curr = q.Dequeue();
                HeapNode <T> l = curr.getLeft();
                if (l == null)
                {
                    HeapNode <T> heapNode = new HeapNode <T>(node, curr);
                    curr.setLeft(heapNode);
                    root = heapNode.sortParent();
                    return(heapNode);
                }
                else
                {
                    q.Enqueue(l);
                }

                HeapNode <T> r = curr.getRight();
                if (r == null)
                {
                    HeapNode <T> heapNode = new HeapNode <T>(node, curr);
                    curr.setRight(heapNode);
                    root = heapNode.sortParent();
                    return(heapNode);
                }
                else
                {
                    q.Enqueue(r);
                }
            }
            return(curr);
        }
    }
Exemple #2
0
    /*
     * Checks the value of the parent. to be called when inserting a new value into the heap. Checks nodes above itself ONLY.
     * GOES BOTTOM->UP
     */
    public HeapNode <T> sortParent()
    {
        //this is the root case
        if (this.parent == null)
        {
            return(this);
        }

        if (parent.nodeComparer.CompareTo(node) > 0)
        {
            HeapNode <T> tempParent = parent.parent;
            HeapNode <T> tempLeft   = parent.left;
            HeapNode <T> tempRight  = parent.right;

            parent.left   = this.left;
            parent.right  = this.right;
            parent.parent = this;

            if (tempLeft != null && this.Equals(tempLeft))
            {
                this.left   = parent;
                this.right  = tempRight;
                this.parent = tempParent;

                if (this.right != null)
                {
                    this.right.parent = this;
                }

                //set new nodes below old parent such that the old parent is the lower nodes
                //new parent
                if (this.left.left != null)
                {
                    this.left.left.parent = this.left;
                }
                if (this.left.right != null)
                {
                    this.left.right.parent = this.left;
                }

                //sets the parent's parent left/right nodes to the new node
                if (parent != null)
                {
                    if (parent.left.Equals(this.left))
                    {
                        parent.left = this;
                    }
                    else if (parent.right.Equals(this.left))
                    {
                        parent.right = this;
                    }
                }
            }
            else if (tempRight != null && this.Equals(tempRight))
            {
                this.left   = tempLeft;
                this.right  = parent;
                this.parent = tempParent;

                if (this.left != null)
                {
                    this.left.parent = this;
                }

                //set new nodes below old parent such that the old parent is the lower nodes
                //new parent
                if (this.right.left != null)
                {
                    this.right.left.parent = this.right;
                }
                if (this.right.right != null)
                {
                    this.right.right.parent = this.right;
                }

                //sets the parent's parent left/right nodes to the new node
                if (parent != null)
                {
                    if (parent.left.Equals(this.right))
                    {
                        parent.left = this;
                    }
                    else if (parent.right.Equals(this.right))
                    {
                        parent.right = this;
                    }
                }
            }

            return(this.sortParent());
        }
        else
        {
            return(parent.sortParent());
        }
    }