-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathtree.cpp
131 lines (113 loc) · 3.28 KB
/
tree.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
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
#include <iostream>
#include <list>
using namespace std;
struct Node {
int val;
Node* left;
Node* right;
Node(int value) : val(value), left(nullptr), right(nullptr) {}
};
class BinaryTree {
public:
Node* root;
BinaryTree() : root(nullptr) {}
void insert(int value, char pos) {
if (root == nullptr) {
root = new Node(value);
} else {
insertRec(root, value, pos);
}
}
void breadthFirstTraversal() const {
if (!root) return;
list<Node*> queue;
queue.push_back(root);
while (!queue.empty()) {
Node* current = queue.front();
cout << current->val << " ";
queue.pop_front();
if (current->left) queue.push_back(current->left);
if (current->right) queue.push_back(current->right);
}
}
void preorderTraversal(Node* node) const {
if (node) {
cout << node->val << " ";
preorderTraversal(node->left);
preorderTraversal(node->right);
}
}
void inorderTraversal(Node* node) const {
if (node) {
inorderTraversal(node->left);
cout << node->val << " ";
inorderTraversal(node->right);
}
}
void postorderTraversal(Node* node) const {
if (node) {
postorderTraversal(node->left);
postorderTraversal(node->right);
cout << node->val << " ";
}
}
private:
void insertRec(Node* current, int value, char pos) {
if (pos == 'l') {
if (current->left == nullptr) {
current->left = new Node(value);
} else {
cout << "Left child already exists. Choose another position.\n";
}
} else if (pos == 'r') {
if (current->right == nullptr) {
current->right = new Node(value);
} else {
cout << "Right child already exists. Choose another position.\n";
}
} else {
cout << "Invalid position. Use 'l' for left or 'r' for right.\n";
}
}
};
int main() {
BinaryTree tree;
int choice, value;
char pos;
cout << "Enter the value of root node: ";
cin >> value;
tree.insert(value, 'r'); // Insert root
do {
cout << "\n1. Insert\n2. Breadth First Traversal\n3. Preorder Traversal\n4. Inorder Traversal\n5. Postorder Traversal\n0. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter the value to be inserted: ";
cin >> value;
cout << "Left or Right of Root (l/r): ";
cin >> pos;
tree.insert(value, pos);
break;
case 2:
tree.breadthFirstTraversal();
break;
case 3:
tree.preorderTraversal(tree.root);
break;
case 4:
tree.inorderTraversal(tree.root);
break;
case 5:
tree.postorderTraversal(tree.root);
break;
case 0:
cout << "Exiting...\n";
break;
default:
cout << "Invalid choice. Please try again.\n";
break;
}
} while (choice != 0);
return 0;
}