 ### C++ Trie Cost of Data

A problem on Trie: Cost of Data

Strings can be efficiently stored as a data structure, to have efficient searching methods.
A new startup is going to use this method, but they don't have much space. So they want to check beforehand how much memory will be required for their data.
Following method describes the way in which this startup's engineers save the data in the structure.

Suppose they have 5 strings/words: et,eq,bd,be,bdp
A empty node is kept $NULL$, and strings are inserted into the structure one by one.
Initially the structure is this:

NULL

After inserting "et", the structure is this:

NULL
|
e
|
t

After inserting "eq", the structure is this:

   NULL
/
e
/ \
t   q

After inserting "bd", the structure is this:

   NULL
/  \
e    b
/ \    \
t   q    d

After inserting "be", the structure is this:

     NULL
/     \
e       b
/ \     / \
t   q   e   d

After inserting "bdp", the structure is this:

    NULL
/     \
e       b
/ \     / \
t   q   e   d
\
p

Therefore a total of 8 nodes are used here.

Input:
First line contains N, the number of strings they have.
Each of the next N lines contain one string. Each string consists only of lowercase letters.

Output:
Print the required number of nodes.

Below is C++ implementation of above program

#include<bits/stdc++.h>
#include<iostream>
using namespace std;
const int alpha=26;

struct node{
node* child[alpha];
bool end;
int countchild;
};
node* getnode(){

node* pnode=new node;
pnode->end=false;
pnode->countchild=0;
for(int i=0;i<alpha;i++){
pnode->child[i]=NULL;
}
return pnode;
}
void insert(node* root,string key,int& count){
node* pcrawl=root;
int size=key.length();
for(int i=0;i<size;i++){
int index=key[i]-'a';
if(!pcrawl->child[index]){
pcrawl->child[index]=getnode();
count+=1;
}
pcrawl->countchild+=1;
pcrawl=pcrawl->child[index];
}
pcrawl->end=true;
}

bool search(node* root,string key){
node* pcrawl=root;
int size=key.length();
for(int i=0;i<size;i++){
int index=key[i]-'a';
if(!pcrawl->child[index]){
return false;
}
pcrawl=pcrawl->child[index];
}
return (pcrawl!=NULL and pcrawl->end);
}
int searchchild(node* root,string key){
node* pcrawl=root;
int size=key.length();
for(int i=0;i<size;i++){
int index=key[i]-'a';
if(!pcrawl->child[index]){
return 0;
}
pcrawl=pcrawl->child[index];
}
if (pcrawl==NULL){
return 0;
}
else{
return pcrawl->countchild;
}
}
int main(){
int n;
string s,s1,s2;
int count=1;
node* root=getnode();
cin>>n;
getline(cin,s);
for(int i=0;i<n;i++){
getline(cin,s);
insert(root,s,count);
}
cout<<"No of nodes="<<count<<endl;
}
----------------------------------------
Sample input and output:
5
et
eq
bd
be
bdq
No of nodes=8 #### More Articles of M Mounika:

Name Views Likes
C++ Segmented Sieve (Print Primes In a Range) 162 0
C++ Sieve Of Erastosthenes 135 0
C++ Gold Mine Problem 295 0
C++ Merge K Sorted Arrays 117 0
C++ K Centers Problem 240 0
C++ Find Nth Catalan Number 311 0
C++ Inplace Rotate square matrix by 90 degrees 286 0
C++ Find Non Repeating Elements in Array 87 0
C++ Merge Two Binary Trees 121 0
C++ Sum of Numbers From Root To Leaf Paths 89 0
C++ Meta Strings 91 0
C++ Flood Fill Algorithm 402 0
C++ smallest substring with maximum distinct characters 200 0
C++ Smallest window with all characters in string 94 0
C++ Minimum Removal of Characters from string to make its permutation as palindrome 87 0
C++ Minimum characters added at front of string in palindrome conversion 69 0
C++ Number of Bracket Reversals needed to make expression Balanced 72 0
C++ String to Palindrome with Append Function 83 0
C++ WildCard pattern matching 76 0
C++ Anagram substring Search 72 0
C++ Manachars Algorithm 74 0
C++ Search String in Grid 83 0
C++ String Matching(Z Algorithm) 67 0
C++ String Matching(Naive Algorithm) 113 0
C++ String Matching(KMP Algorithm) 140 0
C++ Remove Duplicates From String 110 0
C++ Basics of String Manipulation 85 1
C++ Disjoint Data Structure Cycle Detection 87 0
C++ Problem On Disjoint Data Structures 95 0
C++ Disjoint Data Structures Part3 79 0
Disjoint Data Structures Part2 90 0
Disjoint Data Structures 93 1
C++ Segment Trees 321 2
C++ Trie Cost of Data 291 1
C++ Trie Datastructure 279 1
C++ Greedy Approach Minimum number of coins 526 0
C++ Greedy Approach Maximum height Pyramid 329 1
C++ Greedy Approach String lexicographically largest subsequence 247 0
C++ Greedy Approach Lexicographically largest subsequence 364 0
C++ Greedy Approach Prims MST 398 1
C++ Greedy Approach Krushkals MST 458 1
C++ Greedy Approach N-array maximum sum 334 1
C++ Greedy Approach Policemen Catch Thieves 563 1
C++ Greedy Approach Maximum product Subset 546 1
C++ Greedy Approach Minimum Product Subset 349 1
C++ Greedy Approach Fractional Knapsack 737 1
C++ Greedy Approach-Activity Selection Problem 745 1
C++ Greedy Approach-Egyptian Fractions 640 0
C++ Greedy Approach-Huffman Codes 1031 1
C++ Introduction to Greedy Approach 955 2