views:

79

answers:

1

I'm trying to figure out how to do a preorder traversal of a Btree. I know that generally preorder traversal works like this:

preorder(node)
{
print value in node
preorder(left child)
preorder(right child)
}

What's confusing to me is how to make this work with a Btree, since in each node there are multiple values and multiple child pointers. When printing values, do all the values in the node get printed before descending into the left child?

Each node looks like this:

child1 value1 child2 value2 child3 value3 child4

Also, why would anyone want to do a preorder traversal of a Btree, since an inorder traversal is what will display the values in ascending order?

+2  A: 

Print all the values in the current node in some defined order (which is up to you, really, though left-to-right is a sensible default) then visit each child node (again, the order is up to you).

Marcelo Cantos