-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcourseBST.cpp
More file actions
60 lines (51 loc) · 1.38 KB
/
Copy pathcourseBST.cpp
File metadata and controls
60 lines (51 loc) · 1.38 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
#include "CourseBST.h"
#include <iostream>
CourseBST::CourseBST() : root(nullptr) {}
CourseBST::~CourseBST() {
freeRec(root);
}
CourseNode* CourseBST::insertRec(CourseNode* node, const Course& c) {
if (!node) return new CourseNode(c);
if (c.code < node->data.code) {
node->left = insertRec(node->left, c);
}
else if (c.code > node->data.code){
node->right = insertRec(node->right, c);
}
return node;
}
CourseNode* CourseBST::searchRec(CourseNode* node, const std::string& code) const {
if (!node || node->data.code == code) {
return node;
}
if (code < node->data.code){
return searchRec(node->left, code);
}
return searchRec(node->right, code);
}
void CourseBST::inorderRec(CourseNode* node) const {
if (!node) {
return;
}
inorderRec(node->left);
std::cout << node->data.code << " - " << node->data.name
<< " (" << node->data.day << " " << node->data.time << ")\n";
inorderRec(node->right);
}
void CourseBST::freeRec(CourseNode* node) {
if (!node) {
return;
}
freeRec(node->left);
freeRec(node->right);
delete node;
}
void CourseBST::insert(const Course& c) {
root = insertRec(root, c);
}
CourseNode* CourseBST::search(const std::string& code) const {
return searchRec(root, code);
}
void CourseBST::printInOrder() const {
inorderRec(root);
}