views:

349

answers:

7

Hello, I am currently still in school and taking a class on implementing data structures in c++. In my spare time I enjoy programming in "higher level" languages (mostly Ruby with some c#).

So since these higher level languages manage the memory for you, what would you use data structures for? I can understand the need for queues and stacks but would you ever need to use a binary tree in Ruby? or a 2-3-4 tree? Why?

Thanks.

+6  A: 

So since these higher level languages manage the memory for you, what would you use data structures for?

The main reason for using a data structure is not about garbage collection. But it is about storing data in a way that is efficient in some way. So what matters most is HOW you are organizing the data. Which is exactly what the language can't automatically figure out for you.

Sure the high level language will come with several preloaded data structures (and you should 100% use these preloaded data structures when they are provided instead of making your own), but not all data structures are provided that you may need.

Data structures organize the storage of memory in some way so that the algorithms that run on them can be implemented giving efficient results.

For most tasks you wouldn't need to implement your own data structures. But this depends fully on what you are coding.

I can understand the need for queues and stacks but would you ever need to use a binary tree in Ruby?

There are a lot of examples for using a binary tree, but not in common every day projects, for example you may need to implement huffman coding.

Other data structures can be used to have the space savings and fast lookups of using a trie, or you may need to store a LOT of data with fast lookup by using a btree. Several data structures have specific uses and are optimized for different things. Whether the language is modern or not and whether it has garbage collection or not doesn't change that.

The trend though, is that custom implemented data structures are coded less, and thought about less. A similar argument happens with common algorithms. In more modern languages, like LINQ you simply specify to sort. You don't actually say how to sort.

Brian R. Bondy
It's the data structures that make a language "high level".
S.Lott
@S.Lott: Amongst several other things (type inference, garbage collection, ...), but that's another question/answer.
Brian R. Bondy
+1  A: 

In a word, yes.

Although GC will simplify your work, there are still many situations where the ability to organize the data appropriately will let you write a more effective (or performant) program.

joel.neely
+1  A: 

You would use a binary tree if you want to be able to efficiently find something in a sorted collection, just the same as in any other language. Data structure choice is important in any language. It doesn't become less important in higher level languages, except to the degree that the higher level language provides large libraries that incorporate thoughtful intelligent design that handles this for you, in which case you don't have to design the data structure yourself, but you do have to know which built-in data structure to use.

The Java Collections framework is extensive and wonderful, but if you make use of the wrong (inappropriate) Collections object, your program's performance will suffer.

Also, some data structures are more space-efficient than others. Yes, memory is cheap and modern GC'd languages will manage memory for you, but if you are handling huge sets of data, then data structures will matter if you can make something more memory-efficient.

Eddie
+4  A: 

In my experience with Python (superficially similar to Ruby), I've never had to implement a binary tree or a hashmap or anything like that. But the reason why has very little to do with managed memory. There are implementations of the most useful structures, like dictionaries (hashmaps) and lists, in the standard library; for speed and efficiency, they're (at least partially) implemented in whatever lower-level language the interpreter is written in and they will almost certainly outperform any custom implementation you may come up with.

David Zaslavsky
A: 

I program professionally in Ruby and I've found that a thorough knowledge of data structures has been thoroughly helpful, though I've never had to implement a tree with any particular performance characteristics outside of school (though I have had to implement tree-like structures.)

The same data structures that seem irrelevant when implemented on arrays on a micro scale because the language provides them to you tend to re-occur in the "enterprise" when you have to scale across multiple machines or coordinate among disparate services. Knowing what options are available to you when writing these things is a helpful tool to have in your back pocket.

Brian Guthrie
A: 

You use these data structures for exactly the same problems as in other languages. The difference is only that modern languages resp. their standard libraries often already have very efficient implementations for them. Nevertheless, you still have to know them to use them. "Higher level" doesn't mean actual magic.

Svante
+1  A: 

Most of the time

You use them, but you don't need to implement them; they're implemented for you.

For example, a dictionary could typically be implemented internally as a combination of hashmaps and binary trees. The list could be implemented as a linked list, etc.

But sometimes

You might wanna implement a datastructure, if for example, you're storing huge amounts of data (say, millions of lines of text). I myself haven't yet faced a situation where I would need to do such a thing, but thought about projects that might need it. (for example, to efficiently open and edit a huge text file, etc).

hasen j