BST Otrzymuję winy segmentacji

głosy
3

EDIT: uruchomienie go przez gdb daje

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

Potrzebuję pomocy z mojego pierwszego kodu BST, wciąż otrzymuję błąd segmentacji, myślę, że jest przeciek pamięci? jeśli tak, to ja nie wiem gdzie / jak naprawić oto kody, które myślę, że są przyczyną problemu. Czy dlatego, że nie mam konstruktor kopiujący ustanowiony jeszcze ??

plik 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;
}
Utwórz 27/04/2011 o 14:09
źródło użytkownik
W innych językach...                            


2 odpowiedzi

głosy
2

Widzę jakąś możliwą segfault whenn patrzę na swojej metody. Wystarczy pomyśleć o przypadkach krawędzi.

Co się tutaj stało?:

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

lub

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

Rozwiązać te przypadki i kontynuować.

Odpowiedział 27/04/2011 o 14:19
źródło użytkownik

głosy
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;
}

Jesteś zawsze stosując rootzamiast currentRoot, tak naprawdę nie schodzą w dół drzewa i dostanie stackoverflow w pewnym momencie. Ponadto, tracisz kontrolę, jeżeli currentRootjest w stanie NULL, bo jeśli dostęp do niego wtedy, dostaniesz piękny segfault (to co @tgmath oznaczało).

bool Tree::findKey(string& s, int k, Node* currentRoot)
{
    if(currentRoot == NULL)
        return false;
    // as before ...
}
Odpowiedział 27/04/2011 o 14:24
źródło użytkownik

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