#include< iostream>
#include< stdio.h>
#include< string.h>
#define NOT_FOUND 0
#define FOUND 1
using namespace std;
struct trie_node
{
int words;
int prefixes;
trie_node* child[26];
}*root=NULL;
// typedef so that the name becomes short
typedef struct trie_node tn;
//initialize the trie
void initializeRoot(tn *root)
{
tn* node=new tn;
root=node;
//initializing the trie root node
for(int i=0;i < 26;i++)
root-> child[i]=NULL;
root-> words=0;
root-> prefixes=0;
}
//add a child at character 'c' to a node 'node'
void addNode(tn *node, char c)
{
tn* childNode =new tn;
for(int i=0;i < 26;i++)
childNode->child[i]=NULL;
childNode->words=0;
childNode->prefixes=0;
int index=(int)c-97;
node->child[index]=childNode;
}
int findWord(tn *root, char *word)
{
tn* curr;
curr=root;
int len=strlen(word);
//find length of the word
int i;
for(i=0;i < len;i++)
{
// find the child of the current node depending on the next character
int index = (int)word[i] - 97;
curr=curr->child[index];
// if node exists then the word might exist
if(curr==NULL)
return NOT_FOUND;
}
if(i==len)
return FOUND;
else
return NOT_FOUND;
}
void addWord(tn *root, char *word)
{
int len=strlen(word);
tn* curr=root;
tn* parent=root;
int i;
for(i=0;i < len;i++)
{
// increment the number of prefix with the word till word[i]
curr->prefixes++;
int index= (int)word[i] - 97;
parent=curr;
curr=curr->child[i];
// no child exists for word[i] -- create one
if(curr==NULL)
addNode(parent,word[i]);
// word is finished increment the number of words
if(i==len-1)
curr->words++;
}
}
int countPrefix(tn *root, char *prefix)
{
tn* curr;
curr=root;
int len=strlen(prefix);
int i;
for(i=0;i < len;i++)
{
// find the child of the current node depending on the next character
int index = (int)prefix[i] - 97;
curr=curr->child[index];
// if node exists then the word might exist
if(curr==NULL)
return NOT_FOUND;
}
if(i==len)
return curr->prefixes;
else
return NOT_FOUND;
}
int main()
{
initializeRoot(root);
system("pause");
return 0;
}
Saturday, September 29, 2012
Trie!! it's fun
Monday, August 20, 2012
Quick Sort
#include <iostream>
using namespace std;
int partition(int *a,int p,int r)
{
int x=a[r];
int temp;
int i=p-1;
int j=p;
for(j=p;j<(r);j++)
{
if(a[j]<x)
{
i++;
temp=a[j];
a[j]=a[i];
a[i]=temp;
}
}
temp=a[r];
a[r]=a[i+1];
a[i+1]=temp;
int k=0;
return i+1;
}
void quickSort(int *a,int p,int r)
{
int q;
if(p<r)
{
q=partition (a,p,r);
quickSort(a,p,q-1);
quickSort(a,q+1,r);
}
}
int main()
{
int a[10];
int i=0;
while(i<10)
cin>>a[i++];
quickSort(a,0,9);
i=0;
while(i<10)
cout<<a[i++]<<" ";
return 0;
}
Subscribe to:
Posts (Atom)