0

我正在做一个关于使用二叉树的搜索引擎的项目。我认为有很多问题。我尝试了这么多没有任何结果,我不知道如何调用 Btree main,它一直给我一个错误。

我有一个带有 int 和 string 的文本文件。它会是这样的:

1925 John Baird 传输第一个电视信号

在这个程序中,我将按 int 年份进行搜索,并给出那一年发生的事情的信息。

1-信息类

    #include <iostream>
    #include <string>
    using 

namespace std;

class information{ 

private:

    int year;
    string data; 

public:

    information(int  year, string data);
    string getData();
    int getyear();
    void setData(string dat);
    void setYear(int yer);
};

2- BinNode 类

 #include "information.h"
#include <iostream>
// Binary tree node ADT

template <typename Elem> class BinNode {

public:

virtual ˜BinNode() {}

    virtual void setEvent(const information&) = 0;
    virtual BinNode* left() const = 0;
    virtual void setLeft(BinNode*) = 0;
    virtual BinNode* right() const = 0;
    virtual void setRight(BinNode*) = 0;
    virtual bool isLeaf() = 0;
};

3-BNode类

   #include <iostream>
#include "BinNode.h"
#include "information.h"

using namespace std;

// Binary tree node implementation

template <typename Elem> 
class BNode: public BinNode<Elem> {

  private:

    information Event;

    BNode* lc;
    BNode* rc;
       public:
    BNode() { lc=rc=NULL;}
    BNode(information d, Bnode* l = NULL, 
               Bnode* r = NULL) {
        info = d;  lc = l; rc = r;
    }

        information getEvent(){
        return information.getEvent;

    }

     information setEvent(information e) {
         Event = e;
     }


    BNode* left() const {return lc;}

    void setLeft(BNode* b) {lc = b;}

    BNode* right() const {return rc;}
    a
    void setRight(BNode* b) {rc = b}

    bool isLeaf() {
        return (lc==NULL) && (rc==NULL);
    }
};

4-二叉树类

 #include <iostream>;

// Binary tree ADT

template <int> class BinaryTree {

  public:
    BinaryTree();

    virtual bool search() =0;
    virtual bool search() =0;
    virtual void insert() =0;
    virtual bool remove() = 0;
    virtual void fillTree() = 0


};

5- BTree 类

  #include <iostream>
#include<fstream>
#include "BinaryTree.h"
#include "BNode.h"
#include "information.h"

using namespace std;

// Binary tree implementation

template <type Elem> class BTree: public BinaryTree<Elem> {

  private:
    BNode* root;

  public:
       BinaryTree() {root = NULL;}


  void insert(information i){

  BNode* current;

  BNode* trailCurrent;

  BNode* newNode;

  newNode = new BNode;

  newNode->Event= i;

  newNode->lc=NULL; newNode->rc=NULL;

  if (root == NULL)

     root = newNode;

  else{

     current = root;

     while(current!=NULL){

       trailCurrent = current;

       if (current->Event== i){

        cout<< “No duplicate allowed”;return;}

       else if (current->Event> key)

     current = current->lc;

       else current = current->rc;

     }
      if(trailCurrent->Event> i)

        trailCurrent->lc = newNode;

      else trailCurrent->rc = newNode;

  }
 }


  bool search(int key){

  Bnode* current;

  bool found = false;

  if (root == NULL)

     cout << "Empty Tree";

  else{
     current = root;

     while(current!= NULL && !found){

       if (current->Event.getYear() == key)

            found = true;

cout << "The IN FORMATION for " << key << " is " << curr->Event.getData() << endl;

else if (current->Event> key)

                current = current->lc;

       else current = current->rc;

     }
  }
}


  bool remove(information i){

  BNode* current;

  BNode* newNode;

  newNode = new BNode;

  newNode->Event= i;

  newNode->lc=NULL; newNode->rc=NULL;

     current = root;

     while(current!=NULL){

       if (current->Event== i){

           delete current;

       }
     }
  }

};

6-主要

#include <iostream>
#include "BTree.h"
#include "information.h"
#include <fstream>
#include<string>

using namespace std;




int main(){

    BTree <>  b;    
    int ch;
    string data;
    int key,key2;
    int year;

    ifstream file;
    file.open("date.txt");
    if(!file) {
        cout<<" Error opening file. " << endl;
    }



    while(file >> year >> data)
    {


     year = file.get();
     p.setYear(year);  
     cout <<  p.getyear() << " ";
     getline ( file, data );
     p.setData(data);
     cout << p.getData() << endl;
     b.insert(p);
    }
    file.close();



    while(1)
    {
       cout<<" Binary Search Tree"<<endl;
       cout<<" 0. Search by year"<<endl;
       cout<<" 1. Search by tow year "<<endl;
       cout<<" 2. Exit "<<endl;
       cout<<" Enter your choice : ";
       cin>>ch;
       switch(ch)
       {
           case 0 : cout <<" Enter the year  to search : "<<endl;
                    cin>>key;
                    b.search(key);
                    break;

           case 1 : cout<<" Enter the first year: ";
                    cin>>key;
        cout<<" Enter the socend year: ";
        cin>>key2;
                   // b.search(key,key2);
                    break;

           case 2 : return 0;
       }
    }
};
4

1 回答 1

0

我可以看到一个 问题,Btree该类正在重新初始化构造函数BinaryTree

它应该是:

public:
   //BinaryTree() {root = NULL;}
   Btree() { }

派生类,原则上将调用基类构造函数。

另一个:

virtual bool search() =0;

被宣布两次。

另一个:

virtual void fillTree() = 0;

要求您在(派生)类中定义一个fillTree函数。Btree或者将其作为类中的绝对虚函数Derived,但这意味着该类不能被启动,并且应该由Dervied定义该virtual函数的另一个类继承。

链接可能会有所帮助

于 2013-05-06T22:51:06.300 回答