-2

I'm trying to rewrite an "ordinary" class to a template class. I ran into a problem - function int main (). I am not sure if the problems is exactly here, but the compiler reports "missing template arguments before 'b'". I couldn't find some example code related to similar problem on the internet. Can you help me please?

Here's the code I found:

#include <process.h>
#include <conio.h>
#include <iostream>
#include <cstdlib>

using namespace std;

class BinarySearchTree
{
    private:
        struct tree_node
        {
           tree_node* left;
           tree_node* right;
           int data;
        };
        tree_node* root;
    public:
        BinarySearchTree()
        {
           root = NULL;
        }

        bool isEmpty() const { return root==NULL; }
        void insert(int);
        bool search(int);

};

//----------------------------------------------------

bool BinarySearchTree::search(int d)
{
    tree_node* temp = root;
    while (temp != NULL)
        {
         if (temp->data == d)
          {
            cout<<"Tree contains this node"<<endl;
            return true;
          }
        else
          {
            if (d > temp->data)
              {
                temp = temp->right;
              }
            else
              {
                temp = temp->left;
              }
          }
        }
    cout<<"Tree does not contain this node"<<endl;
    return false;
}

void BinarySearchTree::insert(int d)
{
    tree_node* t = new tree_node;
    tree_node* parent;
    t->data = d;
    t->left = NULL;
    t->right = NULL;
    parent = NULL;

  if(isEmpty()) root = t;
  else
  {
    tree_node* curr;
    curr = root;
    // Find the Node's parent
    while(curr)
    {
        parent = curr;
        if(t->data > curr->data) curr = curr->right;
        else curr = curr->left;
    }

    if(t->data < parent->data)
       parent->left = t;
    else
       parent->right = t;
  }
}

//----------------------------------------------------

int main()
{
    BinarySearchTree b;
    int ch,tmp,tmp1,tmp2;
    while(1)
    {
       cout<<endl<<endl;
       cout<<" Binary Search Tree Operations "<<endl;
       cout<<" ----------------------------- "<<endl;
       cout<<" 1. Insertion/Creation "<<endl;
       cout<<" 2. Does Binary Searching Tree contains this node? "<<endl;
       cout<<" 3. Exit "<<endl;
       cout<<" Enter your choice : ";
       cin>>ch;
       switch(ch)
       {
           case 1 : cout<<" Enter Number to be inserted : ";
                    cin>>tmp;
                    b.insert(tmp);
                    break;
           case 2 : cout<<" Enter data to be found : ";
                    cin>>tmp;
                    b.search(tmp);
                    break;
           case 3 : system("pause");
                    return 0;
                    break;
       }
    }
}

I tried to rewrite it:

#include <process.h> 
#include <conio.h>
#include <iostream>
#include <cstdlib>

using namespace std;

template <class T>
class BinarySearchTree
{
    private:
        struct tree_node
        {
           tree_node* left;
           tree_node* right;
           T data;
        };
        tree_node* root;
    public:
        BinarySearchTree<T>()
        {
           root = NULL;
        }

        T isEmpty() const { return root==NULL; }
        T insert(T);
        T search(T);

};

//----------------------------------------------------

template <class T>T BinarySearchTree<T>::search(T d)
{
    tree_node* temp = root;
    while (temp != NULL)
        {
         if (temp->data == d)
          {
            cout<<"Tree contains this node"<<endl;
            return true;
          }
        else
          {
            if (d > temp->data)
              {
                temp = temp->right;
              }
            else
              {
                temp = temp->left;
              }
          }
        }
    cout<<"Tree does not contain this node"<<endl;
    return false;
}


template <class T>T BinarySearchTree<T>::insert(T d)
{
    tree_node* t = new tree_node;
    tree_node* parent;
    t->data = d;
    t->left = NULL;
    t->right = NULL;
    parent = NULL;

  if(isEmpty()) root = t;
  else
  {
    tree_node* curr;
    curr = root;

    while(curr)
    {
        parent = curr;
        if(t->data > curr->data) curr = curr->right;
        else curr = curr->left;
    }

    if(t->data < parent->data)
       parent->left = t;
    else
       parent->right = t;
  }
}

//----------------------------------------------------

int main()
{
    BinarySearchTree b;
    int ch,tmp,tmp1,tmp2;
    while(1)
    {
       cout<<endl<<endl;
       cout<<" Binary Search Tree Operations "<<endl;
       cout<<" ----------------------------- "<<endl;
       cout<<" 1. Insertion/Creation "<<endl;
       cout<<" 2. Does Binary Searching Tree contains this node?"<<endl;
       cout<<" 3. Exit "<<endl;
       cout<<" Enter your choice : ";
       cin>>ch;
       switch(ch)
       {
           case 1 : cout<<" Enter Number to be inserted : ";
                    cin>>tmp;
                    b.insert(tmp);
                    break;
           case 2 : cout<<" Enter data to be found : ";
                    cin>>tmp;
                    b.search(tmp);
                    break;
           case 3 : system("pause");
                    return 0;
                    break;
       }
    }
}
Anakin
  • 81
  • 1
  • 3
  • 7
  • 3
    `BinarySearchTree` a template, not a type. It needs template arguments... The same way you don't do `std::vector v;`. – chris Jun 24 '14 at 03:52
  • 3
    Your rewrite doesn't make any sense. Why are `isEmpty()`, `insert()` and `search()` returning `T`? And this `BinarySearchTree() { ... }` should be `BinarySearchTree() { ... }` – Praetorian Jun 24 '14 at 03:56
  • @Praetorian I was following the guide http://www.codingunit.com/cplusplus-tutorial-templates – Anakin Jun 24 '14 at 04:03
  • @Anakin, `Stack int_stack;` – chris Jun 24 '14 at 04:05
  • "Here's the code I found" - So which is this? the code you found or the code after rewrite? Regardless, you may do well to learn *the language* better. And to that there is no short-path, as many will attest. – WhozCraig Jun 24 '14 at 04:08
  • lol, that is just a random code I choosed for practice ... – Anakin Jun 24 '14 at 04:37

2 Answers2

3

The error is explicit, you need to give a template argument to it, such as:

BinarySearchTree<int> b;
songyuanyao
  • 169,198
  • 16
  • 310
  • 405
1

Apart from template instantiation problem, I could see other issues as well.

  1. What will happen if you use insert method to add a value which is already in the tree. It'll add a new node to the right. This is wrong from BST concepts.

  2. Insert method doesn't actually return anything at all.

Doonyx
  • 580
  • 3
  • 12