Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 130 additions & 0 deletions Implement Trie – 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
PROBLEM STATEMENT- Implementing insertion, search, and startWith operations in a trie or prefix-tree.
Implementation:

Type 1: To insert a string “word” in Trie.

Type 2: To check if the string “word” is present in Trie or not.

Type 3: To check if there is any string in the Trie that starts with the given prefix string “word”.

Example:
Input: type = {1, 1, 2, 3, 2};
value = {"hello", "help", "help", "hel", "hel"};
Output:
true
true
false
Explanation:
Trie object initialized
“hello” inserted in the trie.
“help” insertedin the trie
“help” searched in the trie //returns true
Checked if any previously inserted word has the prefix “hel” //return true
“hel” searches in the trie //returns true


/**************************************CODE****************************************/

#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define MAXN 100001
#define INF 1e18+1
struct Node {
Node *links[26];
bool flag = false;
//checks if the reference trie is present or not
bool containKey(char ch) {
return (links[ch - 'a'] != NULL);
}
//creating reference trie
void put(char ch, Node *node) {
links[ch - 'a'] = node;
}
//to get the next node for traversal
Node *get(char ch) {
return links[ch - 'a'];
}
//setting flag to true at the end of the word
void setEnd() {
flag = true;
}
//checking if the word is completed or not
bool isEnd() {
return flag;
}
};
class Trie {
private:
Node* root;
public:
Trie() {
//creating new obejct
root = new Node();
}

void insert(string word) {
//initializing dummy node pointing to root initially
Node *node = root;
for (int i = 0; i < word.size(); i++) {
if (!node->containKey(word[i])) {
node->put(word[i], new Node());
}
//moves to reference trie
node = node->get(word[i]);
}
node->setEnd();
}

bool search(string word) {
Node *node = root;
for (int i = 0; i < word.size(); i++) {
if (!node->containKey(word[i])) {
return false;
}
node = node->get(word[i]);
}
return node->isEnd();
}

bool startsWith(string prefix) {
Node* node = root;
for (int i = 0; i < prefix.size(); i++) {
if (!node->containKey(prefix[i])) {
return false;
}
node = node->get(prefix[i]);
}
return true;
}
};

int main()
{
int n = 5;
vector<int>type = {1, 1, 2, 3, 2};
vector<string>value = {"hello", "help", "help", "hel", "hel"};
Trie trie;
for (int i = 0; i < n; i++) {
if (type[i] == 1) {
trie.insert(value[i]);
}
else if (type[i] == 2) {
if (trie.search(value[i])) {
cout << "true" << "\n";
}
else {
cout << "false" << "\n";
}
}
else {
if (trie.startsWith(value[i])) {
cout << "true" << "\n";
}
else {
cout << "false" << "\n";
}
}
}
}