c语言 trie树,C语言实现Trie树(字典树)的插入查找删除与遍历操作

Trie树,也称作是字典树,是一种哈希树的变种,查询效率较高。Trie树可以用于统计或者排序大量的字符串,比如对一系列字符串按照字典序排序。

字典树是一个多叉树,每一个节点上存储的不是一个字符串,而是一个字符,从根节点到某个节点的路径上的所有字符串联起来就构成了一个字符串。

对于N个字符串,如果其平均长度为M,则建立字典树的算法复杂度为O(M*N),在字典树上查找某个长度为M的字符串的算法复杂度O(M),字典树可以边搜索边建立,因此总的查找算法复杂度为O(M*N)。

C语言实现Trie树(字典树)的插入查找删除与遍历操作如下:/*

* Trie tree algorithm dmo

*/

# include

# include

# include

#define MAXSTRLEN 1024

typedef struct TRIETREENODE

{

int cnt; /*统计各个单词出现的次数 初始化为0*/

struct TRIETREENODE *next[26]; /*假设单词只有字母*/

}TrieTreeNode;

TrieTreeNode *createTrieTreeNode(); /*新建并初始化一个节点*/

int InsertTrieTreeNode(TrieTreeNode *ptrRoot, char *str); /*插入一个单词*/

int DeleteTrieTreeNode(TrieTreeNode *ptrRoot, char *str); /*删除一个单词*/

int SearchTrieTree(TrieTreeNode *ptrRoot, char *str); /*搜索Trie树*/

void TraverseTrieTree(TrieTreeNode *ptrRoot); /*遍历Trie树*/

void DestroyTrieTree(TrieTreeNode *ptrRoot); /*销毁Trie树*/

/*新建并初始化一个节点*/

TrieTreeNode *createTrieTreeNode()

{

TrieTreeNode *treeNode = NULL;

/*申请内存*/

treeNode = (TrieTreeNode*)malloc(sizeof(TrieTreeNode));

if(treeNode == NULL)

{

printf("Error! malloc error.\n");

  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Trie是一种形数据结构,它主要用于字符串的存储和查找Trie的每个节点代表一个字符,根节点不存储任何字符,其他节点代表一个字符串的一部分。在Trie中,如果一个节点的所有子节点都是叶节点,则该节点表示一个完整的字符串。 下面是一段Java代码,实现了一个简单的Trie: ``` class TrieNode { private TrieNode[] children = new TrieNode[26]; private boolean isEnd; public TrieNode() {} public TrieNode[] getChildren() { return children; } public void setEnd() { isEnd = true; } public boolean isEnd() { return isEnd; } } class Trie { private TrieNode root; public Trie() { root = new TrieNode(); } public void insert(String word) { TrieNode node = root; for (int i = 0; i < word.length(); i++) { int j = word.charAt(i) - 'a'; if (node.getChildren()[j] == null) { node.getChildren()[j] = new TrieNode(); } node = node.getChildren()[j]; } node.setEnd(); } public boolean search(String word) { TrieNode node = root; for (int i = 0; i < word.length(); i++) { int j = word.charAt(i) - 'a'; if (node.getChildren()[j] == null) { return false; } node = node.getChildren()[j]; } return node.isEnd(); } public boolean startsWith(String prefix) { TrieNode node = root; for (int i = 0; i < prefix.length(); i++) { int j = prefix.charAt(i) - 'a'; if (node.getChildren()[j] == null) { return false; } node = node.getChildren()[j]; } return true; } } ``` 该代码实现了一个Trie,其中TrieNode ### 回答2: Trie,也叫字典树或前缀,是一种用于高效存储和检索字符串的数据结构。下面是使用Java编写的一个简单Trie的代码示例: ```java class TrieNode { private TrieNode[] children; private boolean isEndOfWord; public TrieNode() { children = new TrieNode[26]; // 26个字母 isEndOfWord = false; } } class Trie { private TrieNode root; public Trie() { root = new TrieNode(); } public void insert(String word) { TrieNode current = root; for (int i = 0; i < word.length(); i++) { char ch = word.charAt(i); int index = ch - 'a'; if (current.children[index] == null) { current.children[index] = new TrieNode(); } current = current.children[index]; } current.isEndOfWord = true; } public boolean search(String word) { TrieNode current = root; for (int i = 0; i < word.length(); i++) { char ch = word.charAt(i); int index = ch - 'a'; if (current.children[index] == null) { return false; } current = current.children[index]; } return current.isEndOfWord; } public boolean startsWith(String prefix) { TrieNode current = root; for (int i = 0; i < prefix.length(); i++) { char ch = prefix.charAt(i); int index = ch - 'a'; if (current.children[index] == null) { return false; } current = current.children[index]; } return true; } } ``` 在上述代码中,我们使用了两个类:TrieNode和TrieTrieNode表示Trie中的每个节点,包含一个26个元素的数组来存储子节点和一个布尔变量isEndOfWord来表示当前节点是否是一个单词的结尾。Trie类是Trie实现,包含了插入、搜索和前缀搜索三个方法。 在插入方法中,我们首先从根节点开始,遍历插入的字符串的每个字符。通过计算字符在字母表中的位置,我们可以将其作为TrieNode数组的索引,以此构造Trie的路径。最后,将叶子节点的isEndOfWord设置为true,表示该路径对应的字符串在Trie中存在。 在搜索方法中,我们同样从根节点开始,遍历待搜索的字符串的每个字符。如果遇到某个字符在Trie当前节点的子节点中不存在,则说明该字符串不存在于Trie中,返回false。如果成功遍历完所有字符,并且叶子节点对应的isEndOfWord为true,则说明该字符串存在于Trie中,返回true。 在前缀搜索方法中,与搜索方法类似,只是在遍历完所有字符后不进行isEndOfWord的判断,始终返回true。这是因为前缀搜索只需要判断给定字符串的前缀是否存在于Trie中,无需判断是否是一个完整单词。 通过上述样例代码,我们可以实现一个简单的Trie,并且能够进行插入、搜索和前缀搜索等操作Trie的优势在于其高效的字符串存储和检索性能,特别适用于需要进行前缀搜索的场景,如自动补全、拼写纠错等应用。 ### 回答3: Trie(也称为字典树或前缀)是一种形数据结构,用于高效地存储和搜索字符串集合。下面是用Java编写的简单Trie代码段: ```java class TrieNode { private TrieNode[] children; private boolean isEndOfWord; public TrieNode() { children = new TrieNode[26]; // 26个小写字母 isEndOfWord = false; } } public class Trie { private TrieNode root; public Trie() { root = new TrieNode(); } public void insert(String word) { TrieNode currentNode = root; for (int i = 0; i < word.length(); i++) { int index = word.charAt(i) - 'a'; if (currentNode.children[index] == null) { currentNode.children[index] = new TrieNode(); } currentNode = currentNode.children[index]; } currentNode.isEndOfWord = true; } public boolean search(String word) { TrieNode currentNode = root; for (int i = 0; i < word.length(); i++) { int index = word.charAt(i) - 'a'; if (currentNode.children[index] == null) { return false; } currentNode = currentNode.children[index]; } return currentNode != null && currentNode.isEndOfWord; } public boolean startsWith(String prefix) { TrieNode currentNode = root; for (int i = 0; i < prefix.length(); i++) { int index = prefix.charAt(i) - 'a'; if (currentNode.children[index] == null) { return false; } currentNode = currentNode.children[index]; } return true; } } ``` 上述代码实现了一个简单的TrieTrie的核心部分是`TrieNode`类,每个节点包含一个长度为26的子节点数组`children`,以及一个`boolean`类型的`isEndOfWord`字段,表示当前节点是否为一个单词的结尾。 `Trie`类是Trie的主要类,主要提供插入、搜索和前缀搜索功能。在插入操作中,我们从根节点开始遍历字符串的每个字符,并根据字符的ASCII值索引到对应的子节点位置。如果当前节点的子节点为空,则创建一个新节点,并将当前节点更新为新节点。插入完成后,我们将最后一个节点的`isEndOfWord`字段设置为`true`,表示一个单词的结尾。 在搜索操作中,我们也是从根节点开始遍历字符串的每个字符,并根据字符的ASCII值索引到对应的子节点位置。如果遍历过程中发现某个字符的子节点为空,则返回`false`,表示找不到对应的单词。最后,我们检查最后一个字符的节点是否为空,并查看其`isEndOfWord`字段是否为`true`,来确定搜索操作的结果。 在前缀搜索操作中,逻辑与搜索操作类似,只是在找到对应的子节点位置时不需要判断`isEndOfWord`字段,只需保证一直存在就行。 通过这样的Trie数据结构,我们可以高效地存储和搜索大量的字符串集合,具有较低的时间复杂度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值