Thread: binary tree question

  1. #1
    Registered User
    Join Date
    Jan 2015
    Posts
    14

    binary tree question

    Hello!

    I need to create an address book using binary tree. As I see it I must attach an integer to every name in my book, so if I enter a name and it is in my aadress book, program acquire a integer. Now that integer can be searched in binary tree and I can get contact's info.

    My question is that is it good idea. Let's say hypotetically I have billion contacts and program must find that right one to acquire integer, doesn't it take too much time? What is the process behind comparing inserted strings with strings in memory?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well a billion is about 2³⁰
    So in an ideal binary tree, you would need 30 comparisons to traverse from root to tip.

    What you also need is a hash function which turns your address record (or some part of it) into a unique number. A number is good, as they can be typically compared in a single machine cycle.

    Choosing a good hash function is key, so that all possible address records are evenly distributed. You don't want massive clumps of similar records being stored under the same hashed value.

    Comparing 30 strings would take almost no time as well.

    Let's say hypotetically I have billion contacts and program must find that right one to acquire integer, doesn't it take too much time? What is the process behind comparing inserted strings with strings in memory?
    Consider that a good desktop machine might have say 16GB of memory.
    How much usable data per record do you think you'll have?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2015
    Posts
    14
    Thank you very much for your answer. It helped me clear my thoughts about binary trees

  4. #4
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    I assume your address book can be modeled as a function from names (string) to addresses (strings). If your address book should always be sorted, then a binary tree might be more efficient. However, using a balanced binary tree will require up to log_2(n) potentially expensive name string comparisons when accessing an address by name from the book. A better solution is to use a hash table, which is O(1) (constant time) lookup, insertion, and removal -- given that you can provide an way to convert the name strings to integers such that the integers are evenly distributed over the range of integers.. If your name-hashing function just return the ASCII or Unicode character value of the first character in the name, then all names that start with "D", for example, your O(1) performance will quickly devolve into an O(N) one...
    Last edited by MacNilly; 07-16-2016 at 06:21 AM.

  5. #5
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Can't you just use std::map?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary tree question
    By mgracecar in forum C Programming
    Replies: 4
    Last Post: 04-20-2012, 11:05 AM
  2. C++ question about the Binary Tree?
    By Cliff_505 in forum C++ Programming
    Replies: 2
    Last Post: 12-15-2010, 12:48 PM
  3. Binary tree question
    By Cpro in forum C++ Programming
    Replies: 3
    Last Post: 05-03-2007, 07:08 AM
  4. question on binary tree
    By angelic79 in forum C Programming
    Replies: 1
    Last Post: 12-13-2004, 05:43 PM
  5. Binary Tree Question
    By tar in forum C Programming
    Replies: 6
    Last Post: 09-26-2002, 01:22 AM

Tags for this Thread