字典树实现

一、字典树

字典树(Trie树)是一种多叉树结构,每条边代表一个字符,从根节点到其它节点的路径构成一个单词。其具有较好的查询性能,可以用于有效地存储大量字符串,并支持高效的查找、插入和删除操作。

二、代码实现

代码实现:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define ALPHABET_SIZE 26  //字母表长度

// 字典树的节点结构
typedef struct TrieNode {
    struct TrieNode *children[ALPHABET_SIZE];
    int isEndOfWord;
} TrieNode;

// 初始化字典树节点
TrieNode* createNode() {
    TrieNode *node = (TrieNode*)malloc(sizeof(TrieNode));
    node->isEndOfWord = 0;
    for (int i = 0; i < ALPHABET_SIZE; i++) {
        node->children[i] = NULL;
    }
    return node;
}

// 在字典树中插入单词
void insertWord(TrieNode *root, char *word) {
    TrieNode *current = root;
    int len = strlen(word);

    for (int i = 0; i < len; i++) {
        int index = word[i] - 'a';
        if (current->children[index] == NULL) {
            current->children[index] = createNode();
        }
        current = current->children[index];
    }

    current->isEndOfWord = 1;
}

//打印字符串
void printSubstring(const char *str, int start, int length) {  
    for (int i = start; i < start + length && str[i] != '\0'; i++) {  
        putchar(str[i]);  
    } 
    putchar('\n'); 
}  

// 在字符串中查找字典树中的单词
void searchWords(TrieNode *root, char *text) {
    int len = strlen(text);
    TrieNode *current = root;
    int wordLen = 0;

    for (int i = 0; i < len; i++) {
        int index = text[i] - 'a';
        if (current->children[index]) {
            current = current->children[index];
            ++wordLen;
            if (current->isEndOfWord) {
                printf("Word found starting at position: %d, len: %d word is ", i - wordLen + 1, wordLen);
                printSubstring(text, i - wordLen + 1, wordLen); 
            }
        } else {
            current = root;
            wordLen = 0;

        }
    }
}

int main() {
    TrieNode *root = createNode();
    char words[][10] = {"insert", "delete", "update", "select", "create", "drop"};
    int wordsCount = sizeof(words)/sizeof(words[0]);
    char text[] = "deletedeinsertlete";

    // 构建字典树
    for (int i = 0; i < wordsCount; i++) {
        insertWord(root, words[i]);
    }

    // 在文本中查找单词
    searchWords(root, text);

    return 0;
}
//编译 gcc -o tire_tree tire_tree.c

运行结果:
在这里插入图片描述

相关推荐

  1. 字典Trie】LeetCode-208. 实现 Trie (前缀)

    2024-07-17 07:32:02       55 阅读
  2. Tire 字典、前缀

    2024-07-17 07:32:02       29 阅读
  3. 轻松应用字典

    2024-07-17 07:32:02       66 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-17 07:32:02       70 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-17 07:32:02       74 阅读
  3. 在Django里面运行非项目文件

    2024-07-17 07:32:02       62 阅读
  4. Python语言-面向对象

    2024-07-17 07:32:02       72 阅读

热门阅读

  1. Day 10.08函数作业答案·二

    2024-07-17 07:32:02       23 阅读
  2. 面试题 30. 包含 min 函数的栈

    2024-07-17 07:32:02       25 阅读
  3. OpenResty使用Lua笔记

    2024-07-17 07:32:02       26 阅读
  4. Springboot定义阿里云oss工具类

    2024-07-17 07:32:02       25 阅读
  5. 入门 git

    2024-07-17 07:32:02       22 阅读
  6. IPython 的 %history -p 命令:探索命令行历史的秘籍

    2024-07-17 07:32:02       30 阅读
  7. [NOIP2006 提高组] 作业调度方案(含代码)

    2024-07-17 07:32:02       20 阅读
  8. OpenSearch 第三方IoT设备日志分析

    2024-07-17 07:32:02       31 阅读
  9. Photoshop

    Photoshop

    2024-07-17 07:32:02      20 阅读
  10. Github07-16 Python开源项目日报 Top10

    2024-07-17 07:32:02       24 阅读