C++ 搜索引擎使用二叉树代码c++;请帮忙
我正在做一个关于使用二叉树搜索引擎的项目。 我认为有很多问题。我试了这么多,没有结果 我不知道如何在C++ 搜索引擎使用二叉树代码c++;请帮忙,c++,search,C++,Search,我正在做一个关于使用二叉树搜索引擎的项目。 我认为有很多问题。我试了这么多,没有结果 我不知道如何在main中调用Btree,它总是给我一个错误 我有一个带有int和string的文本文件。它将是这样的: 1925年约翰·贝尔德发射了第一个电视信号 在这个程序中,我将按整数年搜索,并给出那一年发生的事情的信息 1-信息类 #include <iostream> #include <string> using namespace std; cl
main
中调用Btree,它总是给我一个错误
我有一个带有int和string的文本文件。它将是这样的:
1925年约翰·贝尔德发射了第一个电视信号
在这个程序中,我将按整数年搜索,并给出那一年发生的事情的信息
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);
};
#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;
};
#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);
}
};
#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
};
#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;
}
}
}
};
#包括
#包括
使用
名称空间标准;
类信息{
私人:
国际年;
字符串数据;
公众:
信息(整数年、字符串数据);
字符串getData();
int getyear();
无效设置数据(字符串数据);
无效设置年(整数年);
};
2-BinNode类
#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);
};
#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;
};
#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);
}
};
#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
};
#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;
}
}
}
};
#包括“information.h”
#包括
//二叉树节点
模板类BinNode{
公众:
虚拟«BinNode(){}
虚拟void setEvent(常量信息&)=0;
虚拟BinNode*left()常量=0;
虚拟void setLeft(BinNode*)=0;
虚拟BinNode*right()常量=0;
虚拟void setRight(BinNode*)=0;
虚拟布尔isLeaf()=0;
};
3-B节点类
#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);
};
#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;
};
#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);
}
};
#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
};
#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;
}
}
}
};
#包括
#包括“BinNode.h”
#包括“information.h”
使用名称空间std;
//二叉树节点的实现
模板
类BNode:公共BinNode{
私人:
信息事件;
BNode*lc;
b节点*rc;
公众:
BNode(){lc=rc=NULL;}
B节点(信息d,B节点*l=NULL,
Bnode*r=NULL){
info=d;lc=l;rc=r;
}
信息获取事件(){
returninformation.getEvent;
}
信息设置事件(信息e){
事件=e;
}
BNode*left()常量{return lc;}
void setLeft(BNode*b){lc=b;}
BNode*right()常量{return rc;}
A.
void setRight(BNode*b){rc=b}
布尔岛{
返回(lc==NULL)和&(rc==NULL);
}
};
4-二叉树类
#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);
};
#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;
};
#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);
}
};
#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
};
#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;
}
}
}
};
#包括;
//二叉树
模板类二叉树{
公众:
二叉树();
虚拟布尔搜索()=0;
虚拟布尔搜索()=0;
虚空插入()=0;
虚拟bool remove()=0;
虚拟空填充树()=0
};
5-b树类
#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);
};
#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;
};
#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);
}
};
#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
};
#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;
}
}
}
};
#包括
#包括
#包括“BinaryTree.h”
#包括“BNode.h”
#包括“information.h”
使用名称空间std;
//二叉树实现
模板类BTree:公共二进制树{
私人:
BNode*根;
公众:
BinaryTree(){root=NULL;}
空白插入(信息一){
B节点*电流;
BNode*牵引电流;
BNode*newNode;
newNode=newbnode;
newNode->Event=i;
newNode->lc=NULL;newNode->rc=NULL;
if(root==NULL)
根=新节点;
否则{
电流=根;
while(当前!=NULL){
牵引电流=电流;
如果(当前->事件==i){
CouteEvent>键)
电流=电流->lc;
否则当前=当前->rc;
}
如果(牵引电流->事件>i)
trailCurrent->lc=newNode;
else trailCurrent->rc=newNode;
}
}
布尔搜索(整数键){
B节点*电流;
bool-found=false;
if(root==NULL)
cout Event.getYear()=键)
发现=真;
cout-rc;
}
}
}
bool删除(信息一){
B节点*电流;
BNode*newNode;
newNode=newbnode;
newNode->Event=i;
newNode->lc=NULL;newNode->rc=NULL;
电流=根;
while(当前!=NULL){
如果(当前->事件==i){
删除当前文件;
}
}
}
};
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;
}
}
};
#包括
#包括“BTree.h”
#包括“information.h”
#包括
#包括
使用名称空间std;
int main(){
b树b;
int-ch;
字符串数据;
int键,键2;
国际年;
ifstream文件;
打开(“date.txt”);
如果(!文件){
cout>数据)
{
year=file.get();
p、 设定年份(年);
cout我可以看到a问题,Btree
类正在将构造函数重新斜体化为BinaryTree
应该是:
public:
//BinaryTree() {root = NULL;}
Btree() { }
原则上,派生类将调用基类构造函数
另一个:
virtual bool search() =0;
virtual void fillTree() = 0;
被宣布两次
另一个:
virtual bool search() =0;
virtual void fillTree() = 0;
要求您在Btree
(派生)类中定义fillTree
函数。或者将其作为派生的
类中的绝对虚拟函数,但这意味着该类不能启动,应该由另一个定义虚拟
函数的Dervied
类继承
这可能会有所帮助如果你希望得到任何帮助,你将不得不将其缩减到你的问题所在。我想,如果你真的不需要这么多代码,我会感到非常惊讶,这在事后看来可能是一件好事,考虑到它的错误部分。这是一个临界点这是一个比这里更好的例子。当您使用调试器时,问题发生在哪里?感谢您对我声明为twicw的搜索,因为我将使用两个搜索方法:第一个带一个参数,第二个带两个参数