zoukankan      html  css  js  c++  java
  • alg_trie: count Word frequency

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
     
    #define TREE_WIDTH 256
     
    #define WORDLENMAX 128
     
    // count Word frequency
     
    struct trie_node_st {
            int count;
            struct trie_node_st *next[TREE_WIDTH];
    };
     
    static struct trie_node_st root={0, {NULL}};
     
    static char *spaces=" \t\n/.\"\'()";
     
    static int
    insert(const char *word)
    {
            int i;
            struct trie_node_st *curr, *newnode;
     
            if (word[0]=='\0') {
                    return 0;
            }
            curr = &root;
            for (i=0; ; ++i) {
                    if (curr->next[ word[i] ] == NULL) {
                            newnode=(struct trie_node_st*)malloc(sizeof(struct trie_node_st));
                            memset(newnode, 0, sizeof(struct trie_node_st));
                            curr->next[ word[i] ] = newnode;
                    }
                    if (word[i] == '\0') {
                            break;
                    }
                    curr = curr->next[ word[i] ];
            }
            curr->count ++;
     
            return 0;
    }
     
    static void
    printword(const char *str, int n)
    {
            printf("%s\t%d\n", str, n);
    }
     
    static int
    do_travel(struct trie_node_st *rootp)
    {
            static char worddump[WORDLENMAX+1];
            static int pos=0;
            int i;
     
            if (rootp == NULL) {
                    return 0;
            }
            if (rootp->count) {
                    worddump[pos]='\0';
                    printword(worddump, rootp->count);
            }
            for (i=0;i<TREE_WIDTH;++i) {
                    worddump[pos++]=i;
                    do_travel(rootp->next[i]);
                    pos--;
            }
            return 0;
    }
     
    int
    main(void)
    {
            char linebuf[200];
            size_t bufsize=0;
            int ret;
     
            while (1) {
                    if (!fgets(linebuf, 200-1, stdin)) {
                            break;
                    }
                    
                   // while (1)
                   {
                            char *word = linebuf;
                            if (word==NULL) {
                                    break;
                            }
                            if (word[0]=='\0') {
                                    continue;
                            }
                            insert(word);
                    }
            }
     
    /* free(linebuf); */
     
            do_travel(&root);
     
            exit(0);
    }
  • 相关阅读:
    转 configure: error: Cannot find ldap.h
    DDoS(Distributed Denial of Service,分布式拒绝服务)
    j2ee 1.5和1.6的区别
    机器学习
    免安装版Tomcat6.0启动方法
    mysql补充(2)常用sql语句
    mysql补充(4)数据完整性
    mysql补充(3)优化sql语句查询常用的30种方法
    mysql补充(1)校对集utf8_unicode_ci与utf8_general_ci
    jdbc(1)(一)
  • 原文地址:https://www.cnblogs.com/cutepig/p/1956897.html
Copyright © 2011-2022 走看看