2013-05-06 61 views
0

我正在做一個關於搜索引擎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; 
     } 
    } 
}; 
+7

你將不得不以削減下來的地方,如果你希望得到任何幫助,您實際上,問題在於。 – 2013-05-06 21:46:58

+0

我認爲這個代碼中有多少是完全不需要的*,對於後來者來說,這可能是一件好事,考慮到它只是錯誤的部分。在[codereview](http://codereview.stackexchange.com)而不是在這裏,這是作爲一種情況的邊界線。 – WhozCraig 2013-05-06 22:14:59

+0

當您使用調試器時,問題發生在哪裏? – 2013-05-06 22:18:06

回答

0

我可以看到一個問題,Btree類是重新活化c onstructor到BinaryTree

它應該是:

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

派生類中,通過原理將調用基類的構造函數。

還有一句:

virtual bool search() =0; 

正在申報兩次。

還有一句:

virtual void fillTree() = 0; 

需要你在Btree(派生)類來定義fillTree功能。或者將其作爲Derived類中的絕對虛函數,但這意味着該類不能啓動,並且應該由定義virtual函數的另一類繼承。

link可能有助於

+0

謝謝你關於我的搜索我宣佈twicw,因爲我會用兩個搜索方法第一個參數和第二個參數與兩個參數 – MiraiDreem 2013-05-07 09:57:33