### Traversal in Binary Search Tree

Hi, guys
I need a help with traversal of binary search tree. In my header file there is a function setTraversal (public) and private print file. As I understood from teacher's explanation, my setTraversal function should call the recursive print function and print the list depending on selected order (pre,in or post-order). I still cannot get my head around what should be in setTraversal function definition. All resources I read last two days explain each order separately (preorder, inorder, postorder). How can I combine them? Would greatly appreciate if you could show me right direction.
Here is my code:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263`` ``````#include "NodeTypeBST.h" #include enum TravType {PRE, IN, POST}; template class BST { public: BST(); BST(const BST&); virtual ~BST(); bool empty() const; void erase(); void erase(const T&); bool find(const T&) const; void insert(const T&); size_t size() const; void setTraverse (TravType); const BST& operator= (const BST&); template friend std::ostream& operator << (std::ostream&, const BST&); protected: NodeTypeBST* root; size_t count; TravType trav; private: void erase(const T&, NodeTypeBST*&); bool find(const T&, NodeTypeBST*&) const; void insert(const T&, NodeTypeBST*&); void copyTree(NodeTypeBST*&, NodeTypeBST*); void destroy(NodeTypeBST*&); const T& predecessor(NodeTypeBST*); void print(std::ostream&, NodeTypeBST*) const; }; template BST::BST() { count = 0; root = NULL; trav = IN; } template BST::BST(const BST& src) { copyTree(root, src.root); count = src.count; trav = src.trav; } template BST::~BST() { destroy(root); } template bool BST::empty() const { return root==NULL; } template void BST::erase() { destroy(root); root ==NULL; count = 0; trav = IN; } template void BST::erase(const T& item) { erase(item, root); } template void BST::erase(const T& item, NodeTypeBST*& node) { if(node !=NULL) { if(item == node->item) { NodeTypeBST*temp = node; if(node->right==NULL) { node = node->left; delete temp; --count; } else if (node->left == NULL) { node = node->right; delete temp; --count; } else { node->item = predecessor(node->left); erase(node->item, node->left); } } else if (item < node-> item) { erase(item, node->left); } else { erase(item,node->right); } } } template bool BST::find(const T& item) const { return find(item, root); } template bool BST::find(const T& item, NodeTypeBST*& node) const { if(node!=NULL) { if(item == node->item) { return true; } else if(item < node->item) { return find(item, node->left); } else { return find(item, node->right); } } else return false; } template void BST::insert(const T& item) { insert(item, root); } template void BST::insert(const T& item, NodeTypeBST*& node) { if(node == NULL) { node = new NodeTypeBST; node->item = item; node->left = node->right = NULL; ++count; } else if (item < node->item) { insert(item, node->left); } else if (item > node->item) { insert(item, node->right); } } template void BST::setTraverse (TravType node) { if(node!=NULL) { print(node) // gives an error "2 arguments are required, 1 provided" } } template size_t BST::size() const { return count; } template const BST& BST::operator= (const BST& src) { if(this!=&src) { destroy(root); copyTree(root, src.root); count = src.count; trav = src.trav; } } template void BST::copyTree(NodeTypeBST*& srcnode, NodeTypeBST* destnode) { if(srcnode!=NULL) { destnode = new NodeTypeBST; destnode->item = srcnode->item; copyTree(destnode->left, srcnode->left); } } template void BST::destroy(NodeTypeBST*& node) { if(node!=NULL) { destroy(node->left); destroy(node->right); delete node; node = NULL; } } template const T& BST::predecessor(NodeTypeBST* node) { while(node->right!=NULL) { node = node->right; } return node->item; } template void BST::print(std::ostream& out, NodeTypeBST* node) const { if(node!=NULL) { switch(trav) { case PRE: out << node->item << " "; print(out, node->left); print(out, node->right); break; case IN: print(out, node->left); out << node->item << " "; print(out, node->right); break; case POST: print(out, node->left); print(out, node->right); out << node->item << " "; } } } template std::ostream& operator << (std::ostream& out, const BST& t) { t.print(out, t.root); return out; }``````
Never mind, I did find the solution and it works now.
Topic archived. No new replies allowed.