File tree 1 file changed +52
-1
lines changed
1 file changed +52
-1
lines changed Original file line number Diff line number Diff line change @@ -59,6 +59,27 @@ class BST
59
59
return getValue (this ->root , key);
60
60
}
61
61
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
+
62
83
protected:
63
84
64
85
node <T, U>* root;
@@ -127,7 +148,7 @@ class BST
127
148
else if (n->key > key) return getValue (n->left , key);
128
149
else return n->value ;
129
150
}
130
-
151
+
131
152
132
153
void destroy (node <T, U>* &n)
133
154
{
@@ -136,4 +157,34 @@ class BST
136
157
137
158
delete n;
138
159
}
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
+ }
139
190
};
You can’t perform that action at this time.
0 commit comments