-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.cpp
More file actions
187 lines (147 loc) · 3.89 KB
/
code.cpp
File metadata and controls
187 lines (147 loc) · 3.89 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
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <iostream>
#include <queue>
#include <stack>
using namespace std;
// Implementing a Node structure for the binary search tree
struct Node {
int data;
Node* left;
Node* right;
Node(int value) {
data = value;
left = nullptr;
right = nullptr;
}
};
// Function to insert a value into the binary search tree
Node* insert(Node* root, int value) {
if (root == nullptr) {
return new Node(value);
}
if (value < root->data) {
root->left = insert(root->left, value);
} else if (value > root->data) {
root->right = insert(root->right, value);
}
return root;
}
// Function to delete a node from the binary search tree
Node* deleteNode(Node* root, int key) {
if (root == nullptr) {
return root;
}
if (key < root->data) {
root->left = deleteNode(root->left, key);
} else if (key > root->data) {
root->right = deleteNode(root->right, key);
} else {
if (root->left == nullptr) {
Node* temp = root->right;
delete root;
return temp;
} else if (root->right == nullptr) {
Node* temp = root->left;
delete root;
return temp;
}
Node* temp = root->right;
while (temp->left != nullptr) {
temp = temp->left;
}
root->data = temp->data;
root->right = deleteNode(root->right, temp->data);
}
return root;
}
// Function to update a node's value in the binary search tree
void updateNode(Node* root, int oldValue, int newValue) {
root = deleteNode(root, oldValue);
root = insert(root, newValue);
}
// Pre-order traversal using stack
void preOrder(Node* root) {
if (root == nullptr) return;
stack<Node*> stk;
stk.push(root);
while (!stk.empty()) {
Node* curr = stk.top();
stk.pop();
cout << curr->data << " ";
if (curr->right) stk.push(curr->right);
if (curr->left) stk.push(curr->left);
}
}
// Post-order traversal using stack
void postOrder(Node* root) {
if (root == nullptr) return;
stack<Node*> stk;
stk.push(root);
stack<int> out;
while (!stk.empty()) {
Node* curr = stk.top();
stk.pop();
out.push(curr->data);
if (curr->left) stk.push(curr->left);
if (curr->right) stk.push(curr->right);
}
while (!out.empty()) {
cout << out.top() << " ";
out.pop();
}
}
// In-order traversal using stack
void inOrder(Node* root) {
stack<Node*> stk;
Node* curr = root;
while (curr != nullptr || !stk.empty()) {
while (curr != nullptr) {
stk.push(curr);
curr = curr->left;
}
curr = stk.top();
stk.pop();
cout << curr->data << " ";
curr = curr->right;
}
}
// Level order traversal using queue
void levelOrder(Node* root) {
if (root == nullptr) return;
queue<Node*> q;
q.push(root);
while (!q.empty()) {
Node* curr = q.front();
q.pop();
cout << curr->data << " ";
if (curr->left) q.push(curr->left);
if (curr->right) q.push(curr->right);
}
}
int main() {
Node* root = nullptr;
// Insert values into the binary search tree
root = insert(root, 50);
root = insert(root, 30);
root = insert(root, 20);
root = insert(root, 40);
root = insert(root, 70);
root = insert(root, 60);
root = insert(root, 80);
// Delete node with value 20
root = deleteNode(root, 20);
// Update node with value 30 to 35
updateNode(root, 30, 35);
cout << "Pre-order traversal: ";
preOrder(root);
cout << endl;
cout << "Post-order traversal: ";
postOrder(root);
cout << endl;
cout << "In-order traversal: ";
inOrder(root);
cout << endl;
cout << "Level order traversal: ";
levelOrder(root);
cout << endl;
return 0;
}