Ejemplo n.º 1
0
rbnode * RBTree::find_insert_pos(rbnode * n, int v)
{
    if (n->value == v) {
        return n;
    }

    if (v < n->value) {
        if (n->lc != pNil) {
            return find_insert_pos(n->lc, v);
        } else {
            return n;
        }
    }

    if (v > n->value) {
        if (n->rc != pNil) {
            return find_insert_pos(n->rc, v);
        } else {
            return n;
        }
    }
}
Ejemplo n.º 2
0
void RBTree::insert(int v)
{
    if (root == pNil) {
        root = new_node(v);
        return;
    }

    rbnode * n = find_insert_pos(root, v);
    if (n->value == v) {
        return;
    } else {
        rbnode * c = new_node(v, RED);
        c->parent = n;
        if (v < n->value) {
            n->lc = c;
        } else {
            n->rc = c;
        }
        insert_fixup(c);
    }
}
Ejemplo n.º 3
0
int	list_index :: insert_ordered (list_element *new_ele)
{
	uint	pos;
	uint	new_size;
	uint	shift;
	int	ret_code = SUCCESS;

	if ( this->sort_ind )
	{

			// Strecth out the index, if it is not large enough

		new_size = this->num_in_index + 1;

		if ( new_size > this->index_size )
			ret_code = this->resize(new_size);

		if ( ret_code == SUCCESS )
		{
				// Find the position in the list to insert the
				//  element.

			pos = find_insert_pos(this->index, this->num_in_index,
			                      new_ele, this->compare);

				// If the insert position is the end of the
				//  list, just add this element to the end.

			if ( pos == this->num_in_index )
				this->index[pos] = new_ele;
			else
			{
					// Shift all of the elements from this
					//  position over to make space for the
					//  new element.

				shift = this->num_in_index - (pos + 1);

				memmove(this->index + pos + 1,
				        this->index + pos,
				        shift * sizeof(list_element *));

				this->index[pos] = new_ele;
			}

			this->num_in_index = new_size;
		}
	}
	else
	{
			// this index is not yet sorted; just add this element
			//  and then sort the index :)

		ret_code = this->add(new_ele);

		if ( ret_code == SUCCESS )
			ret_code = this->sort();
	}

	return	ret_code;
}