我正在做一个关于搜索引擎usinng二叉树的项目。 我觉得有很多问题。我尝试过这样没有任何结果 ,我不知道如何在main
中调用Btree,它总是给我一个错误。搜索引擎使用二叉树代码C++的帮助请
我有一个int和字符串的文本文件。这将是这样的:
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);
};
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-B节点类
#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- B树类
#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;
}
}
};
你将不得不以削减下来的地方,如果你希望得到任何帮助,您实际上,问题在于。 – 2013-05-06 21:46:58
我认为这个代码中有多少是完全不需要的*,对于后来者来说,这可能是一件好事,考虑到它只是错误的部分。在[codereview](http://codereview.stackexchange.com)而不是在这里,这是作为一种情况的边界线。 – WhozCraig 2013-05-06 22:14:59
当您使用调试器时,问题发生在哪里? – 2013-05-06 22:18:06