begin quoting Andrew Lentvorski as of Mon, Feb 18, 2008 at 04:02:37PM -0800: > SJS wrote: > > >>Of course, doing tree-based algorithms in Java and C tends to be a > >>painful experience. > > > >Um.... why? > > > >And do you have an example? > > Anything where the leaf and stem nodes are not identical and have no > predetermined depth. R-trees are a good example.
Hm. I still don't see, but then, I've never *used* R-trees. If it's just leaves and internal nodes, that's not really a problem, as it is only two sorts of nodes. That's simple enough to deal with, so I'm going to assume you're talking about more complicated trees. If it's arbitrary -- say, a tree of lists, sets, maps, arrays, with various sorts of Objects on the leaves, then yeah, but that's bound to be nasty no matter what the language. Even in a dynamically typed language, if I'm processing a node, I have to know what type of node it is so that I know what to do with it. In C, you can have a tree of void* where you have no information about the child nodes, and that indeed is something annoying; Java provides runtime information about the type, and avoids that problem. But then, in C, you don't allow nodes to be of any type other than the tree's node. You don't use void* to point at the children, even if you use void* to point at the data. So traversing the tree with a recursive algorithm is still simple -- the traversal is separate from the processing. > You wind up having lots of casting to Object or you wind up with some > nasty object inheritance hierarchies. Casting from Object can be bypassed by using double-dispatch, but that involves "callbacks", which you seem to object to. (And anyone using object inheritance for a tree is just ASKING for trouble; that's why we have interfaces.) I'm still in the dark. I'll think about R-trees some more. > >All I can think of is that in C (and Java), you have to define your > >tree data structure, or you have to use a clunky generic predefined > >one. It's not until I have to play with XML that I start feeling pain > >with tree-structures in Java or C, but I suspect that's a library > >misdesign problem. > > Well, part of it is the fact that most XML libraries in C and Java > eventually flip themselves inside out in order to be iterative to avoid > stack faults. Suddenly, you can't access things in a recursive fashion > and have to start doing things like callbacks, iterators, etc. I tend to favor XPath. I try to avoid recursion (when working with XML) entirely, because I mostly don't care about the structure anymore. This probably explains why I find XML so painful. . . :) -- Say otherwise, but I like Kool-Aide However, I want to choose the flavor. Stewart Stremler -- [email protected] http://www.kernel-panic.org/cgi-bin/mailman/listinfo/kplug-lpsg
