[数据结构]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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值