-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathl62.cpp
More file actions
173 lines (140 loc) · 4.07 KB
/
l62.cpp
File metadata and controls
173 lines (140 loc) · 4.07 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include<iostream>
#include<queue>
using namespace std;
class node {
public:
int data;
node* left;
node* right;
node(int d) {
this->data = d;
this->left = NULL;
this->right = NULL;
}
};
// Function to build the tree recursively
node* buildTree(node* root) {
cout << "Enter the data: ";
int data;
cin >> data;
if (data == -1) { // Base case for stopping recursion
return NULL;
}
root = new node(data); // Create new node
// Recursively build left and right subtrees
cout << "Enter data for inserting in left of " << data << endl;
root->left = buildTree(root->left);
cout << "Enter data for inserting in right of " << data << endl;
root->right = buildTree(root->right);
return root;
}
// Level order traversal (BFS)
void levelOrderTraversal(node* root) {
if (root == NULL) return;
queue<node*> q;
q.push(root);
q.push(NULL); // Marker for end of level
while (!q.empty()) {
node* temp = q.front();
q.pop();
if (temp == NULL) {
// End of current level
cout << endl;
if (!q.empty()) {
q.push(NULL); // Add marker for the next level
}
} else {
// Print the current node
cout << temp->data << " ";
// Add left and right children
if (temp->left) {
q.push(temp->left);
}
if (temp->right) {
q.push(temp->right);
}
}
}
}
// Inorder traversal (Left, Root, Right)
void inorder(node* root) {
if (root == NULL) {
return;
}
inorder(root->left); // Visit left subtree
cout << root->data << " "; // Print root node
inorder(root->right); // Visit right subtree
}
// Preorder traversal (Root, Left, Right)
void preorder(node* root) {
if (root == NULL) {
return;
}
cout << root->data << " "; // Print root node
preorder(root->left); // Visit left subtree
preorder(root->right); // Visit right subtree
}
// Postorder traversal (Left, Right, Root)
void postorder(node* root) {
if (root == NULL) {
return;
}
postorder(root->left); // Visit left subtree
postorder(root->right); // Visit right subtree
cout << root->data << " "; // Print root node
}
// Function to build tree from level order input
node* buildFromLevelOrder(node* root) {
cout << "Enter data for root: ";
int data;
cin >> data;
if (data == -1) {
return NULL;
}
root = new node(data);
queue<node*> q;
q.push(root);
while (!q.empty()) {
node* temp = q.front();
q.pop();
// Input for left child
cout << "Enter left child for " << temp->data << ": ";
int leftData;
cin >> leftData;
if (leftData != -1) {
temp->left = new node(leftData);
q.push(temp->left);
}
// Input for right child
cout << "Enter right child for " << temp->data << ": ";
int rightData;
cin >> rightData;
if (rightData != -1) {
temp->right = new node(rightData);
q.push(temp->right);
}
}
return root;
}
int main() {
node* root = NULL;
// Choose either buildTree or buildFromLevelOrder for constructing the tree
// Building the tree recursively
root = buildTree(root);
// OR
// Building the tree from level order
// root = buildFromLevelOrder(root);
// Displaying the tree using inorder, preorder, postorder, and level order traversal
cout << "Inorder Traversal of the Binary Tree: ";
inorder(root);
cout << endl;
cout << "Preorder Traversal of the Binary Tree: ";
preorder(root);
cout << endl;
cout << "Postorder Traversal of the Binary Tree: ";
postorder(root);
cout << endl;
cout << "Level Order Traversal of the Binary Tree: " << endl;
levelOrderTraversal(root);
return 0;
}