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:
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#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)   // gives an error "2 arguments are required, 1 provided"
   }
}

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);
   return out;
}
Never mind, I did find the solution and it works now.
Topic archived. No new replies allowed.