数据结构-链表字典树实现(单词查找树,Trietree)C语言

字典树的相关概念请查找其他资料,不再累述

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct Node{
	char data;
	struct Node* next[26];
}LNode,*LinkList;
LinkList CreateHead();
LinkList AddData(LinkList head,char* word);
bool FindWord(LinkList head,char* word);
int main(){
	LinkList head = CreateHead();
	head = AddData(head,"abcd");
	head = AddData(head,"abce");
	head = AddData(head,"abca");
	head = AddData(head,"bacbe");
	head = AddData(head,"bcc");
	if(FindWord(head,"abcd")) printf("yes\n"); 
	if(FindWord(head,"abce")) printf("yes\n"); 
	if(FindWord(head,"abca")) printf("yes\n"); 
	if(FindWord(head,"bacbe")) printf("yes\n"); 
	if(FindWord(head,"bcc")) printf("yes\n");
	if(FindWord(head,"bbac")) printf("yes");
	else printf("NoFind");
} 
LinkList CreateHead(){
	LinkList head =
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Trie是一种用于高效存储和检索字符串数据集的数据结构。在实现Trie的过程中,我们需要使用一个数组来存储节点信息,而不是链表。 以下是C语言实现遍历Trie生成单词字典序的示例代码,其中使用了数组来存储节点信息: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define ALPHABET_SIZE 26 // Trie节点结构体 struct TrieNode { struct TrieNode *children[ALPHABET_SIZE]; int isEndOfWord; }; // 创建Trie节点 struct TrieNode *createNode() { struct TrieNode *node = (struct TrieNode *)malloc(sizeof(struct TrieNode)); node->isEndOfWord = 0; for (int i = 0; i < ALPHABET_SIZE; i++) { node->children[i] = NULL; } return node; } // 插入单词到Trie void insert(struct TrieNode *root, char *word) { struct TrieNode *node = root; int len = strlen(word); for (int i = 0; i < len; i++) { int index = word[i] - 'a'; if (!node->children[index]) { node->children[index] = createNode(); } node = node->children[index]; } node->isEndOfWord = 1; } // 遍历Trie并生成单词字典序 void traverse(struct TrieNode *node, char *word, int level) { if (node->isEndOfWord) { word[level] = '\0'; printf("%s\n", word); } for (int i = 0; i < ALPHABET_SIZE; i++) { if (node->children[i]) { word[level] = i + 'a'; traverse(node->children[i], word, level + 1); } } } int main() { char *words[] = {"hello", "world", "he", "hi", "there", "their", "they", "them"}; int n = sizeof(words) / sizeof(words[0]); // 创建Trie并插入单词 struct TrieNode *root = createNode(); for (int i = 0; i < n; i++) { insert(root, words[i]); } // 遍历Trie并生成单词字典序 char word[100]; traverse(root, word, 0); return 0; } ``` 上述代码中,我们首先定义了一个Trie节点结构体,其中包含一个指向子节点的指针数组和一个表示当前节点是否为单词结尾的标志位。然后,我们定义了几个函数来创建节点、插入单词以及遍历Trie并生成单词字典序。 在`main`函数中,我们首先定义了一个字符串数组来存储要插入Trie单词。然后,我们创建了一个Trie并依次插入这些单词。最后,我们调用了`traverse`函数来遍历Trie并生成单词字典序。在遍历的过程中,我们使用一个字符数组`word`来存储当前遍历到的单词,`level`表示当前遍历到的层数(即单词的长度)。如果当前节点为单词结尾,我们就将`word`数组中的字符打印出来。最后,我们递归遍历当前节点的所有子节点,并将当前节点的字符加入`word`数组中,继续向下遍历。 需要注意的是,如果要生成单词字典序,我们需要在遍历Trie时按照字典序的顺序遍历子节点,即先遍历a、b、c……等字母节点,再遍历下一个节点。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我不是写算法我在创造世界

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值