BST Bölümleme hatası almaya devam

oy
3

DÜZENLEME: gdb verir akıtarak

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

İlk BST koduyla yardıma ihtiyacım var, bir segment hataya almaya devam ben bir bellek sızıntısı olduğunu düşünüyorsanız? bu yüzden bilmiyorum eğer nereye / i soruna neden olduğunu düşünüyorum kodlardır burada nasıl düzeltileceği. Henüz kurulmuş bir kopya kurucu yok diye mi ??

tree.cpp dosyası

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;
}
Oluştur 27/04/2011 saat 14:09
kaynak kullanıcı
Diğer dillerde...                            


2 cevaplar

oy
2

Ben senin yöntemin bakmak whenn bazı olası segfault görüyoruz. Sadece kenar vakalarının düşünüyorum.

Burada yapılan budur ?:

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

veya

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

Bu durumlar düzeltildi ve devam edin.

Cevap 27/04/2011 saat 14:19
kaynak kullanıcı

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

Her zaman kullandığınız rootyerine currentRoot, böylece gerçekten ağacının dibine inerler yoktur ve bir noktada bir stackoverflow alacak. Ayrıca, eğer çek kaçırıyorsun currentRootDİR NULLsonra erişmek eğer, güzel segfault alırsınız, çünkü (bu demek @tgmath budur).

bool Tree::findKey(string& s, int k, Node* currentRoot)
{
    if(currentRoot == NULL)
        return false;
    // as before ...
}
Cevap 27/04/2011 saat 14:24
kaynak kullanıcı

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