[Solved]Modify C Program Turn Binary Tree Data Retrieval Program Using Concept Associative Contain Q37141944
Modify this C++ program. Turn our binary tree into a dataretrieval program using the concept of an associativecontainer.
Step 1: First, define a new class for storingdata about some kind of entity (people, books, music albums, etc.).Make sure that one of the fields in the class is suitable foruniquely identifying an individual object of the class. For people,you could use the last name, social security number, student ID, orsomething like that. For books, you could use the ISBN, subject,course number (for textbooks), author. etc.
Step 2: Modify the TreeNode class so that ithas four fields: a string for the key, a pointer to an object asdefined above, and pointers to the left and right subtrees.
Step 3: Modify the BinaryTree class so that itproperly uses the new TreeNode.
Step 4: Write a search function that acceptsthe value of a key as a parameter, and then returns the objectassociated with that key. The function should return null if thekey was not found.
Step 5: Write a main function that creates atree, fills it with data, and then allows the user to input a keyand then search the tree for that key. Demonstrate that itcorrectly finds the data requested, and returns null for data thatis not present.
Show your code along with screen shots showing everythingworking.
–TreeNode.h–
#pragma once
#include <string>
#include “Student.h”
class TreeNode
{
public:
std::string key;
Student *student;
TreeNode* left;
TreeNode* right;
TreeNode(Student *s)
{
key = s->id;
student = s;
left = right = 0;
}
};
–Students.h–
#pragma once
#include <iostream>
#include <string>
class Student
{
public:
std::string id;
std::string firstName;
std::string lastName;
Student(std::string i, std::string f, std::stringl)
{
id = i;
firstName = f;
lastName = l;
}
void display()
{
std::cout << firstName<< ” ” << lastName << ” (” << id <<“)”;
std::cout << std::endl;
}
};
–BinaryTree.h–
#pragma once
#include “TreeNode.h”
class BinaryTree
{
private:
TreeNode* root = 0;
TreeNode* add(Student *s, TreeNode* p);
bool contains(std::string key, TreeNode* p);
void displayPostOrder(TreeNode* p);
void displayPreOrder(TreeNode* p);
void displayInOrder(TreeNode* p);
public:
BinaryTree() { root = 0; }
void add(Student *s);
void displayPreOrder();
void displayInOrder();
void displayPostOrder();
bool contains(std::string key);
bool isEmpty() { return root == 0; }
Student *search(std::string key);
};
–BinaryTree.cpp–
#include “BinaryTree.h”
#include <iostream>
void BinaryTree::add(Student *s)
{
root = add(s, root);
}
TreeNode* BinaryTree::add(Student *s, TreeNode* p)
{
if (p == 0)
{
p = new TreeNode(s);
}
else
{
if (s->id < p->key)
p->left =add(s, p->left);
else
p->right =add(s, p->right);
}
return p;
}
bool BinaryTree::contains(std::string key)
{
return contains(key, root);
}
bool BinaryTree::contains(std::string key, TreeNode* p)
{
if (p == 0)
return false;
else if (p->key == key)
return true;
else if (key < p->key)
return contains(key,p->left);
else
return contains(key,p->right);
}
void BinaryTree::displayPreOrder()
{
displayPreOrder(root);
}
void BinaryTree::displayInOrder()
{
displayInOrder(root);
}
void BinaryTree::displayPostOrder()
{
displayPostOrder(root);
}
void BinaryTree::displayPostOrder(TreeNode *p)
{
if (p != 0)
{
displayPostOrder(p->left);
displayPostOrder(p->right);
p->student->display();
}
}
void BinaryTree::displayPreOrder(TreeNode *p)
{
if (p != 0)
{
p->student->display();
displayPreOrder(p->left);
displayPreOrder(p->right);
}
}
void BinaryTree::displayInOrder(TreeNode *p)
{
if (p != 0)
{
displayInOrder(p->left);
p->student->display();
displayInOrder(p->right);
}
}
Student* BinaryTree::search(std::string key)
{
return 0;
}
–Main.cpp–
#include <iostream>
#include “BinaryTree.h”
using namespace std;
void main()
{
BinaryTree tree;
int choice = 0;
const int QUIT = 6;
string id, fName, lName;
Student *s = 0;
tree.add(new Student(“jonesd342”, “Dan”,”Jones”));
tree.add(new Student(“rocushp”, “Peter”,”Rocush”));
tree.add(new Student(“mendezd175”, “Dora”,”Mendez”));
tree.add(new Student(“habbenr”, “Ryan”,”Habben”));
tree.add(new Student(“aliy183”, “Jason”, “Ali”));
do
{
cout << endl << “MENU:”<< endl;
cout << “1. Is tree empty?”<< endl;
cout << “2. Add student.”<< endl;
cout << “3. Is studentpresent?” << endl;
cout << “4. Retrieve studentdata.” << endl;
cout << “5. Display allstudents.” << endl;
cout << “6. End program.”<< endl << endl;
cin >> choice;
switch (choice)
{
case 1:
if(tree.isEmpty())
cout << “Tree is empty.” <<endl;
else
cout << “Tree contains data.” <<endl;
break;
case 2:
cout <<“Student ID: “;
cin >>id;
cout <<“First Name: “;
cin >>fName;
cout <<“Last Name: “;
cin >>lName;
tree.add(newStudent(id, fName, lName));
break;
case 3:
cout <<“Enter ID of student: “;
cin >>id;
if(tree.contains(id))
cout << “Yes, student ” << id<< ” is present.” << endl;
else
cout << “No, student ” << id<< ” is absent.” << endl;
break;
case 4:
cout <<“Enter ID of student: “;
cin >>id;
s =tree.search(id);
if (s ==0)
cout << “Student ” << id << “was not found.” << endl;
else
s->display();
break;
case 5:
cout <<endl << “Preorder Display:” << endl;
tree.displayPreOrder();
cout <<endl << “Inorder Display:” << endl;
tree.displayInOrder();
cout <<endl << “Postorder Display:” << endl;
tree.displayPostOrder();
break;
case 6:
cout <<“Ending program…” << endl;
break;
default:
cout <<“ERROR: Invalid option. Try again.” << endl;
}
} while (choice != QUIT);
system(“pause”);
}
Expert Answer
Answer to Modify this C++ program. Turn our binary tree into a data retrieval program using the concept of an associative containe… . . .
OR

