2 years ago

#58249

test-img

Joe

How to implement a C++ Trie with multiple variables

I need to implement a Trie data structure to store first name, last name and age.

The assignment says: "Once the path to the last name is established the first and last name and age of the person is stored in a struct variable of type Person. It is pointed to by the Trie." I don't understand exactly what this means, and all examples I found online only use one string instead of multiple variables (such as first name, last name and age).

I managed to get it working for one string of characters. But I don't know how to store the last name and age appropriately.

Code examples would be greatly appreciated.

Some other requirements/specifications:

  • The name strings are no longer than 10 characters.
  • Assume that all names are lowercase.
  • Accept only a-z letters (no punctuation, no hyphens, no accents, etc).
  • Search a Trie for a specific person's record based on last name only.
  • Search a Trie for a specific person's record based on first name + last name.
  • Delete A Person´s Record. Allow for people with same names do exist in the Trie.
#include <string>
#include <iostream>

using namespace std;
const int ALPHABET_SIZE = 26;


struct Trie
{
    struct Trie* child[ALPHABET_SIZE];
    bool endOfString; // True if the node represents end of word
};

Trie* createNode(void)
{
    Trie* trieNode = new Trie;
    trieNode->endOfString = false;
    
    for (int i = 0; i < ALPHABET_SIZE; i++)
        trieNode->child[i] = NULL;

    return trieNode;
}

void insert(Trie* root, string key)
{
    cout << "Inserting key: " + key << endl;
    Trie* current = root;

    for (int i = 0; i < key.length(); i++)
    {
        int index = key[i] - 'a';

        if (!current->child[index])
            current->child[index] = createNode();

        current = current->child[index];
    }

    current->endOfString = true; // Last node as leaf
}

bool search(Trie* root, string key)
{
    cout << "Searching for key: " + key + "...";

    Trie* current = root;

    for (int i = 0; i < key.length(); i++)
    {
        int index = key[i] - 'a';

        if (!current->child[index])
            return false;

        current = current->child[index];
    }

    return (current != NULL && current->endOfString);
}

bool isEmpty(Trie* root) // check if root has children
{
    for (int i = 0; i < ALPHABET_SIZE; i++)
        if (root->child[i])
            return false;

    return true;
}

Trie* remove(Trie* root, string key, int depth = 0){
    
    if (!root) // If tree is empty, return null
        return NULL;

    if (depth == key.size()) // If the last character is being processed
    {
        if (root->endOfString)
            root->endOfString = false;

        if (isEmpty(root))
        {
            delete (root);
            root = NULL;
        }

        return root;
    }

    int index = key[depth] - 'a';
    root->child[index] = remove(root->child[index], key, depth + 1); // Recur for child

    if (isEmpty(root) && root->endOfString == false) // If the root has no children
    {
        delete (root);
        root = NULL;
    }

    return root;
}

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

    insert(root, "john");
    insert(root, "jane");

    // Search test 1
    if (search(root, "john"))
        cout << "[FOUND]" << endl;
    else
        cout << "[NOT FOUND]" << endl;
    // Search test 2
    if (search(root, "jane"))
        cout << "[FOUND]" << endl;
    else
        cout << "[NOT FOUND]" << endl;
    // Search test 3 (Item does not exist)
    if (search(root, "jo"))
        cout << "[FOUND]" << endl;
    else
        cout << "[NOT FOUND]" << endl;

    // Removal test 
    remove(root, "john");
    if (search(root, "john"))
        cout << "[FOUND]" << endl;
    else
        cout << "[NOT FOUND]" << endl;  

    return 0;
}

c++

data-structures

binary-tree

binary-search-tree

trie

0 Answers

Your Answer

Accepted video resources