-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTree traversing using array.cpp
72 lines (62 loc) · 1.99 KB
/
Tree traversing using array.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
#include <bits/stdc++.h>
using namespace std;
void inorderTraversal(const vector<int>& arr, int index) {
if (index >= arr.size()) {
return;
}
inorderTraversal(arr, 2 * index + 1); // Left child
cout << arr[index] << " "; // Current node
inorderTraversal(arr, 2 * index + 2); // Right child
}
void preorderTraversal(const vector<int>& arr, int index) {
if (index >= arr.size()) {
return;
}
cout << arr[index] << " "; // Current node
preorderTraversal(arr, 2 * index + 1); // Left child
preorderTraversal(arr, 2 * index + 2); // Right child
}
void postorderTraversal(const vector<int>& arr, int index) {
if (index >= arr.size()) {
return;
}
postorderTraversal(arr, 2 * index + 1); // Left child
postorderTraversal(arr, 2 * index + 2); // Right child
cout << arr[index] << " "; // Current node
}
int main() {
vector<int> arr = {10, 5, 15, 2, 8, 13, 17}; // Sample binary tree in array form
int choice;
do {
cout << "\nMenu:\n";
cout << "1. Inorder Traversal\n";
cout << "2. Preorder Traversal\n";
cout << "3. Postorder Traversal\n";
cout << "4. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Inorder Traversal: ";
inorderTraversal(arr, 0);
cout << endl;
break;
case 2:
cout << "Preorder Traversal: ";
preorderTraversal(arr, 0);
cout << endl;
break;
case 3:
cout << "Postorder Traversal: ";
postorderTraversal(arr, 0);
cout << endl;
break;
case 4:
cout << "Exiting...\n";
break;
default:
cout << "Invalid choice. Please try again.\n";
}
} while (choice != 4);
return 0;
}