C ++中的Template类和int main()

时间:2014-06-24 03:51:59

标签: c++ algorithm binary-search-tree class-template

我试图改写一个普通的"类到模板类。我遇到了一个问题 - 函数 int main()。我不确定问题是否恰好在这里,但编译器报告" 在' "之前缺少模板参数。我无法在互联网上找到与类似问题相关的示例代码。你能帮帮我吗?

这是我找到的代码:

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

我试着改写它:

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

2 个答案:

答案 0 :(得分:3)

错误是显式的,您需要为其提供模板参数,例如:

BinarySearchTree<int> b;

答案 1 :(得分:1)

除了模板实例化问题外,我还可以看到其他问题。

  1. 如果使用insert方法添加树中已有的值,会发生什么。它会在右侧添加一个新节点。这是BST概念的错误。

  2. Insert方法实际上根本不返回任何内容。

相关问题