字典树实现

news/2024/8/26 17:19:01 标签: c语言, 数据结构, 字典树

一、字典树

字典树(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

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


http://www.niftyadmin.cn/n/5558314.html

相关文章

Python项目部署到Linux生产环境(uwsgi+python+flask+nginx服务器)

1.安装python 我这里是3.9.5版本 安装依赖&#xff1a; yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make -y 根据自己的需要下载对应的python版本&#xff1a; cd local wget https://www.python.org/ftp…

YOLO--置信度(超详细解读)

YOLO&#xff08;You Only Look Once&#xff09;算法中的置信度&#xff08;Confidence&#xff09;是一个关键概念&#xff0c;用于评估模型对预测框内存在目标对象的信心程度以及预测框对目标对象位置的准确性。 一、置信度的定义 数值范围&#xff1a;置信度是一个介于0和…

数学基础【俗说矩阵】:齐次线性方程和非齐次线性方程求解-学习笔记

一、矩阵基础知识 二元一次方程的传统解法 不论是代入消元法还是加减消元法都统称 【高斯消元法】。 齐次方程组和非齐次方程组 线性方程组的解 线性方程的向量展示 向量规则 矩阵的高斯消元和初等行变行及其规则 高斯消元规则 初等行变换 矩阵经初等行变换成阶梯矩阵&…

MySQL 数据库 day 7.16

ok了家人们今天继续记录一下数据库,看看今天学了什么。 一.事物概述 1.1 环境准备 -- 账户表 create table account( id int primary key auto_increment, name varchar(20), money double );insert into account values (null,张三,1000); insert into account values (n…

Windows命令行(CMD)中,tasklist | findstr(搜索并显示包含特定字符串的进程信息)

文章目录 示例注意事项示例&#xff1a;使用 /FI 选项过滤进程 在Windows命令行&#xff08;CMD&#xff09;中&#xff0c; tasklist 命令用于显示当前运行的进程列表&#xff0c;而 findstr 命令则用于搜索字符串。当你将 tasklist 命令的输出通过管道&#xff08; |&…

Postman、Apifox、Apipost用哪个?

Postman、Apifox、Apipost都是流行的API接口管理工具&#xff0c;它们各自具有不同的特点和优势&#xff0c;因此哪个更好用取决于具体的使用场景和需求。以下是对这三个工具的比较分析&#xff1a; 一、Postman 特点与优势&#xff1a; 支持多种请求方式&#xff1a;包括GE…

uni-app开发时自定义导航栏

1. 修改pages.json文件 增加navigationStyle {"path": "pages/index/index","style": {"navigationStyle": "custom","navigationBarTitleText": "首页"}},2. 下载uni-ui扩展组件库 npm install dcl…

Mongodb数组字段索引之多键索引

学习mongodb&#xff0c;体会mongodb的每一个使用细节&#xff0c;欢迎阅读威赞的文章。这是威赞发布的第92篇mongodb技术文章&#xff0c;欢迎浏览本专栏威赞发布的其他文章。如果您认为我的文章对您有帮助或者解决您的问题&#xff0c;欢迎在文章下面点个赞&#xff0c;或者关…