tag:blogger.com,1999:blog-58545873721236450642024-03-13T10:45:00.235-07:00Akram's BlogWadhttp://www.blogger.com/profile/06179863584049473890noreply@blogger.comBlogger2125tag:blogger.com,1999:blog-5854587372123645064.post-26430829922975045362015-04-13T14:26:00.003-07:002015-04-13T14:26:20.561-07:00Trie Insert and Search<div dir="ltr" style="text-align: left;" trbidi="on">
#include<iostream><br />
#include<string.h><br />
<br />
#define CHAR_TO_INDEX(c) int(c)-int('a')<br />
<br />
<br />
using namespace std;<br />
<br />
struct trie_node<br />
{<br />
int value;<br />
trie_node* children[26];<br />
};<br />
<br />
<br />
class trie<br />
{<br />
public:<br />
trie_node * root;<br />
int count;<br />
trie()<br />
{<br />
root = new trie_node();<br />
root->value = 0;<br />
<br />
for(int i=0; i < 26; i++)<br />
root->children[i] = NULL;<br />
}<br />
<br />
void insert_node(char key[]);<br />
trie_node* getNode();<br />
bool search(char key[]);<br />
<br />
};<br />
<br />
trie_node* trie::getNode()<br />
{<br />
trie_node * root;<br />
root = new trie_node();<br />
root->value = 0;<br />
for(int i=0; i < 26; i++)<br />
root->children[i] = NULL;<br />
<br />
return root;<br />
<br />
}<br />
<br />
void trie::insert_node(char key[])<br />
{<br />
int len = strlen(key);<br />
trie_node* pcrawl;<br />
count++;<br />
pcrawl = root;<br />
int index;<br />
for(int i = 0; i < len; i++)<br />
{<br />
index = CHAR_TO_INDEX(key[i]);<br />
<br />
if(pcrawl->children[index] == NULL)<br />
{<br />
pcrawl->children[index] = getNode();<br />
<br />
}<br />
pcrawl = pcrawl->children[index];<br />
<br />
}<br />
pcrawl->value = count;<br />
<br />
<br />
}<br />
<br />
bool trie::search(char key[])<br />
{<br />
trie_node *pcrwal = root;<br />
int len = strlen(key);<br />
int index;<br />
int i;<br />
<br />
for(i = 0; i<len; i++)<br />
{<br />
index = CHAR_TO_INDEX(key[i]);<br />
if(pcrwal->children[index] != NULL)<br />
{<br />
pcrwal = pcrwal->children[index];<br />
}<br />
else<br />
{<br />
break;<br />
}<br />
<br />
}<br />
<br />
// just check wheather we have find the pattern in trie<br />
<br />
if (i<len)<br />
return false;<br />
else<br />
if(pcrwal->value > 0)<br />
return true;<br />
else<br />
return false;<br />
<br />
}<br />
<br />
<br />
<br />
int main()<br />
{<br />
// Input keys (use only 'a' through 'z' and lower case)<br />
char keys[][8] = {"the", "a", "there", "answer", "any", "by", "bye", "their"};<br />
char output[][32] = {"Not present in trie", "Present in trie"};<br />
<br />
trie* mytrie = new trie();<br />
mytrie ->insert_node("wasim");<br />
mytrie ->insert_node("warish");<br />
mytrie ->insert_node("koser");<br />
<br />
if(mytrie->search("wasim"))<br />
cout<<"Found";<br />
else<br />
cout<<"Not Found";<br />
<br />
<br />
return 0;<br />
}<br />
<div>
<br /></div>
</div>
Wadhttp://www.blogger.com/profile/06179863584049473890noreply@blogger.com0tag:blogger.com,1999:blog-5854587372123645064.post-73570599397027968812014-08-10T22:44:00.003-07:002014-08-10T22:44:54.740-07:00Python it!!!!<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-family: 'Times New Roman', serif; font-size: 12pt;"><a href="https://docs.python.org/3/tutorial/index.html">https://docs.python.org/3/tutorial/index.html</a><br />
<br />
<a href="http://www.swaroopch.com/notes/python/">http://www.swaroopch.com/notes/python/</a><br />
<br />
<a href="http://www.greenteapress.com/thinkpython/">http://www.greenteapress.com/thinkpython/</a>
<br />
<br />
<a href="https://docs.python.org/3/">https://docs.python.org/3/</a><br />
<br />
<a href="http://www.diveintopython.net/">http://www.diveintopython.net/</a></span></div>
Wadhttp://www.blogger.com/profile/06179863584049473890noreply@blogger.com0