Hi, guys, need a little bit help with Binary Search Tree. I have to write a function for traversal through the binary tree in preorder, postorder or inorder. What I understood, this function should call the private print function and print the tree in selected order. My problem is, I don't understand what I need to put in function definition for "void setTraverse" function. In the program I have now it gives me error: "note: candidate expects 2 arguments, 1 provided".
Thank you in advance for any help or advise
Here is my header file
Code:
#ifndef BST__H
#define BST__H
#include "NodeTypeBST.h"
#include <iostream>
enum TravType {PRE, IN, POST};
template<class T>
class BST
{
public:
BST();
BST(const BST<T>&);
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<T>& operator= (const BST<T>&);
template <class U>
friend std::ostream& operator << (std::ostream&, const BST<U>&);
protected:
NodeTypeBST<T>* root;
size_t count;
TravType trav;
private:
void erase(const T&, NodeTypeBST<T>*&);
bool find(const T&, NodeTypeBST<T>*&) const;
void insert(const T&, NodeTypeBST<T>*&);
void copyTree(NodeTypeBST<T>*&, NodeTypeBST<T>*);
void destroy(NodeTypeBST<T>*&);
const T& predecessor(NodeTypeBST<T>*);
void print(std::ostream&, NodeTypeBST<T>*) const;
};
template<class T>
BST<T>::BST()
{
count = 0;
root = NULL;
trav = IN;
}
template<class T>
BST<T>::BST(const BST<T>& src)
{
copyTree(root, src.root);
count = src.count;
trav = src.trav;
}
template<class T>
BST<T>::~BST()
{
destroy(root);
}
template<class T>
bool BST<T>::empty() const
{
return root==NULL;
}
template<class T>
void BST<T>::erase()
{
destroy(root);
root ==NULL;
count = 0;
trav = IN;
}
template<class T>
void BST<T>::erase(const T& item)
{
erase(item, root);
}
template<class T>
void BST<T>::erase(const T& item, NodeTypeBST<T>*& node)
{
if(node !=NULL)
{
if(item == node->item)
{
NodeTypeBST<T>*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<class T>
bool BST<T>::find(const T& item) const
{
return find(item, root);
}
template<class T>
bool BST<T>::find(const T& item, NodeTypeBST<T>*& 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<class T>
void BST<T>::insert(const T& item)
{
insert(item, root);
}
template<class T>
void BST<T>::insert(const T& item, NodeTypeBST<T>*& node)
{
if(node == NULL)
{
node = new NodeTypeBST<T>;
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<class T>
void BST<T>::setTraverse (TravType node)
{
if(node!=NULL)
{
print(node);
}
}
template<class T>
size_t BST<T>::size() const
{
return count;
}
template<class T>
const BST<T>& BST<T>::operator= (const BST<T>& src)
{
if(this!=&src)
{
destroy(root);
copyTree(root, src.root);
count = src.count;
trav = src.trav;
}
}
template<class T>
void BST<T>::copyTree(NodeTypeBST<T>*& srcnode, NodeTypeBST<T>* destnode)
{
if(srcnode!=NULL)
{
destnode = new NodeTypeBST<T>;
destnode->item = srcnode->item;
copyTree(destnode->left, srcnode->left);
}
}
template<class T>
void BST<T>::destroy(NodeTypeBST<T>*& node)
{
if(node!=NULL)
{
destroy(node->left);
destroy(node->right);
delete node;
node = NULL;
}
}
template<class T>
const T& BST<T>::predecessor(NodeTypeBST<T>* node)
{
while(node->right!=NULL)
{
node = node->right;
}
return node->item;
}
template<class T>
void BST<T>::print(std::ostream& out, NodeTypeBST<T>* 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 <class U>
std::ostream& operator << (std::ostream& out, const BST<U>& t)
{
t.print(out, t.root, 1);
return out;
}
#endif // BST__H