2

In a recent question I was encouraged to try using some basic data structures such as binary trees, red-black trees, et cetera, before tackling other things like quadtrees.

My experience in C is fairly limited and I am fearful of using pointers for anything but simple data (like 2D grids, image storage and strings), although I am familiar with referencing, malloc, realloc and other trivial actions, I am not used to the "hard" parts of C, which makes such structures hard to tackle from theory, and I don't want to just copy working code into this.

What I'd like to know, in order to tackle basic trees, is a practical application for them. Sort of an exercise with some guidelines (sort of "don't do this or you will kill performance" or "don't do that or this will leak memory"), just to be able to know the practical purpose. Even if I memorize the theory, I still don't know what sort of experiment to conduct in order to understand their application.

I am mostly attempting to use plain C, I don't really understand C++/# code when reading it, although I have certain mastery of the Lua language in case that helps.

So far I've been coding combining Lua for dictionary searches and designing data (and some logic parts) and left all video and audio storage, heavy math and "world" storage in C (using grid structures and a not-too-bruteforced collision detection approach (using a linear array to place objects in 1/24 of the map, nothing complex in code terms)). Because I could always rely on Lua's solid code for some functions, I neglected learning more of C and now I am paying for it with lack of knowledge.

So, to formulate a question: "What is the basic use case for data trees?" The only idea I have so far is using a splay tree to match strings (filenames?) to textures. Is that a valid use? Should I begin with that?

Community
  • 1
  • 1
roger_rales
  • 191
  • 1
  • 2
  • 7
  • We can make some random suggestions, but what do you like doing? You have developed applications before? What interests you? If you like the graphics side of things, there are tons of situations where trees are not just handy, but also necessary. But never mind the application, you will still need to dive into C. Pick up a good book and learn the language. There are not shortcuts. – Bart Aug 03 '11 at 14:20

1 Answers1

0

One of the uses for data trees was when writing a parser / compiler. After breaking up the source (Lexical analysis) and running the parsing (verifying grammar), we would build a tree structure of the source code (Syntactic tree), which was then repeatedly visited by the next parts of the compiler.

Another use of trees is when having to match if strings belong to a set of words very quickly, using very little memory, you can use a DAWG (Directed acyclic word graph).

Finally, a classic use is writing a solver for a Travelling Salesman problem using a data tree to store your cities in memory.

The classic Sedgewick Algorithms in C, Part 5: Graph Algorithms is full of examples, also if you have access to it.

NetSquirrel
  • 816
  • 6
  • 14