字典树--模板

<<字典树模板>>



#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
//字典树的数据结构
struct Trie{
    Trie *child[26];//这里数组的大小看是小写字母还是数字还是都有
    int num;
    Trie(){//初始化
        num = 0;
        memset(child , 0 , sizeof(child));
    }
};
Trie *root;
//字典树的构建
void Tree_Insert(char *str){
    Trie *s = root;
    int i = 0;
    while(str[i]){
        int id = str[i] -'a' ;
        if(s -> child[id] == 0)//如果该字节点的字节点为空则要创建中间节点
            s -> child[id] = new Trie();
        s = s -> child[id];

        s -> num++;

        i++;
    }  
}
//字典树的查找
int Tree_Find(char *str){
    Trie *s = root;
    int count ;
    while(str[i]){
        int id = str[i] - 'a';
        if(s -> child[id] == 0){//如果当前指针s的对应于当前字符str[i]在字母表的位置的子节点为空
            return count;//直接返回0
        }
        else{
            s = s -> child[id];
            count = s -> num ;

        }

        i++;
    }
    return count;
}
int main(){
   int i , j;
   //建立一个root分配空间

   root = new Trie();

  
   return 0;
}
    
    



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值