Originally posted by: joeSmack
Ha. It does sound like a HW problem. With Tree's, Linked Lists, and Hashtables there are several variations of them each has their own use. Anyhow, here some general ideas. I'll also add a data structure, an array:
Some general concepts:
- locality: the way a computer works, things are much faster if they are close to each other in memory. The more you jump around in memory the worse the performance.
- things to consider: element access, insertion/deletion, search, scalability/load balancing. Your choice of data structure should be based on which of the three things are most important and will have the greatest effect on performance.
- array: arrays are typically fixed size and implemented as contiguous blocks of memory. If you know the fixed size of the data coming in, and don't do insertions or deletions in the middle of the array, and know the index of the element you want arrays can't be beat. Arrarys are bad with everything except element access.
- linked list: linked lists are usually implemented with pointers. linked lists are great for a lot of insertions and deletions. Inserting in the middle is not so bad. A linked list is great for making sorted lists. Element access is slow since it has to loop thru the list.
- tree: Trees are the best at load balancing. Element access is faster than linked lists. The only draw back is that insertions and deletions might be slow, since the tree might have to rebalance itself.
- hashtable: data goes in with a pair (key, value). hashtables are usually great everything but it can be awful at everything depending on your hash algorithm.
- stack: a stack is a data structure where stuff that goes in first, comes out last. ie. a stack of dishes in your sink.
The above data structures also have different ways of accessing data.
Example uses:
- array: a matrix
- linked list: alphabetical sorted ordered list
- tree: b+ trees are used in databases
- hashtable: something like a DNS lookup where a string maps to an ip address
- stack: to remember function calls in programming languages. in C if you call a function, which calls a function, etc. these function calls are put on a stack. the function called last finishes first and gets popped off the stack. this continues until the originating function eventually gets popped off the stack and the program ends.
Hope this helps.