【问题标题】:C++ Trie Data Structure implementation with multiple parameters具有多个参数的 C++ Trie 数据结构实现
【发布时间】:2022-01-15 23:00:41
【问题描述】:

我需要创建一个 Trie 数据结构来存储 first namelast nameage。稍后我需要使用名字和姓氏来实现对记录的搜索。

我知道如何创建 Trie、插入、删除和搜索。但是,我不知道如何适当地存储多个值(名字、姓氏和年龄)。

有人可以指点我正确的方向吗?

#include <string>
#include <iostream>

using namespace std;
const int ALPHA_SIZE = 26;

struct Trie 
{
    struct Trie* child[ALPHA_SIZE];
    bool endofstring; //It is true if node represents end of word.
};

// Create new node
struct Trie* createNode(void) 
{
    struct Trie* tNode = new Trie;
    tNode->endofstring = false;
    for (int i = 0; i < ALPHA_SIZE; i++)
        tNode->child[i] = NULL;
    return tNode;
}

void insert(struct Trie* root, string key) 
{
    struct Trie* curr = root;
    for (int i = 0; i < key.length(); i++) {
        int index = key[i] - 'a';
        if (!curr->child[index])
            curr->child[index] = createNode();
        curr = curr->child[index];
    }
    curr->endofstring = true; //last node as leaf
}

bool search(struct Trie* root, string key) 
{ 
    struct Trie* curr = root;
    for (int i = 0; i < key.length(); i++) {
        int index = key[i] - 'a';
        if (!curr->child[index])
            return false;
        curr = curr->child[index];
    }
    return (curr != NULL && curr->endofstring);
}

bool isEmpty(Trie* root) //check if root has children 
{
    for (int i = 0; i < ALPHA_SIZE; i++)
        if (root->child[i])
            return false;
    return true;
}

Trie* deletion(Trie* root, string key, int depth = 0) 
{
    //If tree is empty return null.
    if (!root)
        return NULL;
    if (depth == key.size()) //If last character of key is being processed,
    {
        if (root->endofstring)
            root->endofstring = false; //then that node will be no more end of string after deleting it.
        if (isEmpty(root)) { //If given key is not prefix of any other string,
            delete (root);
            root = NULL;
        }
        return root;
    }
    //If key not last character,
    int index = key[depth] - 'a';
    root->child[index] =
        deletion(root->child[index], key, depth + 1); //Then recur for the child which will be obtained by using ASCII value.
    if (isEmpty(root) && root->endofstring == false) //If root does not have any child leftand it is not end of another word
    {
        delete (root);
        root = NULL;
    }
    return root;
}

int main() 
{   
    struct Trie* root = createNode();    

    insert(root, "joe");
    insert(root, "hanna");
    insert(root, "john");
    insert(root, "jane");

    search(root, "joe") ? cout << "Key is Found\n" :
        cout << "Key is not Found\n";

    search(root, "he") ? cout << "Key is Found\n" :
        cout << "Key is not Found\n";

    deletion(root, "john") ? cout << "Key is deleted\n" :
        cout << "Key is not Deleted\n";    

    return 0;
}

【问题讨论】:

  • 无关:您无需在任何地方输入struct Trie*。一旦你定义了Trie 类,它也是typedefined,所以你可以简单地写:Trie*
  • 谢谢。我在网上找到了例子。我会记住这一点的。

标签: c++ data-structures trie


【解决方案1】:

这取决于您需要搜索的内容。如果您需要按所有元素搜索,那么您可以使用以下格式

&lt;FIRST NAME&gt;;&lt;LAST NAME&gt;;&lt;AGE&gt;

示例:John;Doe;16

并以此为键。

如果您需要存储其他数据,例如height,则可以将其添加到您的struct Trie

【讨论】:

  • 我只需要按姓氏搜索,或者按名字和姓氏搜索。我不允许使用特殊字符来分隔名字/姓氏和年龄。
  • @Joe 鉴于这些限制,您可以使用&lt;LAST NAME&gt; &lt;FIRST NAME&gt; &lt;AGE&gt; 的格式,如Doe John 16 并按Doe 或John 进行搜索。您不需要将年龄添加到键中,您可以将其存储为结构的成员。
  • 对不起。我不确定我是否理解。我需要在我的 struct Trie 中创建一个 int 来存储年龄吗?
  • @Joe 你可以这样做。如果年龄不是密钥的一部分,那么您需要将其存储在某个地方。这样做的好地方是struct
猜你喜欢
  • 1970-01-01
  • 2011-04-30
  • 2020-03-09
  • 2016-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多