BST continuo a ricevere Segmentation fault

voti
3

EDIT: in esecuzione attraverso il gdb dà

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

Hai bisogno di aiuto con il mio primo codice BST, continuo a ricevere un errore di segmentazione, penso che si tratta di una perdita di memoria? Se è così non so dove / come risolvere qui sono i codici che penso siano la causa del problema. È perché non ho un costruttore di copia set ancora ??

file di 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;
}
È pubblicato 27/04/2011 alle 14:09
fonte dall'utente
In altre lingue...                            


2 risposte

voti
2

Vedo qualche possibile segfault whenn guardo il tuo metodo. Basti pensare a casi limite.

Che succede qui?:

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

o

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

Fissare questi casi e procedere.

Risposto il 27/04/2011 a 14:19
fonte dall'utente

voti
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;
}

Utilizzare sempre root, invece di currentRoot, in modo che non si ha realmente scende verso il basso l'albero e otterrà uno StackOverflow ad un certo punto. Inoltre, vi state perdendo il controllo se l' currentRootIS NULL, perché se si accede poi, si otterrà un bel segfault (questo è quello che voleva dire @tgmath).

bool Tree::findKey(string& s, int k, Node* currentRoot)
{
    if(currentRoot == NULL)
        return false;
    // as before ...
}
Risposto il 27/04/2011 a 14:24
fonte dall'utente

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