GfG QA is closing soon... Please refer PRACTICE or GfG now on wards !!

How to create a multidimension trie in C++ where entries support wildcards.

  1. For example in my case the entries are:
    • MCC, MNC, TAC and ECI.
  2. Valid values for these entries can be:
    • MCC can have following format : */ddd/dXX
    • MNC can have following format : */dd/ddd/dX/dXX
    • TAC can have following format : */hhhh/hXXX
    • ECI can have following format : */hhhhhhh/hXXXXXX
      • Where “h” represents hexadecimal number (0-9 A-F) in any case (lower or upper case).
      • “X” represents wildcard character. It can also be in lower or upper case.
      • “d” represents digit (0-9).
  3. There can be same MCC for the combination of MNC, TAC and ECI. Same (MCC +MNC) combination for TAC and ECI and so on. So the unique key to fetch the value from the entry loaded in the memory needs to be a combination of MCC+MNC+TAC+ECI.

  4. The entries in the table can be for example:

    • MCC MNC TAC ECI
    • 34X 2XX 12XX 987XXXX
    • 341 211 1223 9871234
    • 3XX 431 * *
    • * * * *

// Putting more information

class Pani
{
    struct Wildcard
    {
    // This contains the number of wildcards present in the entry
    };
    struct Node
    {
    // This has the pointer to next node and
    // pointer to the wildcard node
    };
    Node root;

    max_size = 17; //(MCC+MNC+TAC+ECI : 3+3+4+7)
    public:
    // insert function
    // find function
    // ascii to index conversion function
    // index to ascii conversion function
};

Ascii to index conversion function:

  1. 0-9 return 0-9
  2. A-F return 10-15
  3. X return 88.

Similarly the revrse for index to ascii conversion function.

Insert function:

  1. Initializing index to 0.
  2. If index is less than max_size and value (key[index]) is not 'X' (wildcard),
    • Get the ascii value of the key[index].
    • Create the new node and increase the index
  3. If any wildcard is encountered, treat evry entry to be wildcard after this.
    • Create the wildcard node and insert the value at the 'n' index (date[num_wildcards])

Find function:

  1. Initializing index to 0.
  2. If index is less than max_size and value (key[index]) is not 'X' (wildcard),
    • Get the ascii value of the key[index].
    • Go to the same index in the node, if the value is not NULL else return.
    • If the wildcard is present, i.e 'X' is present,
      • If the selected node has wildcard and corresponding data present at the index (number of wildcards: node->wildcard->data[num_wildcard]), return value else return NULL;
asked Jul 20, 2015 by rahulpoddar
retagged Jul 20, 2015 by rahulpoddar

We'll suggest you to kindly design a solution first and then start the discussion over it.It'll definitly boost up your skills.

I have been able to implement for the single entry with wildcards (example: 3XX). The code snapshot is mentioned above. I am not sure how to proceed with multiple entries, each having wildcards. 

...