English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

3 answers

I think Darkhorse meant to say "inorder" traversal (left, root, right) will return the nodes of a binary search tree in sorted order. This is because all nodes in the left subtree should come before the root which should come before the nodes in the right subtree. Darkhorse is right that all have the same time complexity, O(n), where n is the number of nodes in the tree.

For many applications any of the tree will do. To turn a tree into a sorted array or list, you will want to use "inorder". You may want to use preorder or postorder for certain special cases if you are running an algorithm on the node of a tree and need to process a node before its children (in which case use "preorder") or need to process a node's children first (in that case use "postorder").

2007-01-27 17:47:07 · answer #1 · answered by Phineas Bogg 6 · 0 0

The answer depends on what you want to do.

I think they have the same complexity/efficiency.

If you have a sorted binary tree, pre-order is the one that will output the nodes in the correct order, though. (Assuming that the tree was sorted with the smaller nodes in the left subtree, i.e., using the less than function.)

2007-01-24 14:51:07 · answer #2 · answered by Anonymous · 0 0

Really depends on your application...

2007-01-24 14:46:25 · answer #3 · answered by mdigitale 7 · 0 0

fedest.com, questions and answers