Thread: Binary Tree -vs- Linked List

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    7

    Binary Tree -vs- Linked List

    If a binary tree's worst-case-scenario is a structure already in order (i.e. a linked list), then what benefit is there to -ever- use a linked list? Since you're guaranteed equal or better efficiency with a binary tree, I see no logical reason for linked lists to even exist functionally, yet I find them everywhere?

    I'm sorry if this is off-topic for this forum, but I'm actually curious about this question in relation to the C language, since I'm unsure if the answer would be different depending on that or not, and it's the only language I use. Thanks for any help.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Linked lists are
    - much simpler to implement
    - not a performance problem for small lists
    - work well when you don't have an ordering relationship.
    - you don't need to do lots and lots of searching.

    Trees are
    - harder to write
    - can be very expensive to keep balanced
    - imply that the elements can be ordered

    So if you have a large amount of fairly static data which you need to search a lot, and you have some kind of ordering relationship, then building a balanced tree out of it might be worth doing.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    7
    Ok, if I had a list of up to, say, 1000 nodes, and could order them with a fairly 'random' key (good for balancing), would the efficiency that a binary tree provides even be noticeable over a 1000 node linked list?

    Obviously if I had a list of hundreds of thousands or millions of nodes, the efficiency would show. But what about for smaller sized lists (like 1000 nodes)?

    How can I reliably test this in my code? I know I'd need to be able to get the time at a certain point before the traversal, then get it at the end and compare the difference - but what function(s) is/are reliable and accurate enough to compare the miniscule times I'd be getting?

  4. #4
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Instead of trying to measure the thickness of one piece of paper, measure the thickness of 100 pieces of paper and then divide by 100.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > would the efficiency that a binary tree provides even be noticeable over a 1000 node linked list?
    Efficiency of what?
    Inserting at head/tail of a linked list is trivial compared to the cost of inserting the last node into a binary tree.
    It's definitely got the edge on searching, but unless you do a lot of it, its hard to know where the cut-off point is.

    If for example your list is actually the lines of a file in the order the lines where read from a file, then storing that in a tree makes no sense at all.

    > but what function(s) is/are reliable and accurate enough to compare the miniscule times I'd be getting?
    State your OS/Compiler for more information.
    http://msdn.microsoft.com/library/de...ncecounter.asp
    Or my use of rdtsc here http://cboard.cprogramming.com/showt...ighlight=rdtsc

  6. #6
    Registered User Kurisu's Avatar
    Join Date
    Feb 2006
    Posts
    62
    time(NULL) = accuracy is seconds
    GetSystemTime() = milliseconds I believe?
    QueryPerformanceFrequency/QueryPerformanceCounter = 1 microsecond (1,000,000th of a second) accuracy.

    Nanoseconds are possible, but why bother..

    I took a class that covered all this stuff and I believe I had to write a whole program that simulated different structures randomly with different sort algorithms etc..

    Each has its own advantages and disadvantages.

    Hash Tables; Trees; Heap; Linked Lists; Double Linked lists; blah blah... rather tedious class, but was useful.

    Bottom line lists are the easiest to implement and get the job done for alot of tasks. I've used them more than anything else since alot of problems for storing things doesn't require sorting them or really having to search them that much. Only other thing I used recently was a hash table for a symbol table for a compiler I had to write because it required alot of looking up/searching for junx. Trees require algorithms for just outputing everything, which is a pain (i.e. Pre-order, post-order, in-order)

    There are algorithms out there that tell of worst-case best-case for these various structures & search algorithms, but alas be the great student I am I have already forgotten them.. Good Luck if you go a searching. Just look for O notation like

    Linked Lists = O(n)
    Binary Trees = O(Log n)
    [n = # of elements]

    If you really calculate these kind of junx you can see at what level one structure surpasses the other & same for different search algorithms.

    Okay all this talk of structures is putting me to sleep.. Did i mention this is boooring stuff to learn?
    Last edited by Kurisu; 02-08-2006 at 02:31 AM.

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    7
    @ Salem:

    I guess I was referring just to general efficiency. In my implementation, I need a structured list to hold approximately ~1000 or so nodes, each containing some numerical data, some string data, and sorted by a fairly random unsigned int field.

    These nodes will be added and removed at a fairly low rate (maybe a call every minute or two) so I'm not too worried about that. I will be doing a -lot- of searching though, and I'm trying to figure out what structure would best fit my needs. I assumed that my two best choices were a binary tree or linked list, but I'm not limited to one or the other.

    And I'm using gcc 4.0.0 on an x86 64 (2.6.14 kernel).

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Big-Oh notation gives a general guide to the worst-case "large" answer, but simple algorithms and data structures are often quicker for small 'n'.

    For example, you're changing the data by 1 node every minute, then even a simple array based answer sorted by bubble sort (which would only take 1 pass to sort an array which is only out of sort by 1 element). Then you could use the standard bsearch() function to search the array very efficiently.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List or Binary Search Tree
    By rhysmeister in forum C++ Programming
    Replies: 6
    Last Post: 04-29-2004, 03:04 PM
  2. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 09:27 PM
  3. How to use Linked List?
    By MKashlev in forum C++ Programming
    Replies: 4
    Last Post: 08-06-2002, 07:11 AM
  4. BST/Red and Black Tree
    By ghettoman in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2001, 10:45 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM