0

0

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

王林

王林

发布时间:2023-09-06 18:01:03

|

1000人浏览过

|

来源于tutorialspoint

转载

在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’,该变量将用于字符串的结束字符。

    Red Panda AI
    Red Panda AI

    AI文本生成图像

    下载
  • 我们正在使用一个指针来连接表示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 node
struct 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 < alphabet; i++) {
      temp->alpha[i] = NULL;
   }
   return temp;
}

// We will insert the string using trie recursively
void ins_recursive_of_str(tree_trie* itm,
string str, int i){
   if (i < str.length()) {
      int idx = str[i] - 'a';
      if (itm->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 tree
void 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 not
bool isLeafNode(tree_trie* root){
   return root->end_word != false;
}

// This function is an important part of the program to display the joints of trie
void 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 < alphabet; i++){
      if (itm->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 << str[level - 1] << endl;
   }
}

// By using this function call we are diplaying the joint of trie.
void displayJ(tree_trie* root){
   int level = 0;
   char str[max];
   display_joint(root, root, str, level);
}

// main function 
int main(){
   tree_trie* root = buildNode();
   vector s = { "tutor", "true", "tuo"};

   for (string str : s) {
      ins(root, str);
   }
   cout<<"All possible joint of trie using the given list of string"<

输出

All possible joint of trie using the given list of string
u
t

结论

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

相关文章

全能打印神器
全能打印神器

全能打印神器是一款非常好用的打印软件,可以在电脑、手机、平板电脑等设备上使用。支持无线打印和云打印,操作非常简单,使用起来也非常方便,有需要的小伙伴快来保存下载体验吧!

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
数据类型有哪几种
数据类型有哪几种

数据类型有整型、浮点型、字符型、字符串型、布尔型、数组、结构体和枚举等。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

297

2023.10.31

php数据类型
php数据类型

本专题整合了php数据类型相关内容,阅读专题下面的文章了解更多详细内容。

216

2025.10.31

string转int
string转int

在编程中,我们经常会遇到需要将字符串(str)转换为整数(int)的情况。这可能是因为我们需要对字符串进行数值计算,或者需要将用户输入的字符串转换为整数进行处理。php中文网给大家带来了相关的教程以及文章,欢迎大家前来学习阅读。

312

2023.08.02

js 字符串转数组
js 字符串转数组

js字符串转数组的方法:1、使用“split()”方法;2、使用“Array.from()”方法;3、使用for循环遍历;4、使用“Array.split()”方法。本专题为大家提供js字符串转数组的相关的文章、下载、课程内容,供大家免费下载体验。

248

2023.08.03

js截取字符串的方法
js截取字符串的方法

js截取字符串的方法有substring()方法、substr()方法、slice()方法、split()方法和slice()方法。本专题为大家提供字符串相关的文章、下载、课程内容,供大家免费下载体验。

205

2023.09.04

java基础知识汇总
java基础知识汇总

java基础知识有Java的历史和特点、Java的开发环境、Java的基本数据类型、变量和常量、运算符和表达式、控制语句、数组和字符串等等知识点。想要知道更多关于java基础知识的朋友,请阅读本专题下面的的有关文章,欢迎大家来php中文网学习。

1435

2023.10.24

字符串介绍
字符串介绍

字符串是一种数据类型,它可以是任何文本,包括字母、数字、符号等。字符串可以由不同的字符组成,例如空格、标点符号、数字等。在编程中,字符串通常用引号括起来,如单引号、双引号或反引号。想了解更多字符串的相关内容,可以阅读本专题下面的文章。

609

2023.11.24

java读取文件转成字符串的方法
java读取文件转成字符串的方法

Java8引入了新的文件I/O API,使用java.nio.file.Files类读取文件内容更加方便。对于较旧版本的Java,可以使用java.io.FileReader和java.io.BufferedReader来读取文件。在这些方法中,你需要将文件路径替换为你的实际文件路径,并且可能需要处理可能的IOException异常。想了解更多java的相关内容,可以阅读本专题下面的文章。

547

2024.03.22

php源码安装教程大全
php源码安装教程大全

本专题整合了php源码安装教程,阅读专题下面的文章了解更多详细内容。

7

2025.12.31

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
DOM探索之基础详解篇
DOM探索之基础详解篇

共20课时 | 3.4万人学习

iOS应用UI控件开发基础视频
iOS应用UI控件开发基础视频

共148课时 | 30.8万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号