0% found this document useful (0 votes)
3 views

cf

Uploaded by

sunny950775
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

cf

Uploaded by

sunny950775
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

/*

Struct TrueNode{
int subTrieStrings;
Node* children[26];
}
int find(string & prefix, int pos, Node* root){
if(root == NULL) return 0;
if(pos == prefix.size())
return root->subTrie strings;

return find(prefix, pos+1, root->children[prefix[pos]-'a']);


}

*/

/*
#include <iostream>
#include <string>
using namespace std;

struct Node {
int subTrieStrings;
Node* children[26];

Node() {
subTrieStrings = 0;
for (int i = 0; i < 26; ++i)
children[i] = nullptr;
}
};

class Trie {
private:
Node* root;

public:
Trie() {
root = new Node();
}

void insert(string word) {


Node* curr = root;
for (char c : word) {
if (curr->children[c - 'a'] == nullptr)
curr->children[c - 'a'] = new Node();
curr = curr->children[c - 'a'];
curr->subTrieStrings++; // Increment the count of strings passing
through this node
}
}

int find(string &prefix) {


Node* curr = root;
for (char c : prefix) {
if (curr->children[c - 'a'] == nullptr)
return 0; // Prefix not found
curr = curr->children[c - 'a'];
}
return curr->subTrieStrings;
}
};

int main() {
Trie trie;
trie.insert("apple");
trie.insert("app");
trie.insert("banana");
trie.insert("bat");

cout << "Frequency of prefix 'a': " << trie.find("a") << endl;
cout << "Frequency of prefix 'ap': " << trie.find("ap") << endl;
cout << "Frequency of prefix 'ban': " << trie.find("ban") << endl;
cout << "Frequency of prefix 'c': " << trie.find("c") << endl;

return 0;
}

*/

****** INSERTION in TRIES *********

struct Node{
int strings;
bool end;
Node* children[26];
}
Node* create Node(){
Node* node = new Node;
node -> end = false;
node -> strings = 0;
for(int i=0; i<26; i++){
node->children[i] = NULL:
return node;
}
void insert(string & word, int pos, Node*curr){
if(pos == word.size()){
curr->end = true;
return;
}
curr->strings++;
if(!curr->children[word[pos]-'a']);
curr->children[word[pos]-'a'] = createNode();
insert(word, pos+1, curr->children[word[pos]-'a');
}
}

#include <iostream>
#include <string>
using namespace std;

struct Node {
int strings;
bool end;
Node* children[26];

Node() {
strings = 0;
end = false;
for (int i = 0; i < 26; ++i)
children[i] = nullptr;
}
};

Node* createNode() {
Node* node = new Node;
node->end = false;
node->strings = 0;
return node;
}

void insert(string &word, int pos, Node* curr) {


if (pos == word.size()) {
curr->end = true;
return;
}
curr->strings++;
if (!curr->children[word[pos] - 'a'])
curr->children[word[pos] - 'a'] = createNode();
insert(word, pos + 1, curr->children[word[pos] - 'a']);
}

int main() {
Node* root = createNode();

string word1 = "apple";


string word2 = "banana";
string word3 = "bat";

insert(word1, 0, root);
insert(word2, 0, root);
insert(word3, 0, root);

// Example: Checking if a word exists in the trie


Node* curr = root;
string query = "apple";
for (char c : query) {
if (curr->children[c - 'a']) {
curr = curr->children[c - 'a'];
} else {
cout << "Word '" << query << "' doesn't exist in the trie." << endl;
return 0;
}
}
if (curr->end)
cout << "Word '" << query << "' exists in the trie." << endl;
else
cout << "Word '" << query << "' doesn't exist in the trie." << endl;

return 0;
}

*************DELETION in tries**********

bool found = false;


void deletion(string & word, int pos, Node* curr){
if(curr == NULL) return ;
if(pos == word.size()){
if(curr->end){
found = true;
curr->strings--;
}
return;
}
deletion(word, pos+1, curr->children[word[pos]-'a']);
if(found) curr->strings--;
if(curr->children[word[pos]-'a']->strings == 0){
Node* temp = curr->children[];
curr->children[] = NULL;
delete (temp);
}
}

********Short Unique Prefixes***

void findUniquePrefix(string & s, Node* curr, int pos){


if(curr-> strings == 1){
ans = s.substring(0, pos);
return;
}
findUniquePrefix(s, curr->children[s[pos]-'a'], pos+1);
}

tc-o(nl)
sc-o(nl)

You might also like