I'm trying to create a program to work with binary search trees and my instructor has this function set up with this declaration, which must stay the same.
void BSTType<T>::traverse(std::ostream& out, TravType trav) const{
this is currently how i have it implemented, but what is the std::ostream& out used for and how do i use this is my main program? I don't understand why we need this as a parameter and not the current tree I am working with. please help soon. this is due at 8pm and my instructor won't help. if more info is needed just let me know in comments or something.
template <class T>
void BSTType<T>::traverse(std::ostream& out, TravType trav) const{
if(trav==IN)
inorder(root);
else if(trav=PRE)
preorder(root);
else
postorder(root);
}
this is the idea i have for calling this function from my main program
tree.traverse(???, IN);
this should output the tree using inorder traversal by calling the inorder(root) function. I don't understand what goes where the question marks are
here are my functions that traverse can call, depending on what travType the user enters
void BSTType<T>::inorder(BTNodeType<T>* node) const{
if (node!= NULL){
inorder(node->left);
std::cout << node->item << " ";
inorder(node->right);
}
}
template <class T>
void BSTType<T>::postorder(BTNodeType<T>* node) const{
if (node!= NULL){
postorder(node->left);
postorder(node->right);
std::cout << node->item << " ";
}
}
template <class T>
void BSTType<T>::preorder(BTNodeType<T>* node) const{
if(node!=NULL){
std::cout << node->item<< " ";
preorder(node-> left);
preorder(node-> right);
}
}
edit: for some reason this function isn't copying my tree correctly, anyone know why?
template <class T>
void BSTType<T>::copy(BTNodeType<T>*& node1, BTNodeType<T>* node2){
if(node2==NULL)
node1=NULL;
else{
node1=new BTNodeType<T>;
node1->item=node2->item;
copy(node1->left, node2->left);
copy(node1->right, node2->right);
}
}