İlk BST'de eki ile yardımcı

oy
1

DÜZENLEME orada Eksik küçük bir şey !! Hata hala orada

Yani benim ilk BST kod öğrenmek çalışılıyor ve zor .... ben zaten kodlarının sadece birkaç satır ile sorun yaşıyorum edilir. Sorun tablası içinde, ama benim tarzım / diğer hatalar bazı geri bildirimler alabilir böylece her şeyi dahil ettik. Ben işaretçi uygulanmasına bir işaretçi kullanmak önerildi, ancak bunu henüz öğrenmiş sığınak, bu yüzden / rahatlık hissediyorum henüz bu kodu nasıl bilmiyorum.

hatadır

[trinhc@cs1 Assignment_3]$ g++ movieList.cpp -o a.out
/tmp/ccLw6nsv.o: In function `main':
movieList.cpp:(.text+0x7a): undefined reference to `Tree::Tree()'
movieList.cpp:(.text+0xa7): undefined reference to `Tree::insert(int, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
collect2: ld returned 1 exit status

tree.h dosyası

#ifndef TREE_H
#define TREE_H

#include <string>
#include <iostream>
using namespace std;

class Tree
{
 public:
  Tree();
  bool insert(int k, string s);

 private:
  struct Node
  {
    int key;
    string data;
    Node *left;
    Node *right;
  };
  Node* root;
  bool insert(Node*& root, int k, string s);
};

#endif

tree.cpp

#include <iostream>
#include tree.h
#include <stack>
#include <queue>
#include <string>
using namespace std;

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

bool Tree::insert(int k, string s)
{
  return insert(root, k, s);
}

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

movieList.cpp

#include <iostream>
#include <stack>
#include <queue>
#include <string>
#include tree.h


using namespace std;

int main()
{
  Tree test;
  test.insert(100, blah);
  return 0;
}
Oluştur 27/04/2011 saat 03:15
kaynak kullanıcı
Diğer dillerde...                            


4 cevaplar

oy
2

Ağaç testi (); nasıl class Test bir nesneyi tanımlamak değildir, bu acutally ağaç ile döner adlandırılan bir test bildirin.

Deneyin

Ağaç testi; test.instert (100, "fan"); 0 döndürür;

Cevap 27/04/2011 saat 03:34
kaynak kullanıcı

oy
1

noktalarının Çift:

Ben m_root veya my_root veya tree_root veya bu tür bir şey tavsiye ediyorum else- şey için üye değişkeni kökü adını değiştirmek gerekir. Şu anda size bir argüman olarak kök dahil herhangi işlevinde bir ad çatışması biraz var. Bu aynı zamanda senin bahsettiğin hangi kök takip sağlayacaktır.

bool Tree::insert(Node*& root, int k, string s)
{
    if(root == NULL){
        root = new Node;
        root->key = k;
        root->data = s;
        root->left = NULL;
        root->right = NULL;
        return true;
    } else
        if (root == k) //Comparison between pointer and number.
            return false;
        else
            if (root->key > k)
                insert(root->left, k, s);
            else
                insert (root->right,k, s);
    }

Sen Kök- için yorumladı hat> tuş üzerinde kök değiştirmeniz gerekir.

bu ödeme şekli gibi Bunun dışında görünüyor.

DÜZENLEME: Ayrıca, diğer adam neyi söyledi. Bir nesne olarak bildirmek

TYPE name ()

Varsayılan yapıcı () çağırarak eğer, bu nedenle ana işlevi sizin kodu olmalıdır

Tree test;
test.insert(...)
Cevap 27/04/2011 saat 03:38
kaynak kullanıcı

oy
1

Kuralların bazı kopyalanıp bu benim için iyi çalışıyor:

ana:

#include <iostream>
#include <stack>
#include <queue>
#include <string>
#include "tree.h"

    int main()
    {
      Tree test;
      test.insert(100, "blah");
      test.insert(50, "fifty");
      test.insert(110, "one hundred ten");
      return 0;
    }

İşlev ekle:

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)
    insert(currentRoot->left, k, s);
  else
    insert (currentRoot->right,k, s);
}

Bunun dışında biryere sözdizimi hataları var. Ben de birileri bir adlandırma sorunu biraz vardı işaret çünkü olarak adını değiştirdi. Her özyineleme üzerinde onu sağ veya sol alt ağaç kök geçiyoruz çünkü CurrentRoot mantıklı.

Cevap 27/04/2011 saat 03:39
kaynak kullanıcı

oy
0

Eklemek gerekmiyor tree.cppsenin inşa komutuna?

[Trinhc @ cs1 Assignment_3] $ g ++ movieList.cpp -o a.out

Olacaktı

[Trinhc @ cs1 Assignment_3] $ g ++ tree.cpp movieList.cpp -o a.out

Cevap 27/04/2011 saat 05:01
kaynak kullanıcı

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