Пример #1
0
/*
 * Deletes the leftmost node in the subtree.  (Note that "deletes" means "removes from the tree." No memory
 * delete operation is actually performed.)
 *
 * Parameters:
 * - ptn = Pointer to root of subtree to have its leftmost node deleted.
 *
 * Returns:
 * Pointer to root of subtree after having the leftmost node deleted.
 *
 * N.B.:
 * This function is recursive; however, the nature of the tree guarantees that the stack space consumed
 * by its stack frames will be O(log n).
 */
static PRBTREENODE delete_min(PRBTREENODE ptn)
{
    if (!(ptn->ptnLeft))
        return rbtNodeRight(ptn);
    if (!rbtIsRed(ptn->ptnLeft) && !rbtIsRed(ptn->ptnLeft->ptnLeft))
        ptn = move_red_left(ptn);
    ptn->ptnLeft = delete_min(ptn->ptnLeft);
    return fix_up(ptn);
}
Пример #2
0
int inserir_heap(HEAP_ESTATICA *heap, NO *no) {
  if (!cheia_heap(heap)) {
    heap->fim++;
    heap->vetor[heap->fim] = no;
    fix_up(heap);
    return 1;
  }

  return 0;
}
Пример #3
0
//Starts at the bottom of the tree and ensures the tree continues to be a min heap fixing when needed
void Heap::fix_up(const int& t) {
	// at the top of the tree
	if (t == 1)
		return;

	// if current is < its parent
	if (tree[t]->weight < tree[t / 2]->weight) {
		swap(t, t / 2);
		fix_up(t / 2);
	}
}
Пример #4
0
//Puts a node a the end of the tree
void Heap::enqueue(HNode* t) {
	count++;
	// check to see if we need to expand the vector
	if (position == tree.size() - 1)
		tree.resize(tree.size() * 2);

	// insert at the next position
	tree[++position] = t;

	// fix up to the root (or until we dont need to swap)
	fix_up(position);
	// done
}
Пример #5
0
/*
 * Inserts a new node under the current subtree.  An O(log n) operation.
 *
 * Parameters:
 * - ptree = Pointer to the tree head structure, containing the compare function.
 * - ptnCurrent = Pointer to the current subtree we're inserting into.
 * - ptnNew = Pointer to the new tree node to be inserted.  This node must have been initialized with
 *            the rbtInitNode macro to contain NULL left and right pointers and be red.  It is
 *            assumed that the node's key does NOT already exist in the tree.
 * - keyNew = Tree key for the new node.
 *
 * Returns:
 * The pointer to the new subtree after the insertion is performed.
 *
 * N.B.:
 * This function is recursive; however, the nature of the tree guarantees that the stack space consumed
 * by its stack frames will be O(log n).
 */
static PRBTREENODE insert_under(PRBTREE ptree, PRBTREENODE ptnCurrent, PRBTREENODE ptnNew, TREEKEY keyNew)
{
    register int cmp;  /* compare result */
    register TREEKEY keyCurrent;

    if (!ptnCurrent)
        return ptnNew;  /* degenerate case */
    keyCurrent = (*(ptree->pfnGetTreeKey))((*(ptree->pfnGetFromNodePtr))(ptnCurrent));
    cmp = (*(ptree->pfnTreeCompare))(keyNew, keyCurrent);
    //ASSERT(cmp != 0);
    if (cmp < 0)
        ptnCurrent->ptnLeft = insert_under(ptree, ptnCurrent->ptnLeft, ptnNew, keyNew);
    else
        rbtSetNodeRight(ptnCurrent, insert_under(ptree, rbtNodeRight(ptnCurrent), ptnNew, keyNew));
    return fix_up(ptnCurrent);
}
Пример #6
0
/*
 * Deletes the node in the subtree having an arbitrary key. (Note that "deletes" means "removes from the tree."
 * No memory delete operation is actually performed.) An O(log n) operation.
 *
 * Parameters:
 * - ptree = Pointer to the tree head structure, containing the compare function.
 * - ptnCurrent = Pointer to the root of the current subtree we're deleting from.
 * - key = Key value we're deleting from the tree.  It is assumed that this key value exists in the subtree.
 *
 * Returns:
 * Pointer to the root of the subtree after the node has been deleted.
 *
 * N.B.:
 * This function is recursive; however, the nature of the tree guarantees that the stack space consumed
 * by its stack frames (and those of delete_min, where we call it) will be O(log n).
 */
static PRBTREENODE delete_from_under(PRBTREE ptree, PRBTREENODE ptnCurrent, TREEKEY key)
{
    register TREEKEY keyCurrent = (*(ptree->pfnGetTreeKey))((*(ptree->pfnGetFromNodePtr))(ptnCurrent));
    register int cmp = (*(ptree->pfnTreeCompare))(key, keyCurrent);
    if (cmp < 0)
    {
        /* hunt down the left subtree */
        if (!rbtIsRed(ptnCurrent->ptnLeft) && !rbtIsRed(ptnCurrent->ptnLeft->ptnLeft))
            ptnCurrent = move_red_left(ptnCurrent);
        ptnCurrent->ptnLeft = delete_from_under(ptree, ptnCurrent->ptnLeft, key);
    }
    else
    {
        if (rbtIsRed(ptnCurrent->ptnLeft))
        {
            ptnCurrent = rotate_right(ptnCurrent);
            keyCurrent = (*(ptree->pfnGetTreeKey))((*(ptree->pfnGetFromNodePtr))(ptnCurrent));
            cmp = (*(ptree->pfnTreeCompare))(key, keyCurrent);
        }
        if ((cmp == 0) && !rbtNodeRight(ptnCurrent))
            return ptnCurrent->ptnLeft;  /* degenerate case */
        if (   !rbtIsRed(rbtNodeRight(ptnCurrent))
                && (!rbtNodeRight(ptnCurrent) || !rbtIsRed(rbtNodeRight(ptnCurrent)->ptnLeft)))
        {
            ptnCurrent = move_red_right(ptnCurrent);
            keyCurrent = (*(ptree->pfnGetTreeKey))((*(ptree->pfnGetFromNodePtr))(ptnCurrent));
            cmp = (*(ptree->pfnTreeCompare))(key, keyCurrent);
        }
        if (cmp == 0)
        {
            /*
             * Here we find the minimum node in the right subtree, unlink it, and link it into place in place of
             * ptnCurrent (i.e. node pointed to by ptnCurrent should no longer be referenced).  We inherit the
             * child pointers and color of ptnCurrent (minus the reference from the right-hand tree where applicable).
             */
            register PRBTREENODE ptnMin = find_min(rbtNodeRight(ptnCurrent));
            rbtSetNodeRight(ptnMin, delete_min(rbtNodeRight(ptnCurrent)));
            ptnMin->ptnLeft = ptnCurrent->ptnLeft;
            rbtSetNodeColor(ptnMin, rbtNodeColor(ptnCurrent));
            ptnCurrent = ptnMin;
        }
        else /* hunt down the right subtree */
            rbtSetNodeRight(ptnCurrent, delete_from_under(ptree, rbtNodeRight(ptnCurrent), key));
    }
    return fix_up(ptnCurrent);
}
Пример #7
0
// unordered array;
void pq_insert(Item v)
{
    pq[++N] = v;
    fix_up(pq, N);
}