1

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

cc1plus: warnings being treated as errors
tree.cpp: In member function âbool Tree::insert(Tree::Node*&, int, std::string)â:
tree.cpp:34: warning: control reaches end of non-void function

the tree.h file

#ifndef TREE_H
#define TREE_H

#include <iostream>
#include <string>
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*& 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);
}

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;
}
kingcong3
  • 193
  • 1
  • 2
  • 14

2 Answers2

4
cc1plus: warnings being treated as errors
tree.cpp: In member function âbool Tree::insert(Tree::Node*&, int, std::string)â:
tree.cpp:34: warning: control reaches end of non-void function

This just says that you don't return something on every possible path. Try this:

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);
//  ^^^^^^
}
Xeo
  • 129,499
  • 52
  • 291
  • 397
  • @kingcong3: Don't worry about it, everyone starts some time. Gotta admit though, that Visual Studio displays a more readable message: `warning C4715: 'insert' : not all control paths return a value` – Xeo Apr 27 '11 at 04:35
1

How about:

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

All branches need to return a value (boolean in this case).

Pete
  • 10,651
  • 9
  • 52
  • 74
  • Don't simply return an artificial value. If control reaches your last return statement, who knows if the value was really inserted? – Xeo Apr 27 '11 at 04:30
  • I also heard it is bad practice to return more then 2 bool values. – kingcong3 Apr 27 '11 at 04:35
  • @kingcong3 Well, if your function returns a bool surely at some point it can return false and at another point it can return true. But, Xeo makes a good point about returning true. I was just trying to show how to fix the error and why it works. – Pete Apr 27 '11 at 04:38
  • @kingcong3: Where did you hear that? You could return an endless number of values, if that's what could happen during normal control flow. – Xeo Apr 27 '11 at 04:43