Question

Hi there, I am working on a binary search tree code in c++. The program must...

Hi there, I am working on a binary search tree code in c++. The program must store and update students' academic records, each node includes the student name, credits attempted, credits earned and GPA. I have made some progress with the code and written most of the functions in the .cpp file (already did the .h file) but i am struggling with what to put in the main and how to put an update part in the insert function. I know I need a switch statement to substitute the letter grades for the quality points but i am not sure if that should go in main or in the insert function in the .cpp file .. this is what I have so far .. thanks!!

// Implementation file for the StudentRecordsTree class
#include <iostream>
#include "StudentRecordsTree.h"
using namespace std;

//*************************************************************
// insert accepts a TreeNode pointer and a pointer to a node. *
// The function inserts the node into the tree pointed to by *
// the TreeNode pointer. This function is called recursively. *
//*************************************************************

void StudentRecordsTree::insert(TreeNode *&nodePtr, TreeNode *&newNode)
{
  
if (nodePtr == nullptr)
nodePtr = newNode; // Insert the node.
else if (newNode->Name < nodePtr->Name) //if name comes before orig name
insert(nodePtr->left, newNode); // Search the left branch
else //if name comes after orig name
insert(nodePtr->right, newNode); // Search the right branch
}

//**********************************************************
// insertNode creates a new node to hold num as its value, *
// and passes it to the insert function. *
//**********************************************************

void StudentRecordsTree::insertNode(string name, int credits, char grade)
{
  
TreeNode *newNode = nullptr;   // Pointer to a new node.
//bool isFound = true;
//if(!isFound){
// Create a new node and store name in it.
newNode = new TreeNode;
//newNode->Name = name;
//newNode->creditsAtt = creditsAtt;

newNode->left = newNode->right = nullptr;
// Insert the node. & call private insert function above
insert(root, newNode);

switch (grade){
case 'A':
cout << 4 << endl;
case 'B':
cout << 3 << endl;
case 'C':
cout << 2 << endl;
case 'D':
cout << 1 << endl;
case 'F':
cout << 0 << endl;
}
  
}

//***************************************************
// destroySubTree is called by the destructor. It *
// deletes all nodes in the tree. *
//***************************************************

void StudentRecordsTree::destroySubTree(TreeNode *nodePtr)
{
if (nodePtr)
{
if (nodePtr->left)
destroySubTree(nodePtr->left);
if (nodePtr->right)
destroySubTree(nodePtr->right);
delete nodePtr;
}
}
//****************************************************************
// The displayInOrder member function displays the values *
// in the subtree pointed to by nodePtr, via inorder traversal. *
//****************************************************************

void StudentRecordsTree::displayInOrder(TreeNode *nodePtr) const
{
if (nodePtr)
{
displayInOrder(nodePtr->left);
cout << nodePtr->Name << endl;
displayInOrder(nodePtr->right);
}
}

//****************************************************************
// The displayPreOrder member function displays the values *
// in the subtree pointed to by nodePtr, via preorder traversal. *
//****************************************************************

void StudentRecordsTree::displayPreOrder(TreeNode *nodePtr) const
{
if (nodePtr)
{
cout << nodePtr->Name << endl;
displayPreOrder(nodePtr->left);   
displayPreOrder(nodePtr->right);
}
}

//****************************************************************
// The displayPostOrder member function displays the values *
// in the subtree pointed to by nodePtr, via postorder traversal.*
//****************************************************************

void StudentRecordsTree::displayPostOrder(TreeNode *nodePtr) const
{
if (nodePtr)
{
displayPostOrder(nodePtr->left);
displayPostOrder(nodePtr->right);
cout << nodePtr->Name << endl;
}
}
/*int PrintMax(Node *root)
{
if (!root)
return 0; // Only if the tree contains nothing at all

//right most node has max value so search till u hit right most node
if (root->right)
return PrintMax(root->right);
cout << "max value = " << root->data;
}

#include <cstdlib>
#include <iostream>
#include <fstream>
#include"StudentRecordsTree.h"
using namespace std;

int main(int argc, char** argv) {

StudentRecordsTree tree; //instance of StudentRecordsTree class
string k;
int credits;
char grade;
  
//open output file
ofstream outFile;
outFile.open("output.txt");
  
ifstream inFile;
inFile.open("Student.txt");
string temp;
  
//insert student records one by one,
//if it already exists, update

  
  
  
for(int i=0; i < 50; i++){
//if(getline(inFile,k))
while(inFile >> k >> credits >> grade)
{

tree.insertNode(k,credits,grade);
  
}
}
  
//while(inFile >> k >> credits >> grade)
//{
// cout << k << " " << credits << " " << grade << endl;
//}
tree.displayInOrder();
  
  
  
return 0;
  
}

#ifndef STUDENTRECORDS_H
#define   STUDENTRECORDS_H
#include<cstdlib>
#include <string>

class StudentRecordsTree
{
private:
struct TreeNode
{
std::string Name; // key value
int creditsAtt;
int creditsEarn;
double gpa;
TreeNode *left; // Pointer to left child node
TreeNode *right; // Pointer to right child node
};

TreeNode *root; // Pointer to the root node
TreeNode element;

// Private member functions
void insert(TreeNode *&, TreeNode *&);
void destroySubTree(TreeNode *);
void deleteNode (std::string, TreeNode *&);
void displayInOrder(TreeNode *) const;
void displayPreOrder(TreeNode *) const;
void displayPostOrder(TreeNode *) const;

public:
// Constructor
StudentRecordsTree()
{ root = nullptr; }
  
// Destructor
~StudentRecordsTree()
{ destroySubTree(root); }
  
// Binary tree operations
void insertNode(std::string, int, char);
void displayInOrder() const
{ displayInOrder(root); }
void displayPreOrder() const
{ displayPreOrder(root); }
void displayPostOrder() const
{ displayPostOrder(root); }

};
#endif   /* STUDENTRECORDS_H */

Jones,Sue,        3B
Brown,Charlie        3D
Smith,John       3C
Smith,John       4C
Bird,Sam       2C
Bird,Sam       2C
Smith,John       5C
Jones,Sue       3A
Moose,Manny       3A
Bear,Ben       4C
Bear,Ben       5F
Bear,Ben       2C
Brown,Charlie        3C  

^that is the input file

0 0
Add a comment Improve this question Transcribed image text
Answer #1

升.de. Sine g7m g1 REC Nade.* r2 find No .de 벙 VaLe Cv.): es,nRL

Add a comment
Know the answer?
Add Answer to:
Hi there, I am working on a binary search tree code in c++. The program must...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a function, swapSubtrees, that swaps all of the left and right subtrees of a binary...

    Write a function, swapSubtrees, that swaps all of the left and right subtrees of a binary tree. Add this function to the class binaryTreeType and create a program to test this function. #include <iostream> using namespace std; //Definition of the Node template <class elemType> struct nodeType { elemType info; nodeType<elemType> *lLink; nodeType<elemType> *rLink; }; //Definition of the class template <class elemType> class binaryTreeType { public: //Overload the assignment operator. const binaryTreeType<elemType>& operator=(const binaryTreeType<elemType>&) { if (this != &otherTree) //avoid self-copy...

  • In C++ I need the printRange function, and the main.cpp program. Thanks. (Binary search tree) Write...

    In C++ I need the printRange function, and the main.cpp program. Thanks. (Binary search tree) Write a function printRange that takes as input a binary search tree t and two keys, k1 and k2, which are ordered so that k1 < k2, and print all elements x in the tree such that k1 <= x <= k2. You can add this function in BinarySearchTree.h (click the link) that we used in the lecture and lab 7. public: void printRange(int k1,...

  • Need this in C++ Goals: Your task is to implement a binary search tree of linked...

    Need this in C++ Goals: Your task is to implement a binary search tree of linked lists of movies. Tree nodes will contain a letter of the alphabet and a linked list. The linked list will be an alphabetically sorted list of movies which start with that letter. MovieTree() ➔ Constructor: Initialize any member variables of the class to default ~MovieTree() ➔ Destructor: Free all memory that was allocated void printMovieInventory() ➔ Print every movie in the data structure in...

  • Consider the class specifications for the Binary Tree class and Binary Search Tree class in the...

    Consider the class specifications for the Binary Tree class and Binary Search Tree class in the attached files // BinaryTree.h #include <iostream> using namespace std; //Definition of the Node template <class elemType> struct TreeNode { elemType data; TreeNode<elemType> *left; TreeNode<elemType> *right; }; //Definition of class Binary Tree template <class elemType> class BinaryTree { protected: TreeNode<elemType> *root; public: BinaryTree(); BinaryTreel const BinaryTree<elemType>& otherTree); BinaryTree(); bool is Empty() const; virtual boot search(const elemType& searchItem) const = 0; virtual void insert(const elemType& insertItem)...

  • Having code issues wth my C++ program. My program checks if two binary trees are similar...

    Having code issues wth my C++ program. My program checks if two binary trees are similar and if they're not they return false. My program is return true with different binary trees. Could use some help thanks #include <iostream> #include <string> using namespace std; //Struct of Nodes struct BinarySearchTree { int data; BinarySearchTree *left; BinarySearchTree *right; }; // Inserting nodes into BST BinarySearchTree* insert( BinarySearchTree* node, int val) { if (node == NULL) { BinarySearchTree *newNode = new BinarySearchTree(); newNode->data...

  • C++ (Using Binary Search Trees) other methods will result in downvote Implement the binary search tree...

    C++ (Using Binary Search Trees) other methods will result in downvote Implement the binary search tree methods (bst.cpp) for the binary search tree provided in the header file. Test your implementation with the included test. bst.h bst_test.cpp Note: Your implementation must correspond to declarations in the header file, and pass the test. Do not modify these two. I will compile your code against these. If the compilation fails, you will get down vote. bst.h #ifndef BINARY_SEARCH_TREE_H #define BINARY_SEARCH_TREE_H #include <string>...

  • In C++, for the provided template linked list class create a derived class of it which...

    In C++, for the provided template linked list class create a derived class of it which adds the functionality to it to find the high and low value of any given data stored in the list. The derived class must be a template. LinkedList.h #pragma once #include <iostream> using namespace std; template <class T> class ListNode {    public:        T data;        ListNode<T>* next;        ListNode(T data)        {            this->data = data;...

  • C++ Binary Search Tree question. I heed help with the level 2 question please, as level...

    C++ Binary Search Tree question. I heed help with the level 2 question please, as level 1 is already completed. I will rate the answer a 100% thumbs up. I really appreciate the help!. Thank you! searching.cpp #include <getopt.h> #include <iostream> #include <sstream> #include <stdlib.h> #include <unistd.h> using namespace std; // global variable for tree operations // use to control tree maintenance operations enum Mode { simple, randomised, avl } mode; // tree type // returns size of tree //...

  • C++ Binary Search Tree question. I heed help with the level 2 question please, as level...

    C++ Binary Search Tree question. I heed help with the level 2 question please, as level 1 is already completed. I will rate the answer a 100% thumbs up. I really appreciate the help!. Thank you! searching.cpp #include <getopt.h> #include <iostream> #include <sstream> #include <stdlib.h> #include <unistd.h> using namespace std; // global variable for tree operations // use to control tree maintenance operations enum Mode { simple, randomised, avl } mode; // tree type // returns size of tree //...

  • Consider the class specifications for the Binary Tree class and BinarySearch Tree class below: // Binary...

    Consider the class specifications for the Binary Tree class and BinarySearch Tree class below: // Binary Tree.h #include <iostream> using namespace std; //Definition of the Node template <class elemType struct TreeNode { elemType data; TreeNode<elemType> *left; TreeNode<elemType *right; }; //Definition of class Binary Tree template <class elemType> class Binary Tree { protected: TreeNode<elemType> *root; public: BinaryTree(); BinaryTreel const BinaryTree<elemType>& otherTree); -Binary Tree(): bool is Empty() const; virtual bool search(const elemTypes searchItem) const = 0; virtual void insert(const elemTypek insertItem) =...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT