Skip to content

Commit 0758227

Browse files
committedJul 31, 2020
Updated BST Tree
Added methods for printing PreOrder, InOrder and PostOrder traversals of the BST tree
1 parent bc67447 commit 0758227

File tree

1 file changed

+52
-1
lines changed

1 file changed

+52
-1
lines changed
 

‎BST/BST.hpp

+52-1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,27 @@ class BST
5959
return getValue(this->root, key);
6060
}
6161

62+
63+
void printPreorder()
64+
{
65+
preOrder (this->root);
66+
cout << endl;
67+
}
68+
69+
70+
void printInorder()
71+
{
72+
inOrder (this->root);
73+
cout << endl;
74+
}
75+
76+
77+
void printPostorder()
78+
{
79+
postOrder (this->root);
80+
cout << endl;
81+
}
82+
6283
protected:
6384

6485
node <T, U>* root;
@@ -127,7 +148,7 @@ class BST
127148
else if (n->key > key) return getValue (n->left, key);
128149
else return n->value;
129150
}
130-
151+
131152

132153
void destroy (node <T, U>* &n)
133154
{
@@ -136,4 +157,34 @@ class BST
136157

137158
delete n;
138159
}
160+
161+
162+
void preOrder(node <T, U>* n)
163+
{
164+
if (n==NULL) return;
165+
166+
cout<<"("<<n->key<<","<<n->value<<"), ";
167+
preOrder (n->left);
168+
preOrder (n->right);
169+
}
170+
171+
172+
void inOrder (node <T, U>* n)
173+
{
174+
if (n==NULL) return;
175+
176+
inOrder (n->left);
177+
cout<<"("<<n->key<<","<<n->value<<"), ";
178+
inOrder (n->right);
179+
}
180+
181+
182+
void postOrder(node <T, U>* n)
183+
{
184+
if (n==NULL) return;
185+
186+
preOrder (n->left);
187+
preOrder (n->right);
188+
cout<<"("<<n->key<<","<<n->value<<"), ";
189+
}
139190
};

0 commit comments

Comments
 (0)