-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSource.cpp
More file actions
78 lines (68 loc) · 1.36 KB
/
Source.cpp
File metadata and controls
78 lines (68 loc) · 1.36 KB
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
#include<iostream>
using namespace std;
struct Node {
char data;
struct Node *left;
struct Node *right;
};
void Preorder(struct Node *root) {
if (root == NULL) return;
printf("%c ", root->data);
Preorder(root->left);
Preorder(root->right);
}
void Inorder(Node *root) {
if (root == NULL) return;
Inorder(root->left);
printf("%c ", root->data);
Inorder(root->right);
}
void Postorder(Node *root) {
if (root == NULL) return;
Postorder(root->left);
Postorder(root->right);
printf("%c ", root->data);
}
Node* Insert(Node *root, char data) {
if (root == NULL) {
root = new Node();
root->data = data;
root->left = root->right = NULL;
}
else if (data <= root->data)
root->left = Insert(root->left, data);
else
root->right = Insert(root->right, data);
return root;
}
int main() {
/*
Creating an example tree
M
/ \
B Q
/ \ \
A C Z
*/
Node* root = NULL;
root = Insert(root, 'A');
root = Insert(root, 'B');
root = Insert(root, 'D');
root = Insert(root, 'C');
root = Insert(root, 'E');
root = Insert(root, 'F');
root = Insert(root, 'I');
root = Insert(root, 'H');
//Print Nodes in Preorder.
cout << "Preorder: ";
Preorder(root);
cout << "\n";
//Print Nodes in Inorder
cout << "Inorder: ";
Inorder(root);
cout << "\n";
//Print Nodes in Postorder
cout << "Postorder: ";
Postorder(root);
cout << "\n";
}