Ir para conteúdo
Fórum Script Brasil
  • 0

Tree - c++ - insertion


Murilo Marchiori

Pergunta

Hello, friends. I am having a little problem with a c++ tree... When i try to delete a node, and there is just one node in the tree (so I must delete the root), the program does not work ("Segmentation Fault" message, compiling on Linux). Here is my code, and below is the logic that I tried to follow.. can you help, me please? Thank you very much!

*aux is the node that I must delete, and aux_prev is the previous node

if(aux->right == NULL && aux->left == NULL)
{
    if(aux_prev != NULL)
    {                
        if(aux_prev->right == aux)
        {
            aux_prev->right = NULL;
            delete aux;
        }
        else
        {
            aux_prev->left = NULL;
            delete aux;
        }
    }
    else
    {        
        tree.root = NULL;
        delete aux;                                
    }            
}
if (the node don't have sons)
{
    if (is not the root)
    {                
        if (the node is in the right of the previous node)
        {
            aux_ant->right = NULL;
            delete aux;
        }
        else //the node is in the left of the previous node
        {
            aux_ant->left = NULL;
            delete aux;
        }
    }
    else //if is the root
    {    
        tree.root = NULL; //the root ponts to NULL
        delete aux; //delete the node                                
    }            
}

Link para o comentário
Compartilhar em outros sites

0 respostass a esta questão

Posts Recomendados

Até agora não há respostas para essa pergunta

Participe da discussão

Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.

Visitante
Responder esta pergunta...

×   Você colou conteúdo com formatação.   Remover formatação

  Apenas 75 emoticons são permitidos.

×   Seu link foi incorporado automaticamente.   Exibir como um link em vez disso

×   Seu conteúdo anterior foi restaurado.   Limpar Editor

×   Você não pode colar imagens diretamente. Carregar ou inserir imagens do URL.



  • Estatísticas dos Fóruns

    • Tópicos
      152,1k
    • Posts
      651,8k
×
×
  • Criar Novo...