views:

175

answers:

2

I'm being asked to display a binary search tree in sorted order. The nodes of the tree contain strings.

I'm not exactly sure what the best way is to attack this problem. Should I be traversing the tree and displaying as I go? Should I flatten the tree into an array and then use a sorting algorithm before I display?

I'm not looking for the actual code, just a guide where to go next.

+4  A: 

Check out your options for Tree Traversal, it's easier than you might think. Good luck :)

Stephen
+1 Excellent link, thank you. Clearly I was meant to use recursion here. I'm still having trouble with that one.
fakeit
@gakeit Don't have to use recursion, but it is probably the easiest way to start with. Otherwise you've got to build up a list of objects to process and whatever parameters they need (only advantage of this is that you avoid running out of stack space). Also, seeing how its homework, they almost definitely assigned to get some experience with recursion.
Grant Peters
+3  A: 

Is this a binary search tree (BST)? A "just" binary tree (not search) has no properties that would in any way help you (indeed, there may not be any order defined among the payloads!), but for a BST the situation is totally different (indeed the first wikipedia page I pointed to gives concise pseudocode (well, OK, Python;-) for in-order traversal of a BST -- not for just any binary tree of course.

So, did you omit the absolutely-crucial word search between "binary" and "tree" in your question and tag?

Alex Martelli
ops. remedied. thank you.
fakeit
oh, and the python "callback"...passing functions as params still hurts my head. i'm clearly new at this.
fakeit