15

Possible Duplicate:
storing 1 million phone numbers

How to design a data structure for a phone address book with 3 fields name, phone number , address

one must be able to search this phone book on any of the 3 fields

Hash table wouldn't work because all the three fields should hash to the same value which is i think impossible. I thought about trie and other data structures too but couldn't think of a proper answer.

Community
  • 1
  • 1
Anusha Pachunuri
  • 1,389
  • 4
  • 18
  • 39
  • 4
    Have you considered a relational database? –  Feb 02 '12 at 05:10
  • I see that database table is the best answer which i have already thought of. But thats an interview question data structures specific , so i was wondering if there is any such data structure? – Anusha Pachunuri Feb 02 '12 at 05:11
  • If you want to search by phone number, then a dictionary with the phone number as key and person as data would be good. If searching by last name, then a hash-table with last name as key, and data is a list of persons whose hash collides. – Some programmer dude Feb 02 '12 at 06:35
  • Use three hashtables? Or two hashtables and a heap (for the name index)? – Marcin Feb 02 '12 at 08:22
  • @Marcin Heap? Don't you mean tree? – Nick Barnes Feb 02 '12 at 08:27
  • @NickBarnes A heap is a type of binary tree. – Marcin Feb 02 '12 at 08:28
  • @Marcin Yes, but it's not a binary *search* tree. It doesn't allow efficient lookup of a specific element. It only gives fast access to the smallest / largest member. – Nick Barnes Feb 02 '12 at 08:43
  • @NickBarnes: Perhaps I'm mistaken, but it stores its elements in sorted order, so by definition that should make for efficient binary searching. – Marcin Feb 02 '12 at 09:09
  • @Marcin Heaps aren't sorted. They only guarantee that an element is larger (in a max-heap) than all of its descendants. Swapping its two children won't affect the heap, while it would destroy a binary search tree. You can sort the elements efficiently by removing them one by one (and reordering the heap in between), but it's a destructive process. – Nick Barnes Feb 02 '12 at 10:05
  • @Nick and Marcin ...Ok 3 hash tables would do. But wouldnt it take a lot of space? I was expecting an answer that would enable search by all (name,phone no,address) using a single data structure but i see that is not really possible. I want to know about the 2 hash tables and 1 BST approach though? Can you please explain? – Anusha Pachunuri Feb 03 '12 at 05:25

7 Answers7

7

You Should use TRIE data Structure for Implementing Phonebook. TRIE is an ordered tree data structure that uses strings as keys. Unlike Binary Trees, TRIE does not store keys associated with the node.

Good example

Sumit Singh
  • 15,743
  • 6
  • 59
  • 89
Raj
  • 121
  • 2
  • 5
5

You could accomplish this with a single hash table or other type of associative array (if you wanted to). For each person, just have three keys in the table (name, address, phone) all pointing to the same record.

gcbenison
  • 11,723
  • 4
  • 44
  • 82
3

I think a combination of a trie (each phone book entry is one leaf) and two skip lists (one for each name and address) could turn out to be effective.

Just assign each node one set of pointers to move along the name axis, and one set of pointers to move along the address axis (that is, to traverse the skip lists).

moooeeeep
  • 31,622
  • 22
  • 98
  • 187
1

You can't exactly sort something in three ways at the same time. Nor can you feasibly build a single hash table which allows lookup with only a third of the key.

What you probably want to do is basically what databases do:

  • Store one (possibly unsorted) master list of all your records.
  • For each column you want to be able to search on, build some kind of lookup structure which returns a pointer/index into the master list.

So, for example, you build a flat array of {name, phone, address} structs in whatever order you want, and then for each row, put a (phone -> row#) mapping into a hash table. Non-unique columns could hash to a list of row numbers, or you could put them in a binary tree where duplicate keys aren't an issue.

As far as space requirements, you basically end up storing every element twice, so your space requirement will at least double. On top of this you've got the overhead from the data structures themselves; keeping three hash tables loaded at ~70% capacity, your storage requirements increase by at least 2.4 times.

You can do away with one of these auxiliary lookup structures by keeping your main table sorted on one of the columns, so you can search on it directly in O(logN). However, this makes inserting/deleting rows very expensive (O(N)), but if your data is fairly static, this isn't much of an issue. And if this is the case, sorted arrays would be the most space-efficient choice for your auxiliary lookups as well.

Nick Barnes
  • 19,816
  • 3
  • 51
  • 63
  • I guess the master datastructure is best served by a binarysearchtree, because it assists in O(log n) insertion and lookup with a default alphabetically sorted display of names. – Antony Thomas Jun 03 '13 at 16:36
0

in a phone book, the telephone number should be unique, address is unique, but the name could be duplicated.

so perhaps you can use hash table combine with linked list to approach this.

you can use any one or combination of the 'name, address, phone number' as hash key, if you simply use name as hash key, then linked list is needed to store the duplicated entries.

in this approach, search based on the hash key is O(1) efficiency, but search based on the other two will be O(n).

  • Why should the address be unique? Surely an address could have multiple telephone lines. – NPE Feb 02 '12 at 10:48
  • I will tell you where the problem is. Say I hash the entries using the combination of three. And i want to search using name,how can you guarantee that the hash value key=name and the hash value when key=(name,address,ph no) is the same. Thats where my question lied. – Anusha Pachunuri Feb 03 '12 at 05:37
-1

C or C++ or C#?

Use a list of classes

    public class PhoneBook
{
    public string name;
    public string phoneNumber;
    public string address;
}

place this in a list and you have a phone book

DJ Burb
  • 2,346
  • 2
  • 29
  • 38
  • But how will the search get efficient if you just traverse element by element? It would be O(n) right? – Anusha Pachunuri Feb 02 '12 at 05:23
  • 3
    C. The question is tagged `c`. There are no classes. – Cody Gray - on strike Feb 02 '12 at 05:26
  • yes C. I can take your answer involving structures? But still is that the only solution? – Anusha Pachunuri Feb 02 '12 at 05:30
  • Since it is, C then you would probably have to do a linked list of structs .... Look here: http://gd.tuwien.ac.at/languages/c/programming-bbrown/c_086.htm – DJ Burb Feb 02 '12 at 15:11
  • @DJBurb You're missing the point. It doesn't matter if you store the rows in objects or structs or comma-separated strings. What are you going to do to make your set of rows efficiently searchable? "Placing them in a list" is not going to help. – Nick Barnes Feb 03 '12 at 08:08
-1

In C, I think a struct is the best option.

typedef struct _Contact Contact;

struct _Contact
{
    char* name;
    char* number;
    char* address;
};

Contact* add_new_contact( char* name, char* number, char* address )
{
    Contact* c = (Contact*) malloc( sizeof( Contact ) );
    c->name = name;
    c->number = number;
    c->address = address;
    return c;
}

Contact* phone_book [ 20 ];  /* An array of Contacts */

Use the standard string functions ( <string.h> or if using a C++ compiler, <cstring> ) or something like the glib for searching the names, numbers etc.

Here's a simple example:

Contact* search_for_number( Contact* phone_book[], const char* number )
{
    register int i;
    for( i = 0; i < sizeof( phone_book ); i++)
    {
       if ( strcmp( phone_book[i]->number, number ) == 0 ) return phone_book[i];
    }
    return NULL;
}

There is also a good, helpful code example over here.

Alternatively

You may be able to use linked lists, but since C or the C standard library doesn't provide linked-lists, you either need to implement it yourself, or to use a third-party library.

I suggest using the g_linked_list in the glib.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
ApprenticeHacker
  • 21,351
  • 27
  • 103
  • 153
  • I don't think structs and linear traversals were quite what the asker had in mind when they tagged this with "data-structures" and "algorithms"... Hash tables and tries are data structures. Structs are implementation details. – Nick Barnes Feb 02 '12 at 08:33
  • Exactly. I wasn;t looking for implementation details – Anusha Pachunuri Feb 03 '12 at 05:21