In: Computer Science
Please in C++ I don't have the binarySearchTree (Carrano) Implement the code of the BinarySeachTree Class to solve: (Gaddis) Programming Challenger 3. Car Class p. 802 Ch. 13 • Implement a menu of options • Add a motor vehicle to the tree • Remove a motor vehicle to the tree • Search by vehicle model • Vehicle Inventory Updates • Take one of the tours to verify the contents of the tree.
Car Class
Write a class named Car that has the following member
variables:
yearModel. An int that holds the car’s year model.
make. A string that holds the make of the car.
speed. An int that holds the car’s current speed.
In addition, the class should have the following constructor and other member functions.
Constructor. The constructor should accept the car’s year model and make as argu- ments. These values should be assigned to the object’s yearModel and make member variables. The constructor should also assign 0 to the speed member variables.
Accessor. Appropriate accessor functions to get the values stored in an object’s yearModel, make, and speed member variables.
accelerate. The accelerate function should add 5 to the speed member variable each time it is called.
brake. The brake function should subtract 5 from the speed member variable each time it is called.
Demonstrate the class in a program that creates a Car object, and then calls the accelerate function five times. After each call to the accelerate function, get the current speed of the car and display it. Then, call the brake function five times. After each call to the brake function, get the current speed of the car and display it.
#include<iostream>
#include <stdlib.h>
using namespace std;
class Car {
public:
int yearModel;
string make;
int speed;
//default constructor
Car()
{
this->yearModel = 0;
this->make = "";
this->speed = 0;
}
//constructor
Car(int yearModel, string make) {
this->yearModel = yearModel;
this->make = make;
this->speed = 0;
}
//accessor methods
int getYearModel() {
return this->yearModel;
}
string getMake() {
return this->make;
}
int getSpeed() {
return this->speed;
}
//accelerate method
void accelerate() {
this->speed = this->speed + 5;
}
//break method
void brake() {
this->speed = this->speed - 5;
}
};
//to create a node for bst:it contains car objects and left and right pointers
struct node {
Car car;
struct node* left, * right;
node(Car x)
{
car = x;
left = right = NULL;
}
};
// Create a node
struct node* newNode(Car car) {
//struct node* temp = (struct node*)malloc(sizeof(struct node));
struct node* temp = new node(car);
temp->car = car;
temp->left = temp->right = NULL;
return temp;
}
//to insert a car vehicle in bst based on its yearModel number
struct node* insert(struct node* node, Car car) {
// Return a new node if the tree is empty
if (node == NULL)
return newNode(car);
// Traverse to the right place and insert the node
if (car.yearModel < node->car.yearModel)
node->left = insert(node->left, car);
else
node->right = insert(node->right, car);
return node;
}
// Find the inorder successor
struct node* minValueNode(struct node* node) {
struct node* current = node;
// Find the leftmost leaf
while (current && current->left != NULL)
current = current->left;
return current;
}
//to remove the car vehicle from bst based on given yearModel
struct node* deleteNode(struct node* root, int yearModel) {
// Return if the tree is empty
if (root == NULL)
return root;
// Find the node to be deleted
if (yearModel < root->car.yearModel)
root->left = deleteNode(root->left, yearModel);
else if (yearModel > root->car.yearModel)
root->right = deleteNode(root->right, yearModel);
else {
// If the node is with only one child or no child
if (root->left == NULL) {
struct node* temp = root->right;
free(root);
return temp;
}
else if (root->right == NULL) {
struct node* temp = root->left;
free(root);
return temp;
}
// If the node has two children
struct node* temp = minValueNode(root->right);
// Place the inorder successor in position of the node to be deleted
root->car.yearModel = temp->car.yearModel;
// Delete the inorder successor
root->right = deleteNode(root->right, temp->car.yearModel);
}
return root;
}
// search a node based on given yearModel
bool search(struct node* root, int yearModel) {
// Return if the tree is empty
if (root == NULL)
return false;
if (root->car.yearModel == yearModel) {
return true;
}
// Find the node to be search
if (yearModel < root->car.yearModel)
search(root->left, yearModel);
else
search(root->right, yearModel);
}
//update the vehicle based on given yearModel
bool update(struct node* root, int yearModel, int updatedYearModel) {
// Return if the tree is empty
if (root == NULL)
return false;
if (root->car.yearModel == yearModel) {
root->car.yearModel = updatedYearModel;
return true;
}
// Find the node to be update
if (yearModel < root->car.yearModel)
update(root->left, yearModel, updatedYearModel);
else
update(root->right, yearModel, updatedYearModel);
}
// Inorder Traversal of bst
void printBST(struct node* root) {
if (root != NULL) {
//visiting
cout << root->car.yearModel << " -> " << root->car.make << " -> " << root->car.speed << endl;
// Traverse left
printBST(root->left);
// Traverse right
printBST(root->right);
}
}
//to add multiple vehicle in bst...
struct node *add(struct node* root){
//creating multiple car objects
Car car1(2004, "Honda");
Car car2(2001, "Honda");
Car car3(1998, "Honda");
Car car4(2005, "Honda");
Car car5(2000, "Honda");
Car car6(1997, "Honda");
Car car7(2007, "Honda");
Car car8(2008, "Honda");
//inserting or adding car objects into the bst
root = insert(root, car1);
root = insert(root, car2);
root = insert(root, car3);
root = insert(root, car4);
root = insert(root, car5);
root = insert(root, car6);
root = insert(root, car7);
root = insert(root, car8);
return root;
}
int main() {
Car car1(2020, "OD");
int i,choice=0,year,year1;
for (i = 0; i < 5; i++) {
car1.accelerate();
cout << "The current speed of car is :" << car1.getSpeed() << endl;
}
for (i = 0; i < 5; i++) {
car1.brake();
cout << "The current speed of car is :" << car1.getSpeed() << endl;
}
struct node* root = NULL;
do{
cout<<"Please choose: 1 for adding new vehicle. 2 for removing a vehicle. 3 for Searching a vehicle. 4 for Updating a vehicle. 5 for visiting or printing bst. 6 for quit"<<endl;
cout<<"Please enter your choice "<<endl;
cin>>choice;
switch(choice){
case 1:
root=add(root);
break;
case 2:
cout<<"Please enter the yearModel of car to remove "<<endl;
cin>>year;
cout << "removing vehicle " << endl;
root=deleteNode(root,year);
break;
case 3:
cout<<"Please enter the yearModel of car to search "<<endl;
cin>>year;
cout << "Searching the vehicle with yearModel "<<year << endl;
if(search(root,year))
cout << "Found" << endl;
else
cout << "Not found" << endl;
break;
case 4:
cout<<"Please enter the yearModel of car to update "<<endl;
cin>>year;
cout<<"Please enter the yearModel of car to update with "<<endl;
cin>>year1;
if(update(root,year,year1))
cout << "updated" << endl;
else
cout << "Not found" << endl;
break;
case 5:
cout << "visiting or traversal: " << endl;
printBST(root);
break;
case 6:
break;
default:
cout<<"Please enter the valid choice "<<endl;
}
}while(choice!=6);
}
Sample input and output of above c++ code:
The current speed of car is :5
The current speed of car is :10
The current speed of car is :15
The current speed of car is :20
The current speed of car is :25
The current speed of car is :20
The current speed of car is :15
The current speed of car is :10
The current speed of car is :5
The current speed of car is :0
Please choose: 1 for adding new vehicle. 2 for removing a vehicle.
3 for Searching a vehicle. 4 for Updating a vehicle. 5 for visiting
or printing bst. 6 for quit
Please enter your choice
1
Please choose: 1 for adding new vehicle. 2 for removing a vehicle.
3 for Searching a vehicle. 4 for Updating a vehicle. 5 for visiting
or printing bst. 6 for quit
Please enter your choice
5
visiting or traversal:
2004 -> Honda -> 0
2001 -> Honda -> 0
1998 -> Honda -> 0
1997 -> Honda -> 0
2000 -> Honda -> 0
2005 -> Honda -> 0
2007 -> Honda -> 0
2008 -> Honda -> 0
Please choose: 1 for adding new vehicle. 2 for removing a vehicle.
3 for Searching a vehicle. 4 for Updating a vehicle. 5 for visiting
or printing bst. 6 for quit
Please enter your choice
3
Please enter the yearModel of car to search
2007
Searching the vehicle with yearModel 2007
Found
Please choose: 1 for adding new vehicle. 2 for removing a vehicle.
3 for Searching a vehicle. 4 for Updating a vehicle. 5 for visiting
or printing bst. 6 for quit
Please enter your choice
3
Please enter the yearModel of car to search
2020
Searching the vehicle with yearModel 2020
Not found
Please choose: 1 for adding new vehicle. 2 for removing a vehicle.
3 for Searching a vehicle. 4 for Updating a vehicle. 5 for visiting
or printing bst. 6 for quit
Please enter your choice
4
Please enter the yearModel of car to update
2002
Please enter the yearModel of car to update with
2004
Not found
Please choose: 1 for adding new vehicle. 2 for removing a vehicle.
3 for Searching a vehicle. 4 for Updating a vehicle. 5 for visiting
or printing bst. 6 for quit
Please enter your choice
4
Please enter the yearModel of car to update
1998
Please enter the yearModel of car to update with
1999
updated
Please choose: 1 for adding new vehicle. 2 for removing a vehicle.
3 for Searching a vehicle. 4 for Updating a vehicle. 5 for visiting
or printing bst. 6 for quit
Please enter your choice
5
visiting or traversal:
2004 -> Honda -> 0
2001 -> Honda -> 0
1999 -> Honda -> 0
1997 -> Honda -> 0
2000 -> Honda -> 0
2005 -> Honda -> 0
2007 -> Honda -> 0
2008 -> Honda -> 0
Please choose: 1 for adding new vehicle. 2 for removing a vehicle.
3 for Searching a vehicle. 4 for Updating a vehicle. 5 for visiting
or printing bst. 6 for quit
Please enter your choice
2
Please enter the yearModel of car to remove
2000
removing vehicle
Please choose: 1 for adding new vehicle. 2 for removing a vehicle.
3 for Searching a vehicle. 4 for Updating a vehicle. 5 for visiting
or printing bst. 6 for quit
Please enter your choice
5
visiting or traversal:
2004 -> Honda -> 0
2001 -> Honda -> 0
1999 -> Honda -> 0
1997 -> Honda -> 0
2005 -> Honda -> 0
2007 -> Honda -> 0
2008 -> Honda -> 0
Please choose: 1 for adding new vehicle. 2 for removing a vehicle.
3 for Searching a vehicle. 4 for Updating a vehicle. 5 for visiting
or printing bst. 6 for quit
Please enter your choice
6
Code Screen shots:
Code explainations:
As per the requirments I have crated a class Car with required info.
Now I have created a structure to make binary search tree with car objects.
While adding or inserting a car object in bst i have used the car yearModel number.
While searching a vehicle in bst i also used the yearModel number.
While updating a vehicle in bst I used yearModel number of that vehicle and updated with given yearModel number..
While removing a vehicle in bst I used yearModel number of that vehicle and removed it from bst
for making a tour to visit each vehicle of bst i have implemented inorder traversal of bst.