打印由给定字符串列表构建的Trie的所有可能节点

在c++中,trie是一种高级数据结构,用于存储树的集合。单词trie来自检索一词,它被称为数字树或前缀树。

让我们通过给定的字符串列表来举一个所有可能的联接的例子。

我们将字符串输入定义为 {“tutor”, “true”, “tuo”}

打印由给定字符串列表构建的Trie的所有可能节点

我们可以观察到不同的字符串与单个字符串相连。所以这里的tu是连接所有可能字符串的字符列表。

在本文中,我们将使用trie数据结构解决一个字符串列表中所有可能的连接。

语法

struct name_of_structure{   data_type var_name;   // data member or field of the structure.}

登录后复制

参数

struct − 这个关键字用于表示结构数据类型。

name_of_structure − 我们为结构提供任何名称。

结构是将各种相关变量集中在一个地方的集合。

treetrie* alpha[alphabet]

登录后复制

alpha是指向名为treetrie的结构指针/数据成员的变量的名称。alphabet是设置字符总数值的宏,以整数形式表示。

算法

我们首先使用一个名为‘bits/stdc++.h’的头文件,该文件包含了C++的所有标准模板库。

我们正在定义两个宏,分别是‘alphabet’‘max’,它们定义了字母表中的总字符数和字符的最大值。

我们正在创建一个名为‘tree node’的结构,并定义一个指向‘tree_node’的指针来存储字母的地址。使用bool数据类型定义变量‘end_word’,该变量将用于字符串的结束字符。

我们正在使用一个指针来连接表示trie构建的树的新节点,定义一个名为‘buildNode’的函数。

为了插入字符串,我们创建了一个名为‘ins_recursive_of_string’的递归函数,它接受三个参数- itm,str(要插入的字符串),i(表示正在处理的当前字符)。

函数ins()在代码中被定义为ins_recursive_of_str()的包装函数。它接受两个参数:tree_trie* itm(一个tree_trie对象)和string str(要插入的字符串)。它使用当前节点、要插入的字符串和起始索引0来调用递归函数。

接下来,我们正在创建一个名为 LeafNode() 的函数,它接受一个 tree_trie 对象作为参数,并检查它是否是叶节点,即它是否没有子节点。

函数 display_joint() 在代码中定义,并接受四个参数:tree_trie* root, tree_trie* itm(当前正在处理的节点),char str[](一个字符数组 str,用于存储从根节点到当前节点形成的路径字符串),以及一个 int level(表示当前节点深度的整数级别)。

该代码定义了displayJ()函数,它是display_joint()的包装函数。它接受一个tree_trie对象作为参数,并使用根节点、一个空字符数组和起始级别为0作为参数调用display_joint()函数。

该代码定义了main()函数,它生成一个新的tree_trie对象作为Trie根节点。它生成一个包含要插入到Trie中的字符串列表的向量s。然后,它调用ins()函数将每个字符串插入到Trie中。

最后,它打印一条消息来指示输出的开始,并调用 displayJ() 函数来显示所有的 Trie 连接点。

示例

在这个程序中,我们将打印由给定字符串列表构建的trie的所有可能连接点。

#include using namespace std;#define alphabet 26#define max 200// creating a structure for trie nodestruct tree_trie {   tree_trie* alpha[alphabet];   bool end_word;};tree_trie* buildNode(){   tree_trie* temp = new tree_trie();   temp->end_word = false;   for (int i = 0; i alpha[i] = NULL;   }   return temp;}// We will insert the string using trie recursivelyvoid ins_recursive_of_str(tree_trie* itm,string str, int i){   if (i alpha[idx] == NULL) {         // We are creating a new node         itm->alpha[idx] = buildNode();      }      // calling recursion function for inserting a string      ins_recursive_of_str(itm->alpha[idx],      str, i + 1);   }   else {      // We make the end_word true which represents the end of string      itm->end_word = true;   }}// By using function call we are inserting a treevoid ins(tree_trie* itm, string str){   // The necessary argument required for function call   ins_recursive_of_str(itm, str, 0);}// Using function we check whether the node is a leaf or notbool isLeafNode(tree_trie* root){   return root->end_word != false;}// This function is an important part of the program to display the joints of trievoid display_joint(tree_trie* root, tree_trie* itm,char str[], int level){   //Using this variable we are counting the current child   int current_alpha = 0;   for (int i = 0; i alpha[i]) {         str[level] = i + 'a';         display_joint(root, itm->alpha[i],         str, level + 1);         current_alpha++;      }   }      // We are printing the character if it has more than 1 character   if (current_alpha > 1 && itm != root) {      cout  s = { "tutor", "true", "tuo"};   for (string str : s) {      ins(root, str);   }   cout

输出

All possible joint of trie using the given list of stringut

登录后复制

结论

我们探讨了trie数据结构的概念,其中我们从给定的字符串列表构建了所有可能的trie连接点。我们在输出中看到,字符u和t通过使用诸如tutor、true和tuo等字符串连接了trie的所有可能连接点。因此,通过给出可能的连接点,树可以减少其节点。

以上就是打印由给定字符串列表构建的Trie的所有可能节点的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2583348.html

(0)
上一篇 2025年3月6日 14:32:46
下一篇 2025年3月6日 12:39:02

AD推荐 黄金广告位招租... 更多推荐

发表回复

登录后才能评论