Trie (dictionary tree) data structure implementation: insertWord, printAllWords, searchPrefix, deleteWord,...











up vote
3
down vote

favorite












This Trie implementation works, however, I would like to get an advice on how to improve this code.



Any advice is appreciated: functions implementation, memory management, modern C++ usage, const-correctness, code readability, anything else I am missing? Any additional functions that would be useful?



Trie.hpp



#pragma once

#include <iostream>
#include <string>
#include <vector>

/*
Trie for words, which consist of
lowercase English letters only
*/

class Trie {

private:
struct TrieNode {
bool isEndOfWord;
std::vector<std::shared_ptr<TrieNode>> children;
TrieNode() : children(26), isEndOfWord(false) {}
};

std::shared_ptr<TrieNode> root = std::make_shared<TrieNode>();

bool hasChildren (const std::shared_ptr<TrieNode>& root) const;
void printDFS (const std::shared_ptr<TrieNode>& root, std::string& stringToPrint) const;
void searchPrefix (const std::shared_ptr<TrieNode>& root, const std::string& prefix) const;
void searchPrefixDFS (const std::shared_ptr<TrieNode>& root, std::string& currPrefix) const;
bool deleteWordDFS (std::shared_ptr<TrieNode>& root, const std::string& word) const;
void deleteTrieDFS (std::shared_ptr<TrieNode>& root);

public:
Trie();
void insertWord (const std::string& word);
void printAllWords () const;
void searchPrefix (const std::string& prefix);
bool deleteWord (const std::string& word);
void deleteTrie ();
~Trie();
};


Trie.cpp



#include "Trie.hpp"

Trie::Trie () {
// Should I be initializing anything here?
}

void Trie::insertWord (const std::string& word) {
std::shared_ptr<TrieNode> curr = root;
for (const auto& ch : word) {
if (!curr->children[ch - 'a']) {
//std::cout << "adding: " << (char)(ind + 'a') << "n";
curr->children[ch - 'a'] = std::make_shared<TrieNode>();
}
curr = curr->children[ch - 'a'];
}
curr->isEndOfWord = true;
}

bool Trie::hasChildren (const std::shared_ptr<TrieNode>& root) const {
for (const auto& child : root->children) {
if (child) {
return true;
}
}
return false;
}

void Trie::printDFS (const std::shared_ptr<TrieNode>& root,
std::string& stringToPrint) const {
if (root->isEndOfWord) {
std::cout << stringToPrint << std::endl;
}
for (int i = 0; i < 26; ++i) {
if (root->children[i]) {
stringToPrint.push_back(i + 'a');
printDFS(root->children[i], stringToPrint);
stringToPrint.pop_back();
}
}
}

void Trie::printAllWords () const {
std::string stringToPrint;
if (root) {
Trie::printDFS (root, stringToPrint);
}
}

void Trie::searchPrefixDFS (const std::shared_ptr<TrieNode>& root, std::string& currPrefix) const {
if (root->isEndOfWord) {
std::cout << currPrefix << "n";
}
if (!hasChildren(root)){
return;
}
for (int i = 0; i < 26; ++i) {
if (root->children[i]) {
currPrefix.push_back(i + 'a');
searchPrefixDFS(root->children[i], currPrefix);
}
}
}

void Trie::searchPrefix (const std::shared_ptr<TrieNode>& root, const std::string& prefix) const {
std::shared_ptr<TrieNode> curr = root;
for (int lev = 0; lev < prefix.length(); lev++) {
char currChar = prefix[lev];
if (!curr->children[currChar - 'a']) {
std::cout << "Prefix " << prefix << " is not in a trie.n";
return;
}
curr = curr->children[currChar - 'a'];
}
std::string currPrefix = prefix;
Trie::searchPrefixDFS(curr, currPrefix);
}

void Trie::searchPrefix (const std::string& prefix) {
if (prefix.empty()) {
Trie::printAllWords();
} else {
Trie::searchPrefix (root, prefix);
}
}

bool Trie::deleteWordDFS (std::shared_ptr<TrieNode>& curr,
const std::string& word) const {

// This only returns true if we need ot keep going
// This returns false if we are finished with deleting the word
// or if the word in not in the trie

if (!curr) {
return false;
}
if (word.length()) {
if (curr && curr->children[word[0] - 'a'] &&
deleteWordDFS(curr->children[word[0] - 'a'], word.substr(1)) &&
!curr->isEndOfWord) {
if (!hasChildren(curr)) {
curr = nullptr;
return true;
} else {
return false;
}
}
}
if (word.length() == 0 && curr->isEndOfWord) {
// end of word
if (!hasChildren(curr)) {
// if no children - delete it
curr = nullptr;
return true; // keep going to delete parents
} else {
curr->isEndOfWord = false;
return false; // do not keep going
}
}
return false;
}

bool Trie::deleteWord (std::string const & word) {
return Trie::deleteWordDFS(root, word);
}

void Trie::deleteTrieDFS (std::shared_ptr<TrieNode>& root) {
for (auto& node : root->children) {
if (node) {
deleteTrieDFS(node);
}
}
if (!hasChildren(root)) {
root = nullptr;
}
}

void Trie::deleteTrie () {
Trie::deleteTrieDFS(root);
}

Trie::~Trie () {
// Should I be deleting anything here?
}


main.cpp



#include "Trie.hpp"

int main() {

// Vector of words
std::vector<std::string> words;
words.push_back("why");
words.push_back("a");
words.push_back("peach");
words.push_back("app");
words.push_back("cat");
words.push_back("apple");
words.push_back("pea");
words.push_back("banana");
words.push_back("ban");

// Trie init
Trie trieDict;

// Add words to trie
for (const auto& word : words) {
trieDict.insertWord(word);
}

std::cout << "Print all trie words: n";
trieDict.printAllWords();

// Search prefix:
std::string prefix = "p";
std::cout << "Words starting with " << prefix << ":n";
trieDict.searchPrefix(prefix);

// Deleting words from the trie
trieDict.deleteWord("why");
trieDict.deleteWord("app");
//trieDict.deleteWord("a");
//trieDict.deleteWord("peach");
//trieDict.deleteWord("pea");
//trieDict.deleteWord("cat");
//trieDict.deleteWord("ban");

std::cout << "Print all trie words after deletion: n";
trieDict.printAllWords();

// Delete Trie
trieDict.deleteTrie();
std::cout << "Print trie after trie deletion: n";
trieDict.printAllWords();
}









share|improve this question









New contributor




Tanja is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Can you give more explanation about all of these xxxxDFS functions please?
    – Calak
    7 hours ago










  • All the DFS (depth first search) function are basically the recursive functions to go down the tree and do something to each node. For example, deleteTrieDFS traverses each node's children recursively until the node doesn't have children - then the node is deleted (set to nullptr). Does it make sense?
    – Tanja
    7 hours ago















up vote
3
down vote

favorite












This Trie implementation works, however, I would like to get an advice on how to improve this code.



Any advice is appreciated: functions implementation, memory management, modern C++ usage, const-correctness, code readability, anything else I am missing? Any additional functions that would be useful?



Trie.hpp



#pragma once

#include <iostream>
#include <string>
#include <vector>

/*
Trie for words, which consist of
lowercase English letters only
*/

class Trie {

private:
struct TrieNode {
bool isEndOfWord;
std::vector<std::shared_ptr<TrieNode>> children;
TrieNode() : children(26), isEndOfWord(false) {}
};

std::shared_ptr<TrieNode> root = std::make_shared<TrieNode>();

bool hasChildren (const std::shared_ptr<TrieNode>& root) const;
void printDFS (const std::shared_ptr<TrieNode>& root, std::string& stringToPrint) const;
void searchPrefix (const std::shared_ptr<TrieNode>& root, const std::string& prefix) const;
void searchPrefixDFS (const std::shared_ptr<TrieNode>& root, std::string& currPrefix) const;
bool deleteWordDFS (std::shared_ptr<TrieNode>& root, const std::string& word) const;
void deleteTrieDFS (std::shared_ptr<TrieNode>& root);

public:
Trie();
void insertWord (const std::string& word);
void printAllWords () const;
void searchPrefix (const std::string& prefix);
bool deleteWord (const std::string& word);
void deleteTrie ();
~Trie();
};


Trie.cpp



#include "Trie.hpp"

Trie::Trie () {
// Should I be initializing anything here?
}

void Trie::insertWord (const std::string& word) {
std::shared_ptr<TrieNode> curr = root;
for (const auto& ch : word) {
if (!curr->children[ch - 'a']) {
//std::cout << "adding: " << (char)(ind + 'a') << "n";
curr->children[ch - 'a'] = std::make_shared<TrieNode>();
}
curr = curr->children[ch - 'a'];
}
curr->isEndOfWord = true;
}

bool Trie::hasChildren (const std::shared_ptr<TrieNode>& root) const {
for (const auto& child : root->children) {
if (child) {
return true;
}
}
return false;
}

void Trie::printDFS (const std::shared_ptr<TrieNode>& root,
std::string& stringToPrint) const {
if (root->isEndOfWord) {
std::cout << stringToPrint << std::endl;
}
for (int i = 0; i < 26; ++i) {
if (root->children[i]) {
stringToPrint.push_back(i + 'a');
printDFS(root->children[i], stringToPrint);
stringToPrint.pop_back();
}
}
}

void Trie::printAllWords () const {
std::string stringToPrint;
if (root) {
Trie::printDFS (root, stringToPrint);
}
}

void Trie::searchPrefixDFS (const std::shared_ptr<TrieNode>& root, std::string& currPrefix) const {
if (root->isEndOfWord) {
std::cout << currPrefix << "n";
}
if (!hasChildren(root)){
return;
}
for (int i = 0; i < 26; ++i) {
if (root->children[i]) {
currPrefix.push_back(i + 'a');
searchPrefixDFS(root->children[i], currPrefix);
}
}
}

void Trie::searchPrefix (const std::shared_ptr<TrieNode>& root, const std::string& prefix) const {
std::shared_ptr<TrieNode> curr = root;
for (int lev = 0; lev < prefix.length(); lev++) {
char currChar = prefix[lev];
if (!curr->children[currChar - 'a']) {
std::cout << "Prefix " << prefix << " is not in a trie.n";
return;
}
curr = curr->children[currChar - 'a'];
}
std::string currPrefix = prefix;
Trie::searchPrefixDFS(curr, currPrefix);
}

void Trie::searchPrefix (const std::string& prefix) {
if (prefix.empty()) {
Trie::printAllWords();
} else {
Trie::searchPrefix (root, prefix);
}
}

bool Trie::deleteWordDFS (std::shared_ptr<TrieNode>& curr,
const std::string& word) const {

// This only returns true if we need ot keep going
// This returns false if we are finished with deleting the word
// or if the word in not in the trie

if (!curr) {
return false;
}
if (word.length()) {
if (curr && curr->children[word[0] - 'a'] &&
deleteWordDFS(curr->children[word[0] - 'a'], word.substr(1)) &&
!curr->isEndOfWord) {
if (!hasChildren(curr)) {
curr = nullptr;
return true;
} else {
return false;
}
}
}
if (word.length() == 0 && curr->isEndOfWord) {
// end of word
if (!hasChildren(curr)) {
// if no children - delete it
curr = nullptr;
return true; // keep going to delete parents
} else {
curr->isEndOfWord = false;
return false; // do not keep going
}
}
return false;
}

bool Trie::deleteWord (std::string const & word) {
return Trie::deleteWordDFS(root, word);
}

void Trie::deleteTrieDFS (std::shared_ptr<TrieNode>& root) {
for (auto& node : root->children) {
if (node) {
deleteTrieDFS(node);
}
}
if (!hasChildren(root)) {
root = nullptr;
}
}

void Trie::deleteTrie () {
Trie::deleteTrieDFS(root);
}

Trie::~Trie () {
// Should I be deleting anything here?
}


main.cpp



#include "Trie.hpp"

int main() {

// Vector of words
std::vector<std::string> words;
words.push_back("why");
words.push_back("a");
words.push_back("peach");
words.push_back("app");
words.push_back("cat");
words.push_back("apple");
words.push_back("pea");
words.push_back("banana");
words.push_back("ban");

// Trie init
Trie trieDict;

// Add words to trie
for (const auto& word : words) {
trieDict.insertWord(word);
}

std::cout << "Print all trie words: n";
trieDict.printAllWords();

// Search prefix:
std::string prefix = "p";
std::cout << "Words starting with " << prefix << ":n";
trieDict.searchPrefix(prefix);

// Deleting words from the trie
trieDict.deleteWord("why");
trieDict.deleteWord("app");
//trieDict.deleteWord("a");
//trieDict.deleteWord("peach");
//trieDict.deleteWord("pea");
//trieDict.deleteWord("cat");
//trieDict.deleteWord("ban");

std::cout << "Print all trie words after deletion: n";
trieDict.printAllWords();

// Delete Trie
trieDict.deleteTrie();
std::cout << "Print trie after trie deletion: n";
trieDict.printAllWords();
}









share|improve this question









New contributor




Tanja is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Can you give more explanation about all of these xxxxDFS functions please?
    – Calak
    7 hours ago










  • All the DFS (depth first search) function are basically the recursive functions to go down the tree and do something to each node. For example, deleteTrieDFS traverses each node's children recursively until the node doesn't have children - then the node is deleted (set to nullptr). Does it make sense?
    – Tanja
    7 hours ago













up vote
3
down vote

favorite









up vote
3
down vote

favorite











This Trie implementation works, however, I would like to get an advice on how to improve this code.



Any advice is appreciated: functions implementation, memory management, modern C++ usage, const-correctness, code readability, anything else I am missing? Any additional functions that would be useful?



Trie.hpp



#pragma once

#include <iostream>
#include <string>
#include <vector>

/*
Trie for words, which consist of
lowercase English letters only
*/

class Trie {

private:
struct TrieNode {
bool isEndOfWord;
std::vector<std::shared_ptr<TrieNode>> children;
TrieNode() : children(26), isEndOfWord(false) {}
};

std::shared_ptr<TrieNode> root = std::make_shared<TrieNode>();

bool hasChildren (const std::shared_ptr<TrieNode>& root) const;
void printDFS (const std::shared_ptr<TrieNode>& root, std::string& stringToPrint) const;
void searchPrefix (const std::shared_ptr<TrieNode>& root, const std::string& prefix) const;
void searchPrefixDFS (const std::shared_ptr<TrieNode>& root, std::string& currPrefix) const;
bool deleteWordDFS (std::shared_ptr<TrieNode>& root, const std::string& word) const;
void deleteTrieDFS (std::shared_ptr<TrieNode>& root);

public:
Trie();
void insertWord (const std::string& word);
void printAllWords () const;
void searchPrefix (const std::string& prefix);
bool deleteWord (const std::string& word);
void deleteTrie ();
~Trie();
};


Trie.cpp



#include "Trie.hpp"

Trie::Trie () {
// Should I be initializing anything here?
}

void Trie::insertWord (const std::string& word) {
std::shared_ptr<TrieNode> curr = root;
for (const auto& ch : word) {
if (!curr->children[ch - 'a']) {
//std::cout << "adding: " << (char)(ind + 'a') << "n";
curr->children[ch - 'a'] = std::make_shared<TrieNode>();
}
curr = curr->children[ch - 'a'];
}
curr->isEndOfWord = true;
}

bool Trie::hasChildren (const std::shared_ptr<TrieNode>& root) const {
for (const auto& child : root->children) {
if (child) {
return true;
}
}
return false;
}

void Trie::printDFS (const std::shared_ptr<TrieNode>& root,
std::string& stringToPrint) const {
if (root->isEndOfWord) {
std::cout << stringToPrint << std::endl;
}
for (int i = 0; i < 26; ++i) {
if (root->children[i]) {
stringToPrint.push_back(i + 'a');
printDFS(root->children[i], stringToPrint);
stringToPrint.pop_back();
}
}
}

void Trie::printAllWords () const {
std::string stringToPrint;
if (root) {
Trie::printDFS (root, stringToPrint);
}
}

void Trie::searchPrefixDFS (const std::shared_ptr<TrieNode>& root, std::string& currPrefix) const {
if (root->isEndOfWord) {
std::cout << currPrefix << "n";
}
if (!hasChildren(root)){
return;
}
for (int i = 0; i < 26; ++i) {
if (root->children[i]) {
currPrefix.push_back(i + 'a');
searchPrefixDFS(root->children[i], currPrefix);
}
}
}

void Trie::searchPrefix (const std::shared_ptr<TrieNode>& root, const std::string& prefix) const {
std::shared_ptr<TrieNode> curr = root;
for (int lev = 0; lev < prefix.length(); lev++) {
char currChar = prefix[lev];
if (!curr->children[currChar - 'a']) {
std::cout << "Prefix " << prefix << " is not in a trie.n";
return;
}
curr = curr->children[currChar - 'a'];
}
std::string currPrefix = prefix;
Trie::searchPrefixDFS(curr, currPrefix);
}

void Trie::searchPrefix (const std::string& prefix) {
if (prefix.empty()) {
Trie::printAllWords();
} else {
Trie::searchPrefix (root, prefix);
}
}

bool Trie::deleteWordDFS (std::shared_ptr<TrieNode>& curr,
const std::string& word) const {

// This only returns true if we need ot keep going
// This returns false if we are finished with deleting the word
// or if the word in not in the trie

if (!curr) {
return false;
}
if (word.length()) {
if (curr && curr->children[word[0] - 'a'] &&
deleteWordDFS(curr->children[word[0] - 'a'], word.substr(1)) &&
!curr->isEndOfWord) {
if (!hasChildren(curr)) {
curr = nullptr;
return true;
} else {
return false;
}
}
}
if (word.length() == 0 && curr->isEndOfWord) {
// end of word
if (!hasChildren(curr)) {
// if no children - delete it
curr = nullptr;
return true; // keep going to delete parents
} else {
curr->isEndOfWord = false;
return false; // do not keep going
}
}
return false;
}

bool Trie::deleteWord (std::string const & word) {
return Trie::deleteWordDFS(root, word);
}

void Trie::deleteTrieDFS (std::shared_ptr<TrieNode>& root) {
for (auto& node : root->children) {
if (node) {
deleteTrieDFS(node);
}
}
if (!hasChildren(root)) {
root = nullptr;
}
}

void Trie::deleteTrie () {
Trie::deleteTrieDFS(root);
}

Trie::~Trie () {
// Should I be deleting anything here?
}


main.cpp



#include "Trie.hpp"

int main() {

// Vector of words
std::vector<std::string> words;
words.push_back("why");
words.push_back("a");
words.push_back("peach");
words.push_back("app");
words.push_back("cat");
words.push_back("apple");
words.push_back("pea");
words.push_back("banana");
words.push_back("ban");

// Trie init
Trie trieDict;

// Add words to trie
for (const auto& word : words) {
trieDict.insertWord(word);
}

std::cout << "Print all trie words: n";
trieDict.printAllWords();

// Search prefix:
std::string prefix = "p";
std::cout << "Words starting with " << prefix << ":n";
trieDict.searchPrefix(prefix);

// Deleting words from the trie
trieDict.deleteWord("why");
trieDict.deleteWord("app");
//trieDict.deleteWord("a");
//trieDict.deleteWord("peach");
//trieDict.deleteWord("pea");
//trieDict.deleteWord("cat");
//trieDict.deleteWord("ban");

std::cout << "Print all trie words after deletion: n";
trieDict.printAllWords();

// Delete Trie
trieDict.deleteTrie();
std::cout << "Print trie after trie deletion: n";
trieDict.printAllWords();
}









share|improve this question









New contributor




Tanja is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











This Trie implementation works, however, I would like to get an advice on how to improve this code.



Any advice is appreciated: functions implementation, memory management, modern C++ usage, const-correctness, code readability, anything else I am missing? Any additional functions that would be useful?



Trie.hpp



#pragma once

#include <iostream>
#include <string>
#include <vector>

/*
Trie for words, which consist of
lowercase English letters only
*/

class Trie {

private:
struct TrieNode {
bool isEndOfWord;
std::vector<std::shared_ptr<TrieNode>> children;
TrieNode() : children(26), isEndOfWord(false) {}
};

std::shared_ptr<TrieNode> root = std::make_shared<TrieNode>();

bool hasChildren (const std::shared_ptr<TrieNode>& root) const;
void printDFS (const std::shared_ptr<TrieNode>& root, std::string& stringToPrint) const;
void searchPrefix (const std::shared_ptr<TrieNode>& root, const std::string& prefix) const;
void searchPrefixDFS (const std::shared_ptr<TrieNode>& root, std::string& currPrefix) const;
bool deleteWordDFS (std::shared_ptr<TrieNode>& root, const std::string& word) const;
void deleteTrieDFS (std::shared_ptr<TrieNode>& root);

public:
Trie();
void insertWord (const std::string& word);
void printAllWords () const;
void searchPrefix (const std::string& prefix);
bool deleteWord (const std::string& word);
void deleteTrie ();
~Trie();
};


Trie.cpp



#include "Trie.hpp"

Trie::Trie () {
// Should I be initializing anything here?
}

void Trie::insertWord (const std::string& word) {
std::shared_ptr<TrieNode> curr = root;
for (const auto& ch : word) {
if (!curr->children[ch - 'a']) {
//std::cout << "adding: " << (char)(ind + 'a') << "n";
curr->children[ch - 'a'] = std::make_shared<TrieNode>();
}
curr = curr->children[ch - 'a'];
}
curr->isEndOfWord = true;
}

bool Trie::hasChildren (const std::shared_ptr<TrieNode>& root) const {
for (const auto& child : root->children) {
if (child) {
return true;
}
}
return false;
}

void Trie::printDFS (const std::shared_ptr<TrieNode>& root,
std::string& stringToPrint) const {
if (root->isEndOfWord) {
std::cout << stringToPrint << std::endl;
}
for (int i = 0; i < 26; ++i) {
if (root->children[i]) {
stringToPrint.push_back(i + 'a');
printDFS(root->children[i], stringToPrint);
stringToPrint.pop_back();
}
}
}

void Trie::printAllWords () const {
std::string stringToPrint;
if (root) {
Trie::printDFS (root, stringToPrint);
}
}

void Trie::searchPrefixDFS (const std::shared_ptr<TrieNode>& root, std::string& currPrefix) const {
if (root->isEndOfWord) {
std::cout << currPrefix << "n";
}
if (!hasChildren(root)){
return;
}
for (int i = 0; i < 26; ++i) {
if (root->children[i]) {
currPrefix.push_back(i + 'a');
searchPrefixDFS(root->children[i], currPrefix);
}
}
}

void Trie::searchPrefix (const std::shared_ptr<TrieNode>& root, const std::string& prefix) const {
std::shared_ptr<TrieNode> curr = root;
for (int lev = 0; lev < prefix.length(); lev++) {
char currChar = prefix[lev];
if (!curr->children[currChar - 'a']) {
std::cout << "Prefix " << prefix << " is not in a trie.n";
return;
}
curr = curr->children[currChar - 'a'];
}
std::string currPrefix = prefix;
Trie::searchPrefixDFS(curr, currPrefix);
}

void Trie::searchPrefix (const std::string& prefix) {
if (prefix.empty()) {
Trie::printAllWords();
} else {
Trie::searchPrefix (root, prefix);
}
}

bool Trie::deleteWordDFS (std::shared_ptr<TrieNode>& curr,
const std::string& word) const {

// This only returns true if we need ot keep going
// This returns false if we are finished with deleting the word
// or if the word in not in the trie

if (!curr) {
return false;
}
if (word.length()) {
if (curr && curr->children[word[0] - 'a'] &&
deleteWordDFS(curr->children[word[0] - 'a'], word.substr(1)) &&
!curr->isEndOfWord) {
if (!hasChildren(curr)) {
curr = nullptr;
return true;
} else {
return false;
}
}
}
if (word.length() == 0 && curr->isEndOfWord) {
// end of word
if (!hasChildren(curr)) {
// if no children - delete it
curr = nullptr;
return true; // keep going to delete parents
} else {
curr->isEndOfWord = false;
return false; // do not keep going
}
}
return false;
}

bool Trie::deleteWord (std::string const & word) {
return Trie::deleteWordDFS(root, word);
}

void Trie::deleteTrieDFS (std::shared_ptr<TrieNode>& root) {
for (auto& node : root->children) {
if (node) {
deleteTrieDFS(node);
}
}
if (!hasChildren(root)) {
root = nullptr;
}
}

void Trie::deleteTrie () {
Trie::deleteTrieDFS(root);
}

Trie::~Trie () {
// Should I be deleting anything here?
}


main.cpp



#include "Trie.hpp"

int main() {

// Vector of words
std::vector<std::string> words;
words.push_back("why");
words.push_back("a");
words.push_back("peach");
words.push_back("app");
words.push_back("cat");
words.push_back("apple");
words.push_back("pea");
words.push_back("banana");
words.push_back("ban");

// Trie init
Trie trieDict;

// Add words to trie
for (const auto& word : words) {
trieDict.insertWord(word);
}

std::cout << "Print all trie words: n";
trieDict.printAllWords();

// Search prefix:
std::string prefix = "p";
std::cout << "Words starting with " << prefix << ":n";
trieDict.searchPrefix(prefix);

// Deleting words from the trie
trieDict.deleteWord("why");
trieDict.deleteWord("app");
//trieDict.deleteWord("a");
//trieDict.deleteWord("peach");
//trieDict.deleteWord("pea");
//trieDict.deleteWord("cat");
//trieDict.deleteWord("ban");

std::cout << "Print all trie words after deletion: n";
trieDict.printAllWords();

// Delete Trie
trieDict.deleteTrie();
std::cout << "Print trie after trie deletion: n";
trieDict.printAllWords();
}






c++ c++11 trie






share|improve this question









New contributor




Tanja is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Tanja is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 9 hours ago









200_success

127k15148410




127k15148410






New contributor




Tanja is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 11 hours ago









Tanja

161




161




New contributor




Tanja is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Tanja is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Tanja is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • Can you give more explanation about all of these xxxxDFS functions please?
    – Calak
    7 hours ago










  • All the DFS (depth first search) function are basically the recursive functions to go down the tree and do something to each node. For example, deleteTrieDFS traverses each node's children recursively until the node doesn't have children - then the node is deleted (set to nullptr). Does it make sense?
    – Tanja
    7 hours ago


















  • Can you give more explanation about all of these xxxxDFS functions please?
    – Calak
    7 hours ago










  • All the DFS (depth first search) function are basically the recursive functions to go down the tree and do something to each node. For example, deleteTrieDFS traverses each node's children recursively until the node doesn't have children - then the node is deleted (set to nullptr). Does it make sense?
    – Tanja
    7 hours ago
















Can you give more explanation about all of these xxxxDFS functions please?
– Calak
7 hours ago




Can you give more explanation about all of these xxxxDFS functions please?
– Calak
7 hours ago












All the DFS (depth first search) function are basically the recursive functions to go down the tree and do something to each node. For example, deleteTrieDFS traverses each node's children recursively until the node doesn't have children - then the node is deleted (set to nullptr). Does it make sense?
– Tanja
7 hours ago




All the DFS (depth first search) function are basically the recursive functions to go down the tree and do something to each node. For example, deleteTrieDFS traverses each node's children recursively until the node doesn't have children - then the node is deleted (set to nullptr). Does it make sense?
– Tanja
7 hours ago















active

oldest

votes











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");

StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






Tanja is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f208001%2ftrie-dictionary-tree-data-structure-implementation-insertword-printallwords%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes








Tanja is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















Tanja is a new contributor. Be nice, and check out our Code of Conduct.













Tanja is a new contributor. Be nice, and check out our Code of Conduct.












Tanja is a new contributor. Be nice, and check out our Code of Conduct.















 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f208001%2ftrie-dictionary-tree-data-structure-implementation-insertword-printallwords%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'