Trie树之C-实现
title: Trie树之C++实现
comments: true
date: 2016-10-02 16:59:54
categories: 算法
tags:
- Trie树
前言
之前写了一篇偏向于理解Trie的文章-Trie树理解
和前面那篇文章不同的是,这一篇文章记录着Trie树的C++实现;
介绍
看一下代码的结构

其中:
- TrieNode : 树的结点,树
- Client : 客户端(操作树)
- main : 主函数
Code
TrieNode
TrieNode.hpp
//
// TreeNode.hpp
// Trie
//
// Created by staff on 16/9/29.
// Copyright © 2016年 staff. All rights reserved.
//
#ifndef TreeNode_hpp
#define TreeNode_hpp
#include <stdio.h>
#include <iostream>
#include <map>
#include <vector>
#include <stack>
using namespace std;
#define MAX_CHARS 26
class TreeNode {
public:
TreeNode();
~TreeNode();
void setEnd(bool);
bool getEnd();
void setFreq(int);
int getFreq();
void setNChar(char);
char getNChar();
void setID(int);
int getID();
void setStr(string);
string getStr();
vector<TreeNode *> Childs;
private:
bool End;
int Freq;
char NChar;
int Id;
string str;
};
class Tree {
public:
Tree();
~Tree();
void addTrieNode(string word, int id);
void deleteTrieNode();
map<int, string> searchTrie(string word);
void upateTree(string word, int id);
int getWordFreq(string word);
private:
TreeNode * root;
void addTrieNode(TreeNode * node, string word, int id);
void deleteTrieNode(TreeNode * root);
map<int, string> searchTrie(TreeNode * root, string word);
int getPrefixCount(TreeNode * root, string word);
void upateTree(TreeNode * root, string word, int id);
int getWordFreq(TreeNode * root, string word);
};
#endif /* TreeNode_hpp */
TreeNode.cpp
//
// TreeNode.cpp
// Trie
//
// Created by staff on 16/9/29.
// Copyright © 2016年 staff. All rights reserved.
//
#include "TreeNode.hpp"
TreeNode::TreeNode() : End(false), Freq(0), NChar(' '), str("") {
for (int i = 0; i < MAX_CHARS; ++i) {
this->Childs.push_back(nullptr);
}
}
TreeNode::~TreeNode() {
for (auto itr = this->Childs.begin(); itr != this->Childs.end(); itr++) {
delete *itr;
*itr = nullptr;
}
this->Childs.clear();
}
void TreeNode::setEnd(bool value) {
this->End = value;
}
bool TreeNode::getEnd() {
return this->End;
}
void TreeNode::setFreq(int value) {
this->Freq = value;
}
int TreeNode::getFreq() {
return this->Freq;
}
void TreeNode::setNChar(char value) {
this->NChar = value;
}
char TreeNode::getNChar() {
return this->NChar;
}
void TreeNode::setID(int value) {
this->Id = value;
}
int TreeNode::getID() {
return this->Id;
}
void TreeNode::setStr(string value) {
this->str = value;
}
string TreeNode::getStr() {
return this->str;
}
Tree::Tree() {
this->root = new TreeNode();
}
Tree::~Tree() {
}
void Tree::addTrieNode(TreeNode * root, string word, int id) {
if (word.size() == 0) {
return ;
}
TreeNode * pNode = root;
for (int i = 0; i < word.size(); ++i) {
int idx = word[i] - 'a';
if (pNode->Childs[idx] == nullptr) {
pNode->Childs[idx] = new TreeNode();
}
pNode->Childs[idx]->setNChar(word[i]);
pNode->Childs[idx]->setStr(pNode->getStr() + pNode->Childs[idx]->getNChar());
pNode = pNode->Childs[idx];
}
pNode->setID(id);
pNode->setFreq(pNode->getFreq()+1);
pNode->setEnd(true);
}
void Tree::deleteTrieNode(TreeNode * root) {
TreeNode * node = root;
for (int i = 0; i < MAX_CHARS; ++i) {
if (node->Childs[i]) {
deleteTrieNode(node->Childs[i]);
}
}
delete(node);
node = nullptr;
}
map<int, string> Tree::searchTrie(TreeNode * root, string word) {
if (word.size() == 0) {
return {};
}
map<int, string> result;
TreeNode * pNode = root;
int i = 0;
for (; i < word.size(); ++i) {
int idx = word[i] - 'a';
if (pNode->Childs[idx] == nullptr) {
break;
}
pNode = pNode->Childs[idx];
}
if (i != word.size()) {
cout << "not exist!" << endl;
return {};
}
else {
result.insert(make_pair(pNode->getID(), pNode->getStr()));
stack<TreeNode *> stack;
stack.push(pNode);
while (!stack.empty()) {
TreeNode * node = stack.top();
stack.pop();
for (int j = 0; j < MAX_CHARS; ++j) {
if (node->Childs[j]) {
if (node->Childs[j]->getEnd()) {
result.insert(make_pair(node->Childs[j]->getID(), node->Childs[j]->getStr()));
}
else {
stack.push(node->Childs[j]);
}
}
}
}
return result;
}
}
/**
* 词频统计,出现字符串的个数
*
* @param root <#root description#>
* @param word <#word description#>
*
* @return <#return value description#>
*/
int Tree::getWordFreq(TreeNode * root, string word) {
TreeNode * pNode = root;
int i = 0;
for (; i < word.size(); ++i) {
int idx = word[i] - 'a';
if (pNode->Childs[idx] == nullptr) {
return 0;
}
pNode = pNode->Childs[idx];
}
return pNode->getFreq();
}
void Tree::upateTree(TreeNode * root, string word, int id) {
deleteTrieNode(root);
addTrieNode(root, word, id);
}
void Tree::addTrieNode(string word, int id) {
this->addTrieNode(this->root, word, id);
}
void Tree::deleteTrieNode() {
this->deleteTrieNode(this->root);
}
map<int, string> Tree::searchTrie(string word) {
return this->searchTrie(root, word);
}
int Tree::getWordFreq(string word) {
return this->getWordFreq(this->root, word);
}
void Tree::upateTree(string word, int id) {
this->upateTree(this->root, word, id);
}
Client
Client.hpp
//
// Client.hpp
// Trie
//
// Created by staff on 16/9/29.
// Copyright © 2016年 staff. All rights reserved.
//
#ifndef Client_hpp
#define Client_hpp
#include <stdio.h>
#include <iomanip>
#include <fstream>
#include <string>
#include <algorithm>
#include <map>
#include "TreeNode.hpp"
using namespace std;
class Client {
public:
Client();
~Client();
void test();
private:
Tree *tree;
};
#endif /* Client_hpp */
Client.cpp
//
// Client.cpp
// Trie
//
// Created by staff on 16/9/29.
// Copyright © 2016年 staff. All rights reserved.
//
#include "Client.hpp"
Client::Client() {
this->tree = new Tree();
}
Client::~Client() {
}
void Client::test() {
ifstream infile;
infile.open("/Users/George/Desktop/out.txt");
string message;
char buf[1024];
if (infile.is_open()) {
while (infile.good() && !infile.eof()) {
memset(buf, 0, 1024);
infile.getline(buf, 1024);
message = buf;
size_t size = message.length();
size_t space = message.find_first_of(' ');
int id = atoi(message.substr(0, space).c_str());
string word = message.substr(space+1, size-1);
word.erase(word.end()-1);
transform(word.begin(), word.end(), word.begin(), ::tolower);
this->tree->addTrieNode(word, id);
}
char input[20];
printf("please input\n");
scanf("%s", input);
int content_count = this->tree->getWordFreq(input);
printf("content count : %d\n", content_count);
map<int, string> ids = this->tree->searchTrie(input);
int prefix_count = (int)ids.size();
printf("prefix count : %d\n\n", prefix_count);
for (auto id : ids) {
printf("%d:%s\n", id.first, id.second.c_str());
}
}
else {
printf("无法读取文件\n");
}
infile.close();
}
main
//
// main.cpp
// Trie
//
// Created by staff on 16/9/28.
// Copyright © 2016年 staff. All rights reserved.
//
#include <stdio.h>
#include "Client.hpp"
int main(int argc, const char * argv[]) {
// insert code here...
Client * client = new Client();
client->test();
return 0;
}
结果

Trie树之C-实现的更多相关文章
- 基于trie树做一个ac自动机
基于trie树做一个ac自动机 #!/usr/bin/python # -*- coding: utf-8 -*- class Node: def __init__(self): self.value ...
- 基于trie树的具有联想功能的文本编辑器
之前的软件设计与开发实践课程中,自己构思的大作业题目.做的具有核心功能,但是还欠缺边边角角的小功能和持久化数据结构,先放出来,有机会一点点改.github:https://github.com/chu ...
- hihocoder-1014 Trie树
hihocoder 1014 : Trie树 link: https://hihocoder.com/problemset/problem/1014 题意: 实现Trie树,实现对单词的快速统计. # ...
- 洛谷P2412 查单词 [trie树 RMQ]
题目背景 滚粗了的HansBug在收拾旧英语书,然而他发现了什么奇妙的东西. 题目描述 udp2.T3如果遇到相同的字符串,输出后面的 蒟蒻HansBug在一本英语书里面找到了一个单词表,包含N个单词 ...
- 通过trie树实现单词自动补全
/** * 实现单词补全功能 */ #include <stdio.h> #include <stdlib.h> #include <string.h> #incl ...
- #1014 Trie树
本题主要是求构造一棵Trie树,即词典树用于统计单词. C#代码如下: using System; using System.Collections.Generic; using System.Lin ...
- Trie树-字典查找
描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程的学习道路上一同前进. 这一天,他们遇到了一本词典,于是小Hi就向小Ho提出了那个经典的问题: ...
- Trie树的创建、插入、查询的实现
原文:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=28977986&id=3807947 1.什么是Trie树 Tr ...
- Trie树(c++实现)
转:http://www.cnblogs.com/kaituorensheng/p/3602155.html http://blog.csdn.net/insistgogo/article/detai ...
- [转]双数组TRIE树原理
原文名称: An Efficient Digital Search Algorithm by Using a Double-Array Structure 作者: JUN-ICHI AOE 译文: 使 ...
随机推荐
- 【codeforces】940F题解
CF Round #466的最后一题,颇有难度,正解是带修改莫队算法. [题意] 给定一个长度为\(n\)的数组\(a\),并且要求执行\(q\)个操作,有两种不同的操作: ①询问一个区间\([l,r ...
- 【四校联考】【比赛题解】FJ NOIP 四校联考 2017 Round 7
此次比赛为厦门一中出题.都是聚劳,不敢恭维. 莫名爆了个0,究其原因,竟然是快读炸了……很狗,很难受. 话不多说,来看看题: [T1] 题意: 样例: PS:1<=h[i]<=100000 ...
- 脚本病毒分析扫描专题1-VBA代码阅读扫盲、宏病毒分析
1.Office Macor MS office宏的编程语言是Visual Basic For Applications(VBA). 微软在1994年发行的Excel5.0版本中,即具备了VBA的宏功 ...
- 源码安装postgresql数据库
一般情况下,postgresql由非root用户启动. 1.创建postgres用户 groupadd postgres useradd -g postgres postgres 下面的操作都在pos ...
- docker 错误排查:无法进入容器.
docker 错误排查:无法进入容器. #docker exec -it 3c1d bash rpc error: code = 2 desc = oci runtime error: exec fa ...
- Python爬虫之三种网页抓取方法性能比较
下面我们将介绍三种抓取网页数据的方法,首先是正则表达式,然后是流行的 BeautifulSoup 模块,最后是强大的 lxml 模块. 1. 正则表达式 如果你对正则表达式还不熟悉,或是需要一些提 ...
- 练习题 --- 10种Xpath定位
写出10种不同的Xpath定位语法
- cve-2010-3333 Microsoft Office Open XML文件格式转换器栈缓冲区溢出漏洞 分析
用的是泉哥的POC来调的这个漏洞 0x0 漏洞调试 Microsoft Office Open XML文件格式转换器栈缓冲区溢出漏洞 Microsoft Office 是微软发布的非常流行的办公 ...
- android拾遗——四大基本组件介绍与生命周期
Android四大基本组件分别是Activity,Service服务,Content Provider内容提供者,BroadcastReceiver广播接收器. 一:了解四大基本组件 Activity ...
- 流程设计器jQuery + svg/vml(Demo7 - 设计器与引擎及表单一起应用例子)
去年就完成了流程设计器及流程引擎的开发,本想着把流程设计器好好整理一下,形成一个一步一步的开发案例,结果才整理了一点点,发现写文章比写代码还累,加上有事情要忙,结果就.. 明天要去外包驻场了,现把流程 ...