Commit f1bc946d authored by Evan Griffiths's avatar Evan Griffiths

compressTrie function now works :DDDDD

parent 1252ddb4
No preview for this file type
#include "Trie.hpp"
#include "CompressedTrie.hpp"
CompressedTrie* const CompressedTrie::NIL = new CompressedTrie();
CompressedTrie::CompressedTrie() {
}
CompressedTrie *CompressedTrie::compressTrie(Trie *t) {
return NULL;
return _compressTrie(t);
}
CompressedTrie *CompressedTrie::_compressTrie(Trie *t) {
CompressedTrie* result = new CompressedTrie();
for (size_t i = 0; i < 27; i++)
{
if (t->children[i] == NULL)
continue;
std::string prefix (1, (char)((int)'a' + i));
Trie* node = t->children[i];
while (true) {
std::vector<int> indexes = node->getChildren();
if (indexes.size() == 0) {
result->children[prefix] = CompressedTrie::NIL;
break;
}
if (indexes.size() > 1) {
result->children[prefix] = _compressTrie(node);
break;
}
prefix += (char)((int)'a' + indexes[0]);
node = node->children[indexes[0]];
}
}
return result;
}
bool CompressedTrie::query(std::string s) {
......
......@@ -3,14 +3,17 @@
#include <map>
#include <string>
#include <vector>
#include "Trie.hpp"
class CompressedTrie {
public:
static CompressedTrie* const NIL;
std::map<std::string, CompressedTrie *> children;
CompressedTrie();
static CompressedTrie *compressTrie(Trie *);
static CompressedTrie *_compressTrie(Trie *);
bool query(std::string);
void insert(std::string);
};
......
#include "Trie.hpp"
#include <iostream>
#include <string>
#include <cctype>
Trie* const Trie::NIL = new Trie();
Trie::Trie() {
children = new Trie*[27];
#include "Trie.hpp"
#include <iostream>
#include <string>
#include <cctype>
Trie* const Trie::NIL = new Trie();
Trie::Trie() {
children = new Trie*[27];
for (size_t i = 0; i < 27; i++)
{
children[i] = NULL;
}
}
void Trie::insert(std::string s) {
if (s == "") {
//std::cout << "End" << std::endl;
children[26] = NIL;
return;
}
int i = index(s);
//std::cout << i << " is null? " << (children[i] == NULL) << ", " << s[0] << std::endl;
if (children[i] == NULL) {
children[i] = new Trie();
}
children[i]->insert(s.substr(1));
}
bool Trie::query(std::string s) {
if (s == "")
return children[26] == NIL;
}
}
void Trie::insert(std::string s) {
if (s == "") {
//std::cout << "End" << std::endl;
children[26] = NIL;
return;
}
int i = index(s);
//std::cout << i << " is null? " << (children[i] == NULL) << ", " << s[0] << std::endl;
if (children[i] == NULL) {
children[i] = new Trie();
}
children[i]->insert(s.substr(1));
}
bool Trie::query(std::string s) {
if (s == "")
return children[26] == NIL;
int i = index(s);
if (children[i] == NULL) {
return false;
}
return children[i]->query(s.substr(1));
}
int Trie::index(std::string s) {
int index = std::tolower(s[0]);
return index - 97;
}
std::vector<int> Trie::getChildren() {
std::vector<int> result;
int i = index(s);
if (children[i] == NULL) {
return false;
for (size_t i = 0; i < 27; i++)
{
if (children[i] != NULL)
result.push_back(i);
}
return children[i]->query(s.substr(1));
}
int Trie::index(std::string s) {
int index = std::tolower(s[0]);
return index - 97;
}
return result;
}
......@@ -2,6 +2,7 @@
#define TRIE_HPP
#include <string>
#include <vector>
class Trie {
public:
......@@ -11,6 +12,7 @@ public:
void insert(std::string);
bool query(std::string);
int index(std::string);
std::vector<int> getChildren();
};
#endif
No preview for this file type
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment