开发者

How do you display a binary search tree?

I'm being asked to disp开发者_运维技巧lay 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.


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


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?


Binary Search Tree is a just a tree that you can print as a classic ways (preorder, inorder, preorder)

for example :

print(node){
    if(node != null){
       printOut(root.value);
       print(node.left);
       print(node.right);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜