返回

时间:2018-02-04 06:42:43

标签: c++ binary-search-tree

我意识到还有其他像这样的答案,但是seg故障非常普遍,并且它们都不符合我的情况。我正在创建一个二叉树并存储数据。在printInOrder函数中,它一直递归到最远的leftChild,然后在到达return语句时发生seg错误。我不确定为什么会这样做。在此先感谢您的帮助。

#include <string>
#include <cstdlib>
#include <iostream>
#include "BST.h"

using namespace std;

BST::BST(void) {
    root = NULL;

    cout << "Default object is being creating." << "\n";
};
/*
BST::BST(string word, int count) {
    cout << "Default object is being creating." << "\n";
    this->root->word = word;
    this->root->count = count;
};

BST::BST(string word) {
    cout << "Default object is being creating." << "\n";
    this->root->word = word;
    this->root->count = 1;
};
*/
bool BST::hasWord(string word) {
    return true;
};

void BST::printInOrder() {
    pio(this->root);
}

void BST::pio(struct node* node) {
    cout << "root: " << root << endl;
    cout << "node: " << node << endl;
    cout << "leftChild: " << node->leftChild->leftChild << endl;
    if (node != NULL){

        /* first recur on left child */
        pio(node->leftChild);

        /* then print the data of node */
        cout << "Word: " << node->word << endl;
        cout << "Count: " << node->count << endl;  

        /* now recur on right child */
        pio(node->rightChild);
    }
    if (node == NULL) {
        return;
    }
}
void BST::insert(string word)
{
    if(this->root != NULL)
        insert(word, this->root);
    else
    {
        cout << "obj has been inserted " << endl;
        this->root = new node;
        cout << root << endl;
        this->root->word = word;
        this->root->count = 1;
        this->root->leftChild = NULL;
        this->root->rightChild = NULL;
    }
}

void BST::insert(string word, struct node *leaf)
{
    if(word< leaf->word)
    {
        if(leaf->leftChild != NULL)
            insert(word, leaf->leftChild);
        else
        {
            cout << "obj has been inserted " << endl;
            leaf->leftChild = new node;
            cout << leaf->leftChild << endl;
            leaf->leftChild->word = word;
            leaf->leftChild->count = 1;
            leaf->leftChild->leftChild = NULL;    //Sets the leftChild child of the child node to null
            leaf->leftChild->rightChild = NULL;   //Sets the rightChild child of the child node to null
        }  
    }
    else if(word>leaf->word)
    {
        if(leaf->rightChild != NULL)
            insert(word, leaf->rightChild);
        else
        {
            cout << "obj has been inserted " << endl;
            leaf->rightChild = new node;
            cout << leaf->rightChild << endl;
            leaf->rightChild->word = word;
            leaf->rightChild->count = 1;
            leaf->rightChild->leftChild = NULL;  //Sets the leftChild child of the child node to null
            leaf->rightChild->rightChild = NULL; //Sets the rightChild child of the child node to null
        }
    }
  //word is already in the tree so we increment counter
  else  leaf->count += 1;
}

主档案:

#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <dirent.h>
#include <cstring>
#include <functional>
#include <algorithm>
#include <sstream>

#include "BST.h"


using namespace std;

void parseFile(string fullPath, BST bst) {
    ifstream infile(fullPath); // Open it up!
    std::string line;
    char c;
    string word = "";
    while (std::getline(infile, line))
    {
        // Iterate through the string one letter at a time.
        for (int i = 0; i < line.length(); i++) {

            c = line.at(i); // Get a char from string
            tolower(c);        
            // if it's NOT within these bounds, then it's not a character
            if (! ( ( c >= 'a' && c <= 'z' ) || ( c >= 'A' && c <= 'Z' ) ) ) {

                //if word is NOT an empty string, insert word into bst
                if ( word != "" ) {
                    bst.insert(word);
                    //reset word string
                    word = "";
                }
            }
            else {
                word += string(1, c);
            }
         }
     }
}

void getReqDirs(BST bst, const string path, vector<string> files,const bool showHiddenDirs = false){
    DIR *dpdf;
    struct dirent *epdf;
    dpdf = opendir(path.c_str());
    int count = 0;
    if (dpdf != NULL){
        while ((epdf = readdir(dpdf)) != NULL){
            if(showHiddenDirs ? (epdf->d_type==DT_DIR && string(epdf->d_name) != ".." && string(epdf->d_name) != "." ) : (epdf->d_type==DT_DIR && strstr(epdf->d_name,"..") == NULL && strstr(epdf->d_name,".") == NULL ) ){
                getReqDirs(bst,path+epdf->d_name+"/",files, showHiddenDirs);
            }
            if(epdf->d_type==DT_REG){
                //files.push_back(path+epdf->d_name);
                //parseFile(path+epdf->d_name, bst);
                cout << path+epdf->d_name << " ";

            }
        }
    }
    closedir(dpdf);
}



int main()
{

    vector <string> words; // Vector to hold our words we read in.
    string str; // Temp string to
    cout << "Read from a file!" << endl;
    BST bst;
    string path = "test/"; //name of directory that holds the data base
    vector<string> files;  //create vector for file names

/*
    //collect all the file names in the data base
    getReqDirs(bst,path,files,false);
    for (int i = 0; i < files.size(); i++) {
        cout << files[i] << " ";
    }*/
    bst.insert("gary");
    bst.insert("Mil");
    bst.insert("Taylor");
    bst.insert("BrIaN");
    bst.insert("fart");
    bst.insert("Juan");
    bst.insert("James");
    bst.insert("Gary");
    bst.printInOrder();
    //parseFile(path, bst);
    return 0;
}

如果需要任何其他信息,请告诉我。

1 个答案:

答案 0 :(得分:0)

以下行是我在pio中找到的唯一有问题的代码行:

cout << "leftChild: " << node->leftChild->leftChild << endl;

这是一个问题,因为node在某些时候绑定为NULL。

删除该行。

另外,你不需要这些行

if (node == NULL) {
    return;
}

他们没有做任何有用的事情,可以删除。这是该功能的更新版本:

void BST::pio(struct node* node) {
    cout << "root: " << root << endl;
    cout << "node: " << node << endl;

    if (node != NULL){

        /* first recur on left child */
        pio(node->leftChild);

        /* then print the data of node */
        cout << "Word: " << node->word << endl;
        cout << "Count: " << node->count << endl;  

        /* now recur on right child */
        pio(node->rightChild);
    }
}