-
Notifications
You must be signed in to change notification settings - Fork 368
/
tree_traversals.cpp
65 lines (61 loc) · 1.49 KB
/
tree_traversals.cpp
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
#include <bits/stdc++.h>
using namespace std;
class Tree
{
public:
int data;
Tree* left = NULL,*right = NULL;
// Constructor initialised
Tree(int x)
{
data = x;
left = NULL;
right = NULL;
}
};
void inorder_traversal (Tree *root)
{
if (root == NULL) return;
// Visit Left subtree
inorder_traversal(root -> left);
// Print the data
cout << root -> data <<" ";
// Visit right subtree
inorder_traversal(root -> right);
}
void postorder_traversal (Tree *root)
{
if (root == NULL) return;
// Visit Left subtree
postorder_traversal(root -> left);
// Visit right subtree
postorder_traversal(root -> right);
// Print the data
cout << root -> data << " ";
}
void preorder_traversal (Tree *root)
{
if (root == NULL) return;
// Print the data
cout << root -> data << " ";
// Visit Left subtree
preorder_traversal(root -> left);
// Visit right subtree
preorder_traversal(root -> right);
}
int main()
{
Tree *root = new Tree(17);
root -> left = new Tree(10);
root -> right = new Tree(11);
root -> left -> left = new Tree(7);
root -> right -> left = new Tree(27);
root -> right -> right = new Tree(9);
cout << "\nInorder Traversal is: ";
inorder_traversal(root);
cout << "\nPreorder Traversal is: ";
preorder_traversal(root);
cout << "\nPostorder Traversal is: ";
postorder_traversal(root);
return 0;
}