开发者

help with insert on first BST

EDIT there a small thing that I am missing!! the error is still there

So I am attempting to learn how to code my first BST, and it is hard.... I am already having trouble with just a few lines of codes. the problem is in the insert, but I have included everything so that I could get some feedback on my style/other errors. I was suggested to use a pointer to pointer implementation, but we havent learned it yet, so I dont feel comfort/know how to code it yet. the

error is

[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

the tree.h file

#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);
  el开发者_如何学Gose
    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;
}


Tree test(); is not how define an object of class Test, This acutally declare function named test which returns Tree.

try

Tree test; test.instert(100, "blah"); return 0;


I copied some of your code and this is working fine for me:

main:

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

Insert function:

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

Other than that you have syntax errors all over the place. I also changed the name because as someone pointed out there was a bit of a naming problem. CurrentRoot makes sense because you are passing it the root of the left or right subtree on every recursion.


Couple of points:

You need to change the name of your member variable root to something else– I'd recommend m_root, or my_root, or tree_root, or something of those sorts. Right now you've got a little bit of a namespace clash in any function where you include root as an argument. This will also let you keep track of which root you're referring to.

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

You need to change root on the commented line to root->key.

Other than that, it looks like it'll work.

EDIT: Also, what the other guy said. You declare an object as

TYPE name ()

if you are calling the default constructor (), so your code in your main function should be

Tree test;
test.insert(...)


Shouldn't you add tree.cpp to your build command?

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

Would become

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜