Skip to content

Commit

Permalink
Trie (#19)
Browse files Browse the repository at this point in the history
* Trie for english words

* Added trie for numbers

* Update
  • Loading branch information
jainaman224 authored Jun 29, 2016
1 parent 073ee38 commit 8dfbf33
Show file tree
Hide file tree
Showing 2 changed files with 306 additions and 0 deletions.
152 changes: 152 additions & 0 deletions Trie_For_Natural_Numbers/Trie.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
#include <iostream>

using namespace std;

struct node
{
node *number[10];
bool isLeaf;
} *root = NULL;

node *getNode()
{
node *temp = new node();
temp -> isLeaf = false;

for(int i = 0; i < 10; i++)
{
temp -> number[i] = NULL;
}

return temp;
}

void insert(int a)
{
if(root == NULL)
root = getNode();

node *current = root;

while(a != 0)
{
if(current -> number[a % 10] == NULL)
{
node *new_node = getNode();
current -> number[a % 10] = new_node;
current = new_node;
}
else
current = current -> number[a % 10];

a /= 10;
}

current -> isLeaf = true;
}

void search(int value)
{
if(root == NULL)
cout << "There is no word in Trie" << endl;

node *current = root;
int a = value;

while(a != 0)
{
if(current -> number[a % 10] == NULL)
{
break;
}
else
current = current -> number[a % 10];

a /= 10;
}

if(a == 0 && current -> isLeaf == true)
cout << value << " Found" << endl;
else
cout << value << " Not Present" << endl;
}

bool isFreeNode(node *root)
{
for(int i = 0; i < 10; i++)
{
if(root -> number[i] != NULL)
return false;
}

return true;
}

bool removeNumber(node *root, int a)
{
if(root)
{
if(a == 0)
{
if(root -> isLeaf)
{
root -> isLeaf = false;

if(isFreeNode(root))
return true;
}

return false;
}
else
{
if(removeNumber(root -> number[a % 10], a / 10))
{
delete root -> number[a % 10];
root -> number[a % 10] = NULL;

return (!root -> isLeaf && isFreeNode(root));
}
}
}

return false;
}

void remove(int a)
{
if(a != 0 && root != NULL)
removeNumber(root, a);
}

int main()
{
int a = 99, b = 9999, c = 9987, d = 8687, e = 5499;

insert(a);
insert(b);
insert(c);
insert(d);
insert(e);

search(a);
search(b);
search(e);
search(7676);

remove(a);

search(a);

return 0;
}

/* Output
99 Found
9999 Found
5499 Found
7676 Not Present
99 Not Present
*/
154 changes: 154 additions & 0 deletions Trie_For_Words/Trie.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
#include <iostream>

using namespace std;

struct node
{
node *alphabet[26];
bool isLeaf;
} *root = NULL;

node *getNode()
{
node *temp = new node();
temp -> isLeaf = false;

for(int i = 0; i < 26; i++)
{
temp -> alphabet[i] = NULL;
}

return temp;
}

void insert(string a)
{
if(root == NULL)
root = getNode();

node *current = root;

for(unsigned int i = 0; i < a.length(); i++)
{
a[i] = tolower(a[i]);

if(current -> alphabet[int(a[i]) - 97] == NULL)
{
node *new_node = getNode();
current -> alphabet[int(a[i]) - 97] = new_node;
current = new_node;
}
else
current = current -> alphabet[int(a[i]) - 97];
}

current -> isLeaf = true;
}

void search(string a)
{
if(root == NULL)
cout << "There is no word in Trie" << endl;

node *current = root;
unsigned int i;

for(i = 0; i < a.length(); i++)
{
a[i] = tolower(a[i]);

if(current -> alphabet[int(a[i] - 'a')] == NULL)
{
break;
}
else
current = current -> alphabet[int(a[i] - 'a')];
}

if(i == a.length() && current -> isLeaf == true)
cout << a << " Found" << endl;
else
cout << a << " Not Present" << endl;
}

bool isFreeNode(node *root)
{
for(int i = 0; i < 26; i++)
{
if(root -> alphabet[i] != NULL)
return false;
}

return true;
}

bool removeWord(node *root, string a, unsigned int i)
{
if(root)
{
if(i == a.length())
{
if(root -> isLeaf)
{
root -> isLeaf = false;

if(isFreeNode(root))
return true;
}

return false;
}
else
{
a[i] = tolower(a[i]);

if(removeWord(root -> alphabet[int(a[i]) - 97], a, i + 1))
{
delete root -> alphabet[int(a[i]) - 97];
root -> alphabet[int(a[i]) - 97] = NULL;

return (!root -> isLeaf && isFreeNode(root));
}
}
}

return false;
}

void remove(string a)
{
if(a.length() && root != NULL)
removeWord(root, a, 0);
}

int main()
{
string a = "aman", b = "akshit", c = "adish", d = "akshay", e = "akash";

insert(a);
insert(b);
insert(c);
insert(d);
insert(e);

search(a);
search(b);
search(e);
search("swati");

remove(a);

search(a);

return 0;
}

/* Output
aman Found
akshit Found
akash Found
swati Not Present
aman Not Present
*/

0 comments on commit 8dfbf33

Please sign in to comment.