extern void LSQ_DeleteElement(LSQ_HandleT handle, LSQ_IntegerIndexT key) 
{
	AVLTreeT *tree = (AVLTreeT *)handle;
	TreeNodeT *node = NULL, *parent = NULL;
	IteratorT * iter = (IteratorT *)LSQ_GetElementByIndex(handle, key);
    int new_key;
    if (!LSQ_IsIteratorDereferencable(iter))
        return;

    parent = iter->node->parent;
    if (iter->node->l_child == NULL && iter->node->r_child == NULL)
        replaceNode(tree, iter->node, NULL);
    else if (iter->node->l_child != NULL && iter->node->r_child != NULL)
    {
        node = successor(iter->node);
        new_key = node->key;
        iter->node->value = node->value;
        LSQ_DeleteElement(handle, node->key);
        iter->node->key = new_key;
        return;
    }
    else if (iter->node->l_child != NULL)
        replaceNode(tree, iter->node, iter->node->l_child);
    else if (iter->node->r_child != NULL)
        replaceNode(tree, iter->node, iter->node->r_child);
    free(iter->node);
    tree->size--;
	restoreBalance(tree, parent, BT_AFTER_DELETE);
}
Exemple #2
0
/* Test moving on list with GetElementByIndex */
void test7(void)
{
    int i, j;
    srand ( time(NULL) );

//    print_LSQ(handleAll);
    printf("Testing GetElementByIndex");
    for(i = 0; i < MAX_TEST_ATTEMPTS; i++)
    {
        j = rand() % LSQ_GetSize(handleAll);
//        printf("\ni = %d \t j = %d \t size = %d", i , j, LSQ_GetSize(handleAll));
        LSQ_IteratorT iter = LSQ_GetElementByIndex(handleAll, j);
        assert( iter != NULL);
//        printf("\nFound = %d \t Expecting = %d \t", *LSQ_DereferenceIterator(iter), all[j]);
        assert( *LSQ_DereferenceIterator(iter) == all[j]);
        assert( *LSQ_DereferenceIterator(iter) != MAX_ELEMENT_VALUE + 1);

        LSQ_DestroyIterator(iter);
    }
    printf("\t\t ---\t OK \n");

}
Exemple #3
0
/* Функция, возвращающая итератор, ссылающийся на последний элемент контейнера */
LSQ_IteratorT LSQ_GetPastRearElement(LSQ_HandleT handle){
	if(handle == NULL)
		return(LSQ_HandleInvalid);
	return(LSQ_GetElementByIndex(handle, SEQ(handle)->size));
}
Exemple #4
0
/* Функция, возвращающая итератор, ссылающийся на первый элемент контейнера */
LSQ_IteratorT LSQ_GetFrontElement(LSQ_HandleT handle){
	return(LSQ_GetElementByIndex(handle, 0));
}
extern void LSQ_InsertFrontElement(LSQ_HandleT handle, LSQ_BaseTypeT element)
{
    LSQ_IteratorT iterator = LSQ_GetElementByIndex(handle, 0);
    LSQ_InsertElementBeforeGiven(iterator, element);
    LSQ_DestroyIterator(iterator);
}
extern LSQ_IteratorT LSQ_GetPastRearElement(LSQ_HandleT handle)
{
	return IS_HANDLE_INVALID(handle) ? LSQ_HandleInvalid : LSQ_GetElementByIndex(handle, ((ArrayDataT *)handle)->logical_size);
}
Exemple #7
0
LSQ_IteratorT LSQ_GetPastRearElement(LSQ_HandleT handle)
{
    return LSQ_GetElementByIndex(handle, LSQ_GetSize(handle));
}
Exemple #8
0
void LSQ_DeleteRearElement(LSQ_HandleT handle)
{
    _iterator *iter = LSQ_GetElementByIndex(handle, LSQ_GetSize(handle) - 1);
    LSQ_DeleteGivenElement(iter);
    LSQ_DestroyIterator(iter);
}