没啥事用C语言写一个Trie tree玩玩,支持中英文,用g++编译通过
来源:互联网 发布:淘宝搜白夜追凶视频 编辑:程序博客网 时间:2024/06/08 03:56
#include <cstdio>#include <cstdlib>#include <vector>#define ALPHABETS 2600000#define CASE 0#define MAX_WORD_SIZE 25using namespace std;struct node{ struct node *parent; struct node *children[ALPHABETS]; vector<int> occurrences;};int IsGB(char *pText){ unsigned char sqChar[200]; sqChar[0] = *pText; if (sqChar[0] >= 0xa1) if (sqChar[0] == 0xa3) return 1; //全角字符 else return 2; //汉字 else return 0; //英文、数字、英文标点}void insertWord(struct node *trieTree, char *word, int index){ struct node *traverse = trieTree; while (*word != '\0') { if (IsGB(word)!=1) { if (traverse->children[2600000-abs(*word)] == NULL) { traverse->children[2600000-abs(*word)] = (struct node *)calloc(2, sizeof(struct node)); traverse->children[2600000-abs(*word)]->parent = traverse; // Assigning parent } traverse = traverse->children[2600000-abs(*word)]; ++word; ++word; } else { if (traverse->children[*word - CASE] == NULL) { traverse->children[*word - CASE] = (struct node *)calloc(2, sizeof(struct node)); traverse->children[*word - CASE]->parent = traverse; // Assigning parent } traverse = traverse->children[*word - CASE]; ++word; } } traverse->occurrences.push_back(index); }struct node *searchWord(struct node *treeNode, char *word){ while (*word != '\0') { if (IsGB(word)!=1) { if (treeNode->children[2600000-abs(*word)] != NULL) { treeNode = treeNode->children[2600000-abs(*word)]; ++word; ++word; } else { break; } } else { if (treeNode->children[*word - CASE] != NULL) { treeNode = treeNode->children[*word - CASE]; ++word; } else { break; } } } if (*word == '\0' && treeNode->occurrences.size() != 0) { printf("Word found"); return treeNode; } else { // Word not found printf("Word not found"); return NULL; }}void removeWord(struct node *trieTree, char *word){ struct node *trieNode = searchWord(trieTree, word); if (trieNode == NULL) { return; } trieNode->occurrences.pop_back(); bool noChild = true; int childCount = 0; int i; for (i = 0; i < ALPHABETS; ++i) { if (trieNode->children[i] != NULL) { noChild = false; ++childCount; } } if (!noChild) { return; } struct node *parentNode; while (trieNode->occurrences.size() == 0 && trieNode->parent != NULL && childCount == 0) { childCount = 0; parentNode = trieNode->parent; for (i = 0; i < ALPHABETS; ++i) { if (parentNode->children[i] != NULL) { if (trieNode == parentNode->children[i]) { parentNode->children[i] = NULL; free(trieNode); trieNode = parentNode; } else { ++childCount; } } } }}void lexicographicalPrint(struct node *trieTree, vector<char> word){ int i; bool noChild = true; if (trieTree->occurrences.size() != 0) { vector<char>::iterator charItr = word.begin(); while (charItr != word.end()) { printf("%c", *charItr); ++charItr; } printf(" -> @ index -> "); vector<int>::iterator counter = trieTree->occurrences.begin(); while (counter != trieTree->occurrences.end()) { printf("%d, ", *counter); ++counter; } printf("\n"); } for (i = 0; i < ALPHABETS; ++i) { if (trieTree->children[i] != NULL) { noChild = false; word.push_back(CASE + i); lexicographicalPrint(trieTree->children[i], word); word.pop_back(); } }}int main(){ int n, i; vector<char> printUtil; struct node *trieTree = (struct node *)calloc(1, sizeof(struct node)); char word[MAX_WORD_SIZE]; printf("Enter the number of words-\n"); scanf("%d", &n); for (i = 1; i <= n; ++i) { scanf("%s", word); insertWord(trieTree, word, i); } lexicographicalPrint(trieTree, printUtil); // scanf("%s", word); // removeWord(trieTree, word); // printf("\n"); // // lexicographicalPrint(trieTree, printUtil); printf("\nEnter the Word to be search - "); scanf("%s", word); searchWord(trieTree, word); return 0;}
编译:g++ tr.cpp -o tr.exe
阅读全文
0 0
- 没啥事用C语言写一个Trie tree玩玩,支持中英文,用g++编译通过
- 写一个只能C编译通过,而C++不能编译通过的函数。
- 【用C语言写一个内存管理程序】
- 用C语言写的一个万年历
- 用C语言写一个的计算器
- 用C语言写一个状态机
- 用C语言写一个订座系统
- 用c语言写一个网络爬虫
- 用c语言写 成一个vector
- 一个JavaScript WEB日历控件,支持IE6,FireFox,可支持不同语言版本,目前支持中英文。
- 【C语言】g++常用编译选项备忘
- C语言写一个万年历
- Trie (prefix tree) 实现 (C++)
- 编译时成病毒了 写一个C语言程序 谁告诉我是为什么啊
- 通过GCC编译器编译c语言
- 用C语言写一个类似于C++的DIB类
- 一个女生用C语言写的爱情函数
- 用C语言写一个简单的通讯录
- iOS 更改状态栏、导航栏颜色的几种方法
- okhttp简单封装
- 大数据测试方法
- 原创文章test
- sqlserver游标使用
- 没啥事用C语言写一个Trie tree玩玩,支持中英文,用g++编译通过
- czl蒻蒟的OI之路14、15
- Laravel 调用 个推 对Android和IOS进行推送
- Linux新用户没有sudo权限
- python进程池的使用
- 身为运维的你,怎么掌握python才不会失业
- 索引
- Target is not a JDK root. System library was not found. Eclipse Oxygen 4.7 + Java9 error
- 中国程序员如何升职加薪,也许我们该学学印度人