binary search tree printing

I am not sure how to use the parameters ostream and enum type in a print function. Can someone tell me if this print function looks correct.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
enum TraversalOrderType {preorder, inorder, postorder};

template <class ItemType>
void Tree<ItemType>::print(ostream & Outstream, TraversalOrderType order)
{
	Outstream << printHelper(rootPtr, order);
}

template <class ItemType>
void printHelper(TreeNode< ItemType > * ptr, TraversalOrderType order)
{
	if (ptr != NULL)
	{
		switch(order)
		{
		case preorder:
			cout << ptr->data;
			printHelper(ptr->leftPtr, order);
			printHelper(ptr->rightPtr, order);
			break;
		case inorder:
			printHelper(ptr->leftPtr, order);
			cout << ptr->data;
			printHelper(ptr->rightPtr, order);
			break;
		case postorder:
			printHelper(ptr->leftPtr, order);
			printHelper(ptr->rightPtr, order);
			cout << ptr->data;
		}
	}
}

Is this the correct way to call the print function?
1
2
3
4
5
6
7
8
int main()
{
	Tree<int> tree1;

	tree1.print(cout, preorder);

	return 0;
}
Everything looks good except:
- You need to pass the out stream to printHelper so it knows where to output the data it reads (instead of always outputting it to cout)
- You shouldn't try to stream the results of printHelper to the out stream since that function doesn't return anything
On the prompt that I am given, the out stream is not passed to printHelper. If I make the print() function just call printHelper() instead of trying to stream the results, how would I use the ostream argument correctly.
Topic archived. No new replies allowed.