Question: How can I write DvcScheduleSearch.cpp to solve the problem where in college catalogs, sometimes courses are listed that have not been offered in many years,

How can I write DvcScheduleSearch.cpp to solve the problem where in college catalogs, sometimes courses are listed that have not been offered in many years, and departments have no intention of offering them again. It's a disservice to students and to the community to continue to list them. For this (and several other reasons) the administration wants a program that will output the last semester in which a course was offered, and the first semester a course was offered.. In a loop, prompt the user to enter a course name (like COMSC-210) including a subject code (like COMSC), a dash, and an alphanumeric sequence number. If a user enters uppercase or lowercase X, quit the loop and the program -- be sure to explain this in the prompt. For any other input, look up the course and output the first time and the last time that course was offered, nicely labeled (like "COMSC-210 was first offered in Fall 2001, and last offered in Fall 2018"). Or if the course is not found, output something like this: "CS-100 could not be found as far back as the year 2000" because that's as far back as the database goes. The file is in this format: term section course instructor whenWhere
Spring 20241462 ADJUS-120 McLeran NOT AVAILABLE
Spring 20245373 ADJUS-120 Weaver NOT AVAILABLE
I have created a header file that I would like to use, but not sure how to take the next step:
#ifndef BINARY_SEARCH_TREE_H
#define BINARY_SEARCH_TREE_H
#include
#include
template class BinarySearchTree {
private:
struct Node {
K key;
V value;
Node *left;
Node *right;
Node(const K &key, const V &value)
: key(key), value(value), left(nullptr), right(nullptr){}
};
Node *rootNode;
int siz;
void Keys(const Node *node, std::queue &q) const {
if (node != nullptr){
Keys(node->left, q);
q.push(node->key);
Keys(node->right, q);
}
}
void clear(Node *node){
if (node != nullptr){
clear(node->left);
clear(node->right);
delete node;
}
}
Node *copyTree(Node *const node){
if (node == nullptr){
return nullptr;
}
Node *newNode = new Node(node->key, node->value);
newNode->left = copyTree(node->left);
newNode->right = copyTree(node->right);
return newNode;
}
Node **findNode(const K &key, Node **node){
while (*node != nullptr && key !=(*node)->key){
if (key <(*node)->key){
node = &((*node)->left);
} else {
node = &((*node)->right);
}
}
return node;
}
public:
BinarySearchTree() : rootNode(nullptr), siz(0){}
BinarySearchTree(const BinarySearchTree &other)
: rootNode(nullptr), siz(0){
rootNode = copyTree(other.rootNode);
siz = other.siz;
}
BinarySearchTree &operator=(const BinarySearchTree &other){
if (this != &other){
clear(rootNode);
rootNode = copyTree(other.rootNode);
siz = other.siz;
}
return *this;
}
~BinarySearchTree(){ clear(rootNode); }
int size() const { return siz; }
V &operator[](const K &key){
Node **node = findNode(key, &rootNode);
if (*node == nullptr){
*node = new Node(
key, V()); // Insert with default-constructed value if not found
siz++;
}
return (*node)->value;
}
bool containsKey(const K &key) const {
Node *node = rootNode;
while (node != nullptr && key != node->key){
if (key < node->key){
node = node->left;
} else {
node = node->right;
}
}
return node != nullptr;
}
void deleteKey(const K &key){
Node **node = findNode(key, &rootNode);
if (*node == nullptr){
throw std::out_of_range("Key not found");
}
Node *oldNode =*node;
if ((*node)->left == nullptr){
*node =(*node)->right;
} else if ((*node)->right == nullptr){
*node =(*node)->left;
} else {
Node **pred = &(*node)->left;
while ((*pred)->right != nullptr){
pred = &(*pred)->right;
}
(*node)->key =(*pred)->key;
(*node)->value =(*pred)->value;
oldNode =*pred;
*pred =(*pred)->left;
}
delete oldNode;
siz--;
}
std::queue Keys() const {
std::queue q;
Keys(rootNode, q);
return q;
}
void clear(){
clear(rootNode);
rootNode = nullptr;
siz =0;
}
};
#endif // BINARY_SEARCH_TREE_H

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!