zoukankan      html  css  js  c++  java
  • 一个简单的Trie树实现

    header file

    #ifndef TRIE_H_INCLUDED
    #define TRIE_H_INCLUDED
    
    #include<stdio.h>
    #include<malloc.h>
    
    typedef struct trie
    {
        int words;
        int prefixes;
        struct trie *edges[26];
    } trie;
    
    trie * initialize(trie *node);
    trie * addWord(trie *ver,char *str);
    int countWords(trie *ver,char *str);
    int countPrefix(trie *ver,char *str);
    
    #endif // TRIE_H_INCLUDED
    

    source file

    #include "trie.h"
    
    trie * initialize(trie *node)
    {
        if(node==NULL)
        {
            node=(trie *)malloc(sizeof(trie));
            node->words=0;
            node->prefixes=0;
            int i;
            for(i=0; i<26; i++)
                node->edges[i]=NULL;
            return node;
        }
    
    }
    
    trie * addWord(trie *ver,char *str)
    {
        printf("%d --- %c  ",ver->words, str[0]);
        if(str[0]=='')
        {
            ver->words=ver->words+1;
        }
        else
        {
    
            ver->prefixes=(ver->prefixes)+1;
            char k;
            k=str[0];
            str++;
            int index=k-'a';
            if(ver->edges[index]==NULL)
            {
                ver->edges[index]=initialize(ver->edges[index]);
            }
    
            ver->edges[index]=addWord(ver->edges[index],str);
        }
        return ver;
    }
    
    int countWords(trie *ver,char *str)
    {
        if(str[0]=='')
            return ver->words;
        else
        {
            int k=str[0]-'a';
            str++;
            if(ver->edges[k]==NULL)
                return 0;
            return countWords(ver->edges[k],str);
        }
    }
    
    int countPrefix(trie *ver,char *str)
    {
        if(str[0]=='')
            return ver->prefixes;
        else
        {
            int k=str[0]-'a';
            str++;
            if(ver->edges[k]==NULL)
                return 0;
            return countPrefix(ver->edges[k],str);
        }
    }
    

    test file


    #include<stdio.h>
    #include<malloc.h>
    #include "trie.h"
    
    void testTrie()
    {
        trie *start=NULL;
        start=initialize(start);
        int ch=1;
        while(ch)
        {
    
            printf("
     1. Insert a word ");
            printf("
     2. Count words");
            printf("
     3. Count prefixes");
            printf("
     0. Exit
    ");
            printf("
    Enter your choice: ");
            scanf("%d",&ch);
            char input[1000];
            switch(ch)
            {
            case 1:
                printf("
    Enter a word to insert: ");
                scanf("%s",input);
                start=addWord(start,input);
                break;
            case 2:
                printf("
    Enter a word to count words: ");
                scanf("%s",input);
                printf("
    %d",countWords(start,input));
                break;
            case 3:
                printf("
    Enter a word to count prefixes: ");
                scanf("%s",input);
                printf("
    %d",countPrefix(start,input));
                break;
            }
        }
    }
    
    int main()
    {
        testTrie();
        return 0;
    }
    


  • 相关阅读:
    iOS-深入理解(转载)
    iOS开发
    夜光遥感
    希尔伯特曲线在地图图像分割中的应用
    希尔伯特曲线
    NLP生成论文
    MapGIS SDK(C++)【基础篇】
    从npm到vue和nodejs
    分形在遥感和GIS中的应用
    MapReduce、Hadoop、PostgreSQL、Spark
  • 原文地址:https://www.cnblogs.com/java20130722/p/3206779.html
Copyright © 2011-2022 走看看