-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem2.cpp
More file actions
86 lines (65 loc) · 2.11 KB
/
Problem2.cpp
File metadata and controls
86 lines (65 loc) · 2.11 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
#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 = nullptr;
right = nullptr;
}
};
class StudentBST {
private:
Node* root;
Node* insertRecursive(Node* current, int studentId, const string& studentName, float studentCGPA) {
if (current == nullptr) {
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 == nullptr || current->id == studentId) {
return current;
}
if (studentId < current->id) {
return searchRecursive(current->left, studentId);
} else {
return searchRecursive(current->right, studentId);
}
}
public:
StudentBST() {
root = nullptr;
}
void insert(int studentId,string studentName, float studentCGPA) {
root = insertRecursive(root, studentId, studentName, studentCGPA);
}
Node* search(int studentId) {
return searchRecursive(root, studentId);
}
};
int main() {
StudentBST studentTree;
studentTree.insert(123, "Siddique", 3);
studentTree.insert(456, "Abu bakar siddique", 4);
Node* foundStudent = studentTree.search(456);
if (foundStudent != nullptr) {
cout << "Student found: ID = " << foundStudent->id
<< ", Name = " << foundStudent->name
<< ", CGPA = " << foundStudent->cgpa << endl;
} else {
cout << "Student with ID not found." << endl;
}
}