[数据结构]Trie
Posted yccy1230
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[数据结构]Trie相关的知识,希望对你有一定的参考价值。
//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;
以上是关于[数据结构]Trie的主要内容,如果未能解决你的问题,请参考以下文章