//trie.h
#pragma once
#define ALPHABET_SIZE 26
struct Trie_Node
{
int value;
Trie_Node * childs[ALPHABET_SIZE];
};
class Trie
{
private:
Trie_Node *root;
int count;
Trie_Node* CreateNode();
public:
Trie(void);
~Trie(void);
int Search(const char key[]);
void Insert(const char key[]);
};
//trie.cpp
#include "stdafx.h"
#include "Trie.h"
#include
#include
#define CHARTOINDEX(c) ((int)c - (int)'a')
using namespace std;
Trie::Trie(void)
{
this->root = CreateNode();
this->count = 0;
}
Trie::~Trie(void)
{
}
Trie_Node* Trie::CreateNode()
{
Trie_Node* newnode = new Trie_Node();
if(newnode)
{
newnode->value = 0;
for(int i = 0; i < ALPHABET_SIZE; i++) newnode->childs[i] = nullptr;
}
return newnode;
}
void Trie::Insert(const char key[])
{
std::locale loc;
int length = strlen(key);
int index = 0;
char lower;
this->count++;
Trie_Node* pCrawl = this->root;
for(int level = 0; level < length; level++) { lower = std::islower(key[level], loc) ? key[level] : _tolower(key[level]); index = CHARTOINDEX(lower); //filtering any non alphabetic character. if( index < 0 || index > 26)
continue;
if(!pCrawl->childs[index])
{
pCrawl->childs[index] = CreateNode();
}
pCrawl = pCrawl->childs[index];
}
//mark last node as leaf by assigning value
pCrawl->value = this->count;
}
int Trie::Search(const char key[])
{
std::locale loc;
int length = strlen(key);
int index = 0;
char lower ;
Trie_Node* pCrawl = this->root;
for(int level = 0; level < length; level++) { lower = std::islower(key[level], loc) ? key[level] : _tolower(key[level]); index = CHARTOINDEX(lower); //filtering any non alphabetic character. if( index < 0 || index > 26)
return 0;
if(!pCrawl->childs[index])
return 0;
pCrawl = pCrawl->childs[index];
}
return (nullptr != pCrawl && pCrawl->value);
}
//main.cpp
#include "Trie.h"
#include
#include
#include
#include
#include
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
char output[][32] = {"not present in trie", "present in trie"};
Trie* mytrie = new Trie();
string temp;
while(1)
{
cout << "Enter the word or sentence in the trie.....'0' to exit " << endl; getline(cin, temp); if(temp.length() == 1) { if( '0' == temp[0]) break; } istringstream iss(temp); vector tokens;
copy(istream_iterator(iss),
istream_iterator(),
back_inserter(tokens));
for(auto word : tokens)
mytrie->Insert(word.c_str());
}
while(1)
{
cout << "Enter the word to search in the trie.....'0' to exit " << endl; getline(cin, temp); if(temp.length() == 1) { if( '0' == temp[0]) break; } cout << "searching '" << temp.c_str() <<"' --------" << output[mytrie->Search(temp.c_str())] << endl; } getchar(); return 0; }
#pragma once
#define ALPHABET_SIZE 26
struct Trie_Node
{
int value;
Trie_Node * childs[ALPHABET_SIZE];
};
class Trie
{
private:
Trie_Node *root;
int count;
Trie_Node* CreateNode();
public:
Trie(void);
~Trie(void);
int Search(const char key[]);
void Insert(const char key[]);
};
//trie.cpp
#include "stdafx.h"
#include "Trie.h"
#include
#include
#define CHARTOINDEX(c) ((int)c - (int)'a')
using namespace std;
Trie::Trie(void)
{
this->root = CreateNode();
this->count = 0;
}
Trie::~Trie(void)
{
}
Trie_Node* Trie::CreateNode()
{
Trie_Node* newnode = new Trie_Node();
if(newnode)
{
newnode->value = 0;
for(int i = 0; i < ALPHABET_SIZE; i++) newnode->childs[i] = nullptr;
}
return newnode;
}
void Trie::Insert(const char key[])
{
std::locale loc;
int length = strlen(key);
int index = 0;
char lower;
this->count++;
Trie_Node* pCrawl = this->root;
for(int level = 0; level < length; level++) { lower = std::islower(key[level], loc) ? key[level] : _tolower(key[level]); index = CHARTOINDEX(lower); //filtering any non alphabetic character. if( index < 0 || index > 26)
continue;
if(!pCrawl->childs[index])
{
pCrawl->childs[index] = CreateNode();
}
pCrawl = pCrawl->childs[index];
}
//mark last node as leaf by assigning value
pCrawl->value = this->count;
}
int Trie::Search(const char key[])
{
std::locale loc;
int length = strlen(key);
int index = 0;
char lower ;
Trie_Node* pCrawl = this->root;
for(int level = 0; level < length; level++) { lower = std::islower(key[level], loc) ? key[level] : _tolower(key[level]); index = CHARTOINDEX(lower); //filtering any non alphabetic character. if( index < 0 || index > 26)
return 0;
if(!pCrawl->childs[index])
return 0;
pCrawl = pCrawl->childs[index];
}
return (nullptr != pCrawl && pCrawl->value);
}
//main.cpp
#include "Trie.h"
#include
#include
#include
#include
#include
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
char output[][32] = {"not present in trie", "present in trie"};
Trie* mytrie = new Trie();
string temp;
while(1)
{
cout << "Enter the word or sentence in the trie.....'0' to exit " << endl; getline(cin, temp); if(temp.length() == 1) { if( '0' == temp[0]) break; } istringstream iss(temp); vector
copy(istream_iterator
istream_iterator
back_inserter(tokens));
for(auto word : tokens)
mytrie->Insert(word.c_str());
}
while(1)
{
cout << "Enter the word to search in the trie.....'0' to exit " << endl; getline(cin, temp); if(temp.length() == 1) { if( '0' == temp[0]) break; } cout << "searching '" << temp.c_str() <<"' --------" << output[mytrie->Search(temp.c_str())] << endl; } getchar(); return 0; }
No comments:
Post a Comment