BST seguir recibiendo Fallo de segmentación

votos
3

EDIT: la ejecución a través del BGF da

Program received signal SIGSEGV, Segmentation fault.
0x0000000000400e4c in Tree::findKey(std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, int, Tree::Node*) ()

Necesitar algo de ayuda con mi primer código BST, me siguen dando un fallo de segmentación, creo que es una pérdida de memoria? Si así que no sé dónde / cómo solucionar estos son los códigos que creo que son la causa del problema. ¿Es porque no tiene un constructor de copia creado todavía ??

archivo tree.cpp

Tree::Tree()
{
  root = NULL;
}

bool Tree::insert(int k, string s)
{
  return insert(root, k, s);
}
//HELPER Call find data with key function
bool Tree::findKey(string& s, int k)
{
    return findKey(s, k, root);
}
bool Tree::insert(Node*& currentRoot, int k, string s)
{
  if(currentRoot == NULL){
    currentRoot = new Node;
    currentRoot->key = k;
    currentRoot->data = s;
    currentRoot->left = NULL;
    currentRoot->right = NULL;
    return true;
  }
  else if (currentRoot->key == k)
    return false;
  else if (currentRoot->key > k)
    return insert(currentRoot->left, k, s);
  else
    return insert (currentRoot->right,k, s);
}
bool Tree::findKey(string& s, int k, Node* currentRoot)
{
    if (currentRoot->key == k){
        s = root->data;
        return true;
    }
    else if (root->key < k)
        return findKey (s, k, root->right);
    else if (root->key > k)
        return findKey (s, k, root->left);
    else
        return false;
}

main.cpp

int main()
{
string sout;
  Tree test;
    test.insert(1, a);
    test.insert(2, b);
    test.insert(3, c);
    test.findKey(sout, 3);
    cout<<sout<<endl;
  return 0;
}
Publicado el 27/04/2011 a las 14:09
fuente por usuario
En otros idiomas...                            


2 respuestas

votos
2

Veo una posible violación de segmento whenn miro a su método. Basta pensar en casos extremos.

¿Qué pasa aquí?:

Tree test; 
test.findKey(sout, 3);

o

Tree test;
test.insert(1, "a");
test.findKey(sout, 3);

Solucionar estos casos y proceder.

Respondida el 27/04/2011 a las 14:19
fuente por usuario

votos
2

bool Tree::findKey(string& s, int k, Node* currentRoot)
{
    if (currentRoot->key == k){
        s = root->data;
        return true;
    }
    else if (root->key < k)
        return findKey (s, k, root->right);
    else if (root->key > k)
        return findKey (s, k, root->left);
    else
        return false;
}

Siempre se está utilizando rooten lugar de currentRoot, por lo que en realidad no desciende por el árbol y obtendrá un stackoverflow en algún momento. Además, se está perdiendo la comprobación de si el currentRootes decir NULL, porque si se accede a él a continuación, obtendrá un buen violación de segmento (esto es lo que quería decir @tgmath).

bool Tree::findKey(string& s, int k, Node* currentRoot)
{
    if(currentRoot == NULL)
        return false;
    // as before ...
}
Respondida el 27/04/2011 a las 14:24
fuente por usuario

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more