Skip to content

Commit f4124f4

Browse files
committed
Updated AVL Tree
Added methods for printing PreOrder, InOrder and PostOrder traversals of the AVL tree
1 parent 4c39dcd commit f4124f4

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

AVL Tree/AVL.hpp

+51
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,27 @@ class AVLTree : public BST <T, U>
5454
return getValue(this->root, key);
5555
}
5656

57+
58+
void printPreorder()
59+
{
60+
preOrder (this->root);
61+
cout << endl;
62+
}
63+
64+
65+
void printInorder()
66+
{
67+
inOrder (this->root);
68+
cout << endl;
69+
}
70+
71+
72+
void printPostorder()
73+
{
74+
postOrder (this->root);
75+
cout << endl;
76+
}
77+
5778
protected:
5879

5980
AVL_node <T, U>* root;
@@ -219,4 +240,34 @@ class AVLTree : public BST <T, U>
219240
else if (n->key > key) return getValue (n->left, key);
220241
else return n->value;
221242
}
243+
244+
245+
void preOrder(AVL_node <T, U>* n)
246+
{
247+
if (n==NULL) return;
248+
249+
cout<<"("<<n->key<<","<<n->value<<"), ";
250+
preOrder (n->left);
251+
preOrder (n->right);
252+
}
253+
254+
255+
void inOrder (AVL_node <T, U>* n)
256+
{
257+
if (n==NULL) return;
258+
259+
inOrder (n->left);
260+
cout<<"("<<n->key<<","<<n->value<<"), ";
261+
inOrder (n->right);
262+
}
263+
264+
265+
void postOrder(AVL_node <T, U>* n)
266+
{
267+
if (n==NULL) return;
268+
269+
preOrder (n->left);
270+
preOrder (n->right);
271+
cout<<"("<<n->key<<","<<n->value<<"), ";
272+
}
222273
};

0 commit comments

Comments
 (0)