TrieTree.h 1.4 KB
Newer Older
邹晓航 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#ifndef _TRIE_TREE_H_
#define _TRIE_TREE_H_

#include "String.h"
#include "Vector.h"

#include <utility>
#include <memory>
#include <map>
#include <iostream>

namespace TinySTL{
	class trie_tree{
	private:
		struct trie_node{
			char data;
			bool is_a_word;
			std::map<char, std::shared_ptr<trie_node>> map_childs;
邹晓航 已提交
19
			trie_node() :data('\0'), is_a_word(false){}
邹晓航 已提交
20
		};
邹晓航 已提交
21 22 23
	public:
		typedef string value_type;
		typedef size_t size_type;
邹晓航 已提交
24 25
	private:
		trie_node *data;
邹晓航 已提交
26
		size_type size_;
邹晓航 已提交
27 28 29 30 31 32
	public:
		trie_tree();
		~trie_tree();
		trie_tree(const trie_tree&) = delete;
		trie_tree& operator = (const trie_tree&) = delete;

邹晓航 已提交
33 34 35
		bool empty()const;
		size_type size()const;

邹晓航 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
		vector<string> get_word_by_prefix(const string& prefix)const;
		void print_tree(std::ostream& os = std::cout)const;
		bool insert(const string& word);
		bool is_existed(const string& word)const;
	private:
		inline trie_node* get_root()const{ return data; }
		void _get_word_by_prefix(const string& prefix, std::shared_ptr<trie_node> sp, const string& real_prefix, vector<string>& words)const;
		void __get_word_by_prefix(std::shared_ptr<trie_node> sp, string& word, const string& prefix, vector<string>& words)const;
		void _print_tree(std::ostream& os, std::shared_ptr<trie_node> sp, string word)const;
		bool _insert(const string& word, std::shared_ptr<trie_node> sp);
		bool _is_existed(const string& word, std::shared_ptr<trie_node> sp)const;
	};// end of trie_tree
}

#endif