-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem3.cpp
More file actions
136 lines (104 loc) · 3.48 KB
/
Problem3.cpp
File metadata and controls
136 lines (104 loc) · 3.48 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
#include <iostream>
using namespace std;
class Node {
public:
int id;
string name;
float cgpa;
Node* left;
Node* right;
Node(int studentId, string studentName, float studentCGPA) {
id = studentId;
name = studentName;
cgpa = studentCGPA;
left = NULL;
right = NULL;
}
};
class StudentBST {
private:
Node* root;
Node* insertRecursive(Node* current, int studentId, string studentName, float studentCGPA) {
if (current == NULL) {
return new Node(studentId, studentName, studentCGPA);
}
if (studentId < current->id) {
current->left = insertRecursive(current->left, studentId, studentName, studentCGPA);
} else if (studentId > current->id) {
current->right = insertRecursive(current->right, studentId, studentName, studentCGPA);
}
return current;
}
Node* searchRecursive(Node* current, int studentId) {
if (current == NULL || current->id == studentId) {
return current;
}
if (studentId < current->id) {
return searchRecursive(current->left, studentId);
} else {
return searchRecursive(current->right, studentId);
}
}
Node* findMin(Node* current) {
while (current->left != NULL) {
current = current->left;
}
return current;
}
Node* deleteRecursive(Node* current, int studentId) {
if (current == NULL) {
return current;
}
if (studentId < current->id) {
current->left = deleteRecursive(current->left, studentId);
} else if (studentId > current->id) {
current->right = deleteRecursive(current->right, studentId);
} else {
if (current->left == NULL) {
Node* temp = current->right;
delete current;
return temp;
} else if (current->right == NULL) {
Node* temp = current->left;
delete current;
return temp;
}
Node* temp = findMin(current->right);
current->id = temp->id;
current->name = temp->name;
current->cgpa = temp->cgpa;
current->right = deleteRecursive(current->right, temp->id);
}
return current;
}
public:
StudentBST() {
root = NULL;
}
void insert(int studentId, const string& studentName, float studentCGPA) {
root = insertRecursive(root, studentId, studentName, studentCGPA);
}
Node* search(int studentId) {
return searchRecursive(root, studentId);
}
void remove(int studentId) {
root = deleteRecursive(root, studentId);
}
};
int main() {
StudentBST studentTree;
studentTree.insert(1234, "Abu Bakar", 3);
studentTree.insert(4567, "siddique", 3.5);
studentTree.insert(7890, "John", 4);
Node* foundStudent = studentTree.search(4567);
if (foundStudent != NULL) {
cout << "Student found: ID = " << foundStudent->id
<< ", Name = " << foundStudent->name
<< ", CGPA = " << foundStudent->cgpa << endl;
} else {
cout << "Student with ID not found." << endl;
}
int deleteId = 456;
studentTree.remove(deleteId);
cout << "Deleted student with ID " << deleteId <<endl;
}