-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryTree_Basic.cpp
75 lines (59 loc) · 1.41 KB
/
BinaryTree_Basic.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
66
67
68
69
70
71
72
73
74
75
#include<bits/stdc++.h>
using namespace std;
struct node {
int data;
node *left;
node *right;
} *root;
class Binary {
public:
Binary() {
root = NULL;
}
node *create() {
int x;
cout << "\n Enter the data (-1 for no sub node): ";
cin >> x;
node *tmp = new node;
tmp->data = x;
if (x == -1)
return NULL;
cout << "\n Enter the left child of " << x << ": ";
tmp->left = create();
cout << "\n Enter the right child of " << x << ": ";
tmp->right = create();
return tmp;
}
void preorder(node *root) {
if (root != NULL) {
cout << root->data << " ";
preorder(root->left);
preorder(root->right);
}
}
void inorder(node *root) {
if (root != NULL) {
inorder(root->left);
cout << root->data << " ";
inorder(root->right);
}
}
void postorder(node *root) {
if (root != NULL) {
preorder(root->left);
preorder(root->right);
cout << root->data << " ";
}
}
};
int main() {
Binary o;
root = o.create();
cout << "\n Inorder of given tree is: ";
o.inorder(root);
cout << "\n Preorder of given tree is: ";
o.preorder(root);
cout << "\n Postorder of given tree is: ";
o.postorder(root);
return 0;
}