Beispiel #1
0
void rbDelete(root_ptr ts_root, tree_ptr node_z)
{
    tree_ptr node_y = node_z;
    COLOR y_original_color = node_y->color;

    tree_ptr node_x = NULL;
    if (node_z->left == NULL) {
        node_x = node_z->right;
        rbTransplant(ts_root, node_z, node_z->right);
    }
    else if (node_z->right == NULL) {
        node_x = node_z->left;
        rbTransplant(ts_root, node_z, node_z->left);
    } else {
        node_y = treeMinimum(node_z->right);
        y_original_color = node_y->color;
        node_x = node_y->right;
        if (node_y->parent == node_z)
            node_x->parent = node_y;
        else {
            rbTransplant(ts_root, node_y, node_y->right);
            node_y->right = node_z->right;
            node_y->right->parent = node_y;
        }

        rbTransplant(ts_root, node_z, node_y);
        node_y->left = node_z->left;
        node_y->left->parent = node_y;
        node_y->color = node_z->color;
    }

    if (y_original_color == BLACK)
        rbDeleteFixup(ts_root, node_x);
}
Beispiel #2
0
extern LSQ_IteratorT LSQ_GetFrontElement(LSQ_HandleT handle)
{
	AVLTreeT * tree = (AVLTreeT *)handle;
	if IS_HANDLE_INVALID(handle)
		return LSQ_HandleInvalid;
	return createIterator(handle, treeMinimum(tree->root));
}
Beispiel #3
0
void treeRemove(Elem* root, Elem* node) 
{
	if (node->left == NULL)
	{
		transplant(root, node, node->right);
	}
	else if (node->right == NULL)
	{
		transplant(root, node, node->left);
	}
	else
	{
		Elem* y = treeMinimum(node->right);
		if (y->parent != node)
		{
			transplant(root, y, y->right);
			y->right = node->right;
			y->right->parent = y;
		}

		transplant(root, node, y);
		y->left = node->left;
		y->left->parent = y;
	}
}
BRNode * treeSuccessor(BRNode * x) {
    if (x->right != NULL) {
        return treeMinimum(x->right);
    }
    BRNode * y = x->p;
    while (y != NULL && x == y->right) {
        x = y;
        y = y->p;
    }
    return y;
}
Beispiel #5
0
 _redblacktree::tree* _redblacktree::treeSuccessor(tree *x){
     tree * y = NULL;
     if (x->right != NULL) {
         return treeMinimum(x->right);
     }
     if (x != root)
         y = x->parent;
     while (y != NULL && x == y->right) {
         x = y;
         y = y->parent;
     }
     return x;
 }
Beispiel #6
0
extern void LSQ_AdvanceOneElement(LSQ_IteratorT iterator)
{
	IteratorT * iter = (IteratorT *)iterator;
	if (IS_HANDLE_INVALID(iterator) || (iter->tree->size == 0))
		return;
	if (LSQ_IsIteratorBeforeFirst(iterator)){
		iter->node = treeMinimum(iter->tree->root);	
		iter->state = IST_DEREFERENCABLE;
		return;
	}
	iter->node = successor(iter->node);
	if (iter->node == NULL)
		iter->state = IST_PAST_REAR;
}
Beispiel #7
0
static TreeNodeT * successor(TreeNodeT * node)
{
	TreeNodeT * parent = NULL;
	TreeNodeT * cur_node = node;
	if (node == NULL)
		return NULL;
	parent = node->parent;
	if (node->r_child != NULL)
		return treeMinimum(node->r_child);
	while (parent != NULL && cur_node == parent->r_child)
	{
		cur_node = parent;
		parent = parent->parent;
	}
	return parent;
}
Beispiel #8
0
node* tree::treeSuccessor(node* n)
{
    node* retval=m_nil;
    if(n->rightNode()!=m_nil)
    {
        retval=treeMinimum(n->rightNode());
    }
    else
    {
        node* y=n->parentNode();
        node* x=n;
        while(y!=m_nil&&x==y->rightNode())
        {
            x=y;
            y=y->parentNode();
        }
        retval=x;
    }
    return retval;
}
Beispiel #9
0
void treeDelete(struct node **root, struct node *target) {
    struct node *y;
    
    if (target->left == NULL)
        transplant(root, target, target->right);
        
    else if (target->right == NULL)
        transplant(root, target, target->left);
        
    else {
        y = treeMinimum(target->right);
        
        if (y->p != target) {
            transplant(root, y, y->right);
            y->right = target->right;
            y->right->p = y;
        }
        
        transplant(root, target, y);
        y->left = target->left;
        y->left->p = y;
    }
}
int main()
{

    Arvore *raiz = NULL;
    Arvore *buscasNaArvore = NULL;
    Arvore *trazMenorMaior = NULL;
    int chave;
    int chaveBuscada;
    //int valor;
    int opcao;

    do{

        printf("Arvores de busca binaria sem balanceamento\n");
        printf("1 - Inserir\n");
        printf("2 - Mostrar\n");
        printf("3 - Pesquisar\n");
        printf("4 - Relembrar quem esta na raiz\n");
        printf("5 - Mostrar menor valor na arvore\n");
        printf("6 - Mostrar maior valor na arvore\n");
        printf("7 - Remover valor da arvore\n");
        printf("0 - Sair\n");

        scanf("%d",&opcao);

        switch(opcao){

            case 1:
                 printf("\ninforme a chave e tecle enter\n");
                 scanf("%d",&chave);
                 raiz = treeInsert(raiz,chave);
                 break;

            case 2:
                 printf("Valores alocados nessa arvore = \n");
                 mostraArvoreEmOrdem(raiz);
                 printf("\n");
                 break;

            case 3:
                 printf("\ndigite uma chave a ser buscada nessa arvore e tecle enter\n");
                 scanf("%d",&chaveBuscada);
                 buscasNaArvore = treeSearch(raiz,chaveBuscada);

                 if(buscasNaArvore!=NULL){

                    printf("encontrei esse cara\n");
                }else{
                    printf("valor nao presente na arvore\n");
                }
                break;

            case 4:
                mostraRaizDaArvore(raiz);
                break;

            case 5:
                if(raiz!=NULL){
                    trazMenorMaior = treeMinimum(raiz);
                    printf("menor valor nesta arvore = %d\n",trazMenorMaior->chave);
                }
                break;

            case 6:
                if(raiz!=NULL){
                    trazMenorMaior = treeMaximum(raiz);
                    printf("maior valor nesta arvore = %d\n",trazMenorMaior->chave);
                }
                break;

            case 7:
                if(raiz!=NULL){

                    printf("Informe o valor para deletar e tecle enter\n");
                    scanf("%d",&chaveBuscada);
                    raiz = treeDelete(raiz,chaveBuscada);
                }
                break;
            }

        }while(opcao!=0);

    return 0;
}