Question

I am writing a program in C++ that requires IP addresses( all IPv4) to be looked up and stored in a fast way. Every IP address has a data associated with it. In case it already exists in the trie, I intend to merge the data of the IP address in the trie with the new addresses data. If it is not present, I intend to add it as a new entry to the trie. Deletion of IP address is not necessary.

In order to implement this, I need to design a Patricia Trie. However, I am unable to visualize the design beyond this. It seems quite naive of me, but the only idea that came to my mind was to change the IP address to their binary form and then use the trie. I am however clueless about HOW exactly to implement this.

I would be really thankful to you if you could help me with this one. Please note that I did find a similar question here . The question or more specifically the answer was beyond my understanding as the code in the CPAN web site was not clear enough for me.

Also note, my data is the following format

10.10.100.1: "Tom","Jack","Smith"

192.168.12.12: "Jones","Liz"

12.124.2.1: "Jimmy","George"

10.10.100.1: "Mike","Harry","Jennifer"

Was it helpful?

Solution

Patricia tries solve the problem of finding the best covering prefix for a given IP address (they are used by routers to quickly determine that 192.168.0.0/16 is the best choice for 192.168.14.63, for example). If you are just trying to match IP addresses exactly, a hash table is a better choice.

OTHER TIPS

I think you are referring to a RadixTree. I have an implementation of a RadixTrie in Java, if you want to use that as a starting point, which does the actual key to value mapping. It uses a PatriciaTrie as it's backing structure.

Example using the following strings.

  1. 10.10.101.2
  2. 10.10.100.1
  3. 10.10.110.3

Trie example (uncompressed)

└── 1
    └── 0
        └── .
            └── 1
                └── 0
                    └── .
                        └── 1
                            ├── 0
                            │   ├── 1
                            │   │   └── .
                            │   │       └── (2) 10.10.101.2
                            │   └── 0
                            │       └── .
                            │           └── (1) 10.10.100.1
                            └── 1
                                └── 0
                                    └── .
                                        └── (3) 10.10.110.3

Patricia Trie (compressed)

└── [black] 10.10.1
    ├── [black] 0
    │   ├── [white] (0.1) 00.1
    │   └── [white] (1.2) 01.2
    └── [white] (10.3) 10.10.110.3
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top