Implement Phone Address Book - Reverse Index

Now I'm trying to create a data structure for implementing the adreess phone book. Let's say there are two information: name, phone numbers. The same name can have more than one number. Now I would like to create a data structure that will help me get the number assigned to the name and get the name given the number. I have to do it as soon as possible. I thought about saving the card (name to number), and then for a specific number somehow reverse index to that card, and then find the name. I'm not quite sure if this is the best way to do this. Can you suggest the perfect data structure. Will use some backlink. If so, how do I use it in this context.

+3
source share
4 answers

Assuming that prefix matches are desirable, I would suggest using Patricia trie. Assuming the name and phone number can never collide - i.e. You will not have anyone with the name 435-9876 in your directory - then you can insert pairs with an indexed phone number in trie, as well as with pairs indexed by name. If, for some strange reason, names and numbers may collide, you can add a special character to prefix the phone numbers so that they might collide.

Insertions will cost O (s)
Search will cost O (s)
where s is the length of the search string / inserted key.

Linux, , , , , .

EDIT:

. trie , , patricia trie , , , .

  • , , .
  • node .
  • node , , .
  • , .

, Jane Jamie , trie, - "Ja", , , "Jan", , node "Jane", , , "Jam", .

           [2]
   n                m
'Jane'            'Jamie'

,

           [2]
   n                m
'Jane'             [3]
              e             i
            'James'       'Jamie'

Jamelia

           [2]
   n                m
'Jane'             [3]
              e             i
              [4]         'Jamie'
             l     s
        'Jamelia'   'James'

. , . , , , , 2 s, n m, , . , , , , . . node [3]. node, Jam . , [3], . , , , , "Jam" .

, s, , , s, , . , , , node .

java- , ++ .

, , . , , ! - , .

+1

(-).

, Sets ( ) , .

Map<String,Set<String>> NamesToNumbers;

, - .

Map<String,String> NumbersToNames;

, , put(), remove() .. Key/Value.

Psuedo , id put() :

public void put(String Name,String PhoneNumber)
{

Set NumbersForName = NamesToNumbers.get(Name);
if (NumbersForName == null)
  {
    NumbersForName = new Set();
    NamesToNumbers.put(Name,NumbersForName);
  }

NumbersForName.put(PhoneNumber);
NumbersToNames.put(PhoneNumber,Name);  

}

O (1), O (1), .

Java 5+, Google Collections, Bi-map, , .

+1

Name x Number. . , , .

( perl)

  $db{$id} = [name, number]

Then impose this on two hashes returning sets of identifiers.

$name_index[$name] = [$id1, $id2, $id3]
$number_index[$number] = #as above

Updating will require a certain number of errors, but will quickly return results.

You can do this in any language that has a hash / map construct.

+1
source

You can use a pair of dictionaries (aka maps): one for finding numbers with names ( name -> List<number>) and one for finding names with numbers ( number -> name).

0
source

Source: https://habr.com/ru/post/1718307/


All Articles