[数据结构]Trie
2016-06-04 17:55
507 查看
//Trie.cpp #include "Trie.h" #pragma once Trie_node::Trie_node() { data = NULL; for (int i = 0; i<num_chars; i++) branch[i] = NULL; } Error_code Trie::insert(const Record & new_entry) { Error_code result = success; if (root == NULL) root = new Trie_node; int position = 0; char next_char; Trie_node *location = root; while (location != NULL && (next_char = new_entry.key_letter(position)) != '\0') { int next_position = alphabetic_order(next_char); if (location->branch[next_position] == NULL) location->branch[next_position] = new Trie_node; location = location->branch[next_position]; position++; } if (location->data != NULL) result = duplicate_error; else location->data = new Record(new_entry); return result; } Error_code Trie::trie_search(const Key & target, Record & x) const { int position = 0; char next_char; Trie_node *location = root; while (location != NULL && (next_char = target.key_letter(position)) != '\0') { location = location->branch[alphabetic_order(next_char)]; position++; } if (location != NULL && location->data != NULL) { x = *(location->data); return success; } else return not_present; } Trie::Trie() { root = NULL; } int alphabetic_order(char c) { if (c == ' ' || c == '\0') return 0; if ('a' <= c && c <= 'z') return c - 'a' + 1; if ('A' <= c && c <= 'Z') return c - 'A' + 1; return 27; }
//Trie.h #include "Record.h" const int num_chars = 28; enum Error_code { not_present, overflow, underflow, duplicate_error, success }; struct Trie_node { Record * data; Trie_node * branch[num_chars]; Trie_node(); }; class Trie { public: Error_code insert(const Record &new_entry); Error_code trie_search(const Key &target, Record &x) const; Trie(); private: Trie_node *root; }; int alphabetic_order(char c);
//Key #include <iostream> #include<string> using namespace std; const int key_size = 10; class Key { char str[key_size]; public: Key(char s[]); char * the_key() const; char key_letter(int position) const; }; #include "Key.h" Key::Key(char s[]) { for (int i = 0; i <= strlen(s); i++) str[i] = s[i]; } char * Key::the_key() const { return (char *)str; } char Key::key_letter(int position) const { if (position<strlen(str)) return str[position]; else return '\0'; }
//Record #include "Key.h" #include<string> using namespace std; class Record { public: operator Key(); Record(char s[] = "", string con=""); char * the_key() const; char key_letter(int position) const; private: char str[key_size]; string content; }; ostream & operator << (ostream &output, Record &x); #include "Record.h" Record::Record(char s[], string con) { for (int i = 0; i <= strlen(s); i++) str[i] = s[i]; content = con; } Record::operator Key() { Key tmp(str); return tmp; } char Record::key_letter(int position) const { if (position<strlen(str)) return str[position]; else return '\0'; } char * Record::the_key() const { return (char *)str; } ostream & operator << (ostream &output, Record &x) { output << x.the_key(); output << " "; return output; }
相关文章推荐
- 数据结构实验之链表四:有序链表的归并
- [数据结构]B-Tree
- PAT L2-011.玩转二叉树(数据结构,二叉链表)
- 数据结构和算法之数组奇数、偶数分离
- 数据结构实验之链表三:链表的逆置
- 【数据结构】二叉搜索树(增、删、查)的递归与非递归实现
- 数据结构—单链表的实现
- 数据结构实验六排序
- 数组-Kth Largest Element in an Array(找出第K大的数)
- 《数据结构与算法分析——c语言描述》 练习6.32 答案
- 【数据结构】HashTable原理及实现学习总结
- 数据结构(Splay平衡树): [NOI2007] 项链工厂
- Java数据结构源码分析-HashMap
- 算法系列(六)数据结构之表队列和栈
- 数据结构学习笔记之广义表的相关知识点
- 数据结构——队列
- Java数据结构源码分析-前言
- noi2015 d1t2 软件包管理器
- APUE 读书笔记 -----孤儿进程与僵尸进程[总结] +数据结构+C
- java数据结构之链式队列的实现