带有 BST 的字典函数返回向量

Dictionary function with BST returns vector

本文关键字:返回 向量 函数 字典 BST 带有      更新时间:2023-10-16

函数dictionary_select返回一个向量,其中包含以 W 开头的单词。在此代码中,dictionary_select给出错误消息。但我找不到它。有人可以帮助我吗?

#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct tree {
    string data;
    tree *left;
    tree *right;        
};
typedef tree* dictionary;
bool dictionary_ins_word(dictionary & D, const string & W)
{
    if(W == "" || W == " ")
        return false;
    tree* dic;
    dic = new tree;
    dic->data = W;
    dic->left = NULL;
    dic->right = NULL; 
    if(D == NULL) {
         D = dic;     
    }
    else {
        if(W <= D->data)
            dictionary_ins_word(D->left, W);
        else
            dictionary_ins_word(D->right, W);
    }
    return true;
}
bool dictionary_lookup(const dictionary & D, const string & W)
{
    if(W == "" || W == " ")
        return false;
    if(D == NULL)
        return false;
    if(W == D->data)
        return true;
    else if (W < D->data)
        return dictionary_lookup(D->left, W);
    else
        return dictionary_lookup(D->right, W);
}
bool dictionary_is_empty(const dictionary & D)
{
    if(D == NULL)  
        return true; 
    else
        return false;
}
bool dictionary_del_word(dictionary & D, const string & W)
{
    if(!dictionary_lookup(D, W))
        return false;
    if(W < D->data)
        dictionary_del_word(D->left, W);
    else if(W > D->data)
        dictionary_del_word(D->right, W);
    else {
        string item; 
        tree* temp;
        temp = D;
        if(D->left == NULL) {
            D = D->right;
            delete temp;
        }
        else if(D->right == NULL) {
            D = D->left;
            delete temp;
        }
        else {
            while(D->left->right != NULL) 
                D->left = D->left->right;
            item = D->left->data;
            D->data = item;
            dictionary_del_word(D->left, W);
        }
    }
    return true;
}
bool dictionary_min(string & W, const dictionary & D)
{
    dictionary min;
    if(D == NULL)
        return false;
    min = D;
    while(min->left != NULL)
        min = min->left;
    W = min->data;
    return true;     
}
vector <string> dictionary_select(const dictionary & D, const string & W)
{
vector < string > result;
    vector < string > zeroVec;
    string temp;
    zeroVec.push_back("");
    if(D == NULL)
        return zeroVec;
    temp = D->data;    
    size_t found = temp.find(W);
    if(found == 0)
        result.push_back(D->data);
    if(W <= D->data)
        return dictionary_select(D->left, W);
    else
        return dictionary_select(D->right, W);
}
int main()
{
    bool b[5];
    dictionary l;
    string W, str;
    vector <string> vec;
    l = new tree;
    l->data = "berdi";
    l->left = NULL;
    l->right = NULL;
    b[0] = dictionary_ins_word(l, "atas");
    b[1] = dictionary_ins_word(l, "cara");
    b[2] = dictionary_ins_word(l, "ata");
    b[3] = dictionary_ins_word(l, "atax");
    vec = dictionary_select(l, "ata");
    for(int i=0; i<vec.size(); i++) {
        cout << vec[i] << " ";        
    }
    getchar();
    return 0; 
}

问题是你的函数dictionary_select声明结果向量并且从不返回它。 以下是您将如何更改此设置:

vector <string> dictionary_select(const dictionary & D, const string & W)
{
    vector < string > result;
    vector < string > tempVector; // add this to store the result from the recursion
    vector < string > zeroVec;
    string temp;
    zeroVec.push_back("");
    if(D == NULL)
        return zeroVec;
    temp = D->data;    
    size_t found = temp.find(W);
    if(found == 0)
        result.push_back(D->data);
    if(W <= D->data)
        tempVector = dictionary_select(D->left, W); // get the recursion result
    else
        tempVector = dictionary_select(D->right, W);  // get the recursion result
    result.insert(result.end(), tempVector.begin(), tempVector.end()); // append all the results
    return result; // return the result
}

更新

为了使函数返回正确的数据,您还必须确保查看树的左侧和右侧以获得适当的结果。以下函数进行了更新:

vector <string> dictionary_select(const dictionary & D, const string & W)
{
    vector < string > result;
    vector < string > tempVectorLeft; // add this to store the result from the left recursion
    vector < string > tempVectorRight; // add this to store the result from the right recursion
    vector < string > zeroVec;
    string temp;
    zeroVec.push_back("");
    if(D == NULL)
        return zeroVec;
    temp = D->data;    
    size_t found = temp.find(W);
    if(found == 0)
        result.push_back(D->data);
    if(found == 0 || W <= D->data)
        tempVectorLeft = dictionary_select(D->left, W);  // store results
    if(found == 0 || W > D->data)
        tempVectorRight = dictionary_select(D->right, W);  // store results
    result.insert(result.end(), tempVectorLeft.begin(), tempVectorLeft.end()); // append all the left results
    result.insert(result.end(), tempVectorRight.begin(), tempVectorRight.end()); // append all the right results
    return result;
}

我向你建议以下变体:

vector <string> dictionary_select(const dictionary & D, const string & W)
{
    vector < string > result;
    vector < string > zeroVec;
    if (D == NULL)
        return zeroVec;
    string temp = D->data;
    size_t found = temp.find(W);
    if (found == 0)
        result.push_back(D->data);
    if (found || W <= D->data)
        for(auto x: dictionary_select(D->left, W))
            result.push_back(x);
    if (found || W > D->data)
        for (auto x : dictionary_select(D->right, W))
            result.push_back(x);
    return result;
}

初始算法中使用的if/else方法中的问题在于,您可以在左节点或右节点的递归之间进行选择,您可以有这样的注释:

       atar
       /  
     ata  atax

因此,如果节点头部的数据匹配,则需要左右检查以确保没有遗漏某些内容。