CPP Self Balancing Tree DataStructures
#pragma once
#include<iostream>
#include"TNode.h"
#include"Courses.h"
#include<queue>
#include <cmath>
#include<vector>
#include<list>
#include<fstream>
#include<string>
#ifndef BST_H
#define BST_H
using namespace std;
class BST
{
private:
TNode* root;
public:
//constructor
BST();
//is empty
bool isempty();
//SWAP
//void swap(TNode* x, TNode* y);
//MAXIMUM
TNode* getMaximum(TNode* temp);
void Maximum();
//search for modification
void search_Modify(int d);
//SEARCHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
TNode* search(int key, TNode* p, TNode* c);
//TNode* searchbyName(int key, TNode* p, TNode* c);
//ADD student complete
void addNewStudent(string n, int r);
void addNewStudentFile(string n, int r);
//search on key
TNode* searchOnKey(int key);
//REMOVE
void remove(int e);
//visit
void visit(TNode* t);
void inorder(TNode* t);
void preorder(TNode* t);
void postorder(TNode* t);
//destructor related
void delet(TNode* t);
~BST();
//PRINT FUNCTIONS
void printin();
void printpre();
void printpost();
void printTree();
//BISMILLAH
//MODIFY
//----------------------------------------------------------------------------------------
//check style
bool checkStyle(TNode* x);
//right Rotate
TNode* rightRotate(TNode* x);
//left rotate
TNode* leftRotate(TNode* x);
//-----------------------------------------------------------------------------------------
void printBinaryTree(TNode* root);
void printSpace(double n, TNode* removed);
int heightOfTree(TNode* root);
//-----------------------------------------------------------------------------------------
//CASE 1 KA FUNCTION
TNode* funcCase1(TNode* c);
//CASE 2 KA FUNCTION
TNode* funcCase2(TNode* x);
//CASE 3 KA FUNCTION
TNode* funcCase3V(TNode* c);
void asliModify(TNode* x);
// LEVEL 2 FUNCTIONS
void displayStudentRecord();
//void displayByName(string n);
void searchingByName_terevasal(TNode* t, string n);
void searchByRollNO(int r);
/////////////////////////////
void updateStudentRecord();
void editStudentName();
void editStudentGrade();
void addNewCourse();
///////////////////////
void deleteRecordsMenu();
void deleteStudentByRollNO(int r);
void deleteStudentByName(string n);
void delByNameTrivarsal(TNode* t, string n);
void deleteCourseof_W_Grade(int r);
////////////////////////
void listOfStudentsInCourse();
void calc_Listof_EnrolledStudents(TNode *t ,string c);
///////////
void calculateGpa();
void studentHighestGPA();
TNode* highestGpaTraversal(TNode* t , float &g);
///////////////////
void deansHonorList();
void deanListTraversal(TNode* t);
//////////////
void writingtofile();
void fileWritingTraversal(TNode* t);
void visitforWriting(TNode *t);
void readingfromFile();
};
//constructor
BST::BST()
{
root = NULL;
}
//is empty
bool BST::isempty()
{
if (root == NULL)
{
cout << " NON EMPTY !!" << endl;
return true;
}
else
cout << " Tree is EMPTY !!" << endl;
return false;
}
//MAXIMUM
TNode* BST::getMaximum(TNode* temp)
{
TNode* previ = temp;
while (temp != NULL)
{
previ = temp;
temp = temp->getright();
}
//NOTE '_'
// temp null py chala jayga to us waja
//sy prev max py hoga to uslo return kr rha hu
return previ;
}
void BST::Maximum()
{
TNode* temp = root;
TNode* previ = root;
while (temp != NULL)
{
previ = temp;
temp = temp->getright();
}
cout << " maximum node : " << previ->getrollNO() << endl;
}
//search for modification
void BST::search_Modify(int d)
{
TNode* current = root;
TNode* previous = current;
while (current != NULL)
{
if (current->getrollNO() == d)
{
break;
}
else if (current->getrollNO() > d)
{
previous = current;
current = current->getleft();
}
else
{
previous = current;
current = current->getright();
}
}
if (current != NULL)
{
cout << "STUDENT NODE Found " << endl;
visit(current);
asliModify(current);
cout << " MODIFIED TREE AFTER SEARCH !!!! " << endl;
printBinaryTree(root);
}
else
{
asliModify(previous);
}
}
//SEARCHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
TNode* BST::search(int key, TNode* p, TNode* c)
{
TNode* current = root;
//TNode* prev = current;
while (current != NULL)
{
if (current->getrollNO() == key)
{
break;
}
else if (current->getrollNO() > key && current->getleft() != NULL)
{
//prev = current;
current = current->getleft();
}
else if (current->getrollNO() > key && current->getleft() == NULL)
{
current = NULL;
return current;
}
else if (current->getrollNO() < key && current->getright() != NULL)
{
//prev = current;
current = current->getright();
}
else if (current->getrollNO() < key && current->getright() == NULL)
{
current = NULL;
return current;
}
}
if (current != NULL)
{
cout << " Student Found " << endl;
c = current;
if (current == root)
{
p = NULL;
}
else
{
p = current->getparent();
}
return p, c;
}
else
{
cout << " NOT Found " << endl;
c = current;
}
}
//ADD
void BST::addNewStudent(string n , int r )
{
TNode* temp = new TNode(n , r);
if (root == NULL)
{
root = temp;
}
else
{
TNode* current = root;
TNode* previous = current;
while (current != NULL)
{
previous = current;
if (r < current->getrollNO())
{
current = current->getleft();
}
else
{
current = current->getright();
}
}
if (previous->getrollNO() > r)
{
previous->setleft(temp);
temp->setparent(previous);
cout << " after adding value in tree before modification " << endl << endl;
//printBinaryTree(root);
printin();
asliModify(temp);
}
else
{
previous->setright(temp);
temp->setparent(previous);
asliModify(temp);
}
}
}
//add by file
void BST::addNewStudentFile(string n , int r )
{
TNode* temp = new TNode(n , r , 1);
if (root == NULL)
{
root = temp;
}
else
{
TNode* current = root;
TNode* previous = current;
while (current != NULL)
{
previous = current;
if (r < current->getrollNO())
{
current = current->getleft();
}
else
{
current = current->getright();
}
}
if (previous->getrollNO() > r)
{
previous->setleft(temp);
temp->setparent(previous);
cout << " after adding value in tree before modification " << endl << endl;
//printBinaryTree(root);
printin();
asliModify(temp);
}
else
{
previous->setright(temp);
temp->setparent(previous);
asliModify(temp);
}
}
}
//search on key
TNode* BST::searchOnKey(int key)
{
TNode* current;
current = root;
while (current != NULL)
{
if (current->getrollNO() == key)
break;
else if (current->getrollNO() > key)
current = current->getleft();
else
current = current->getright();
}
if (current != NULL)
{
cout << "\nNODE FOUND " << current->getrollNO() << endl;
asliModify(current);
cout << " MOdified tree after searching a node " << endl << endl;
printBinaryTree(root);
return current;
}
else
{
cout << " STUDENT NOT FOUND " << endl;
}
}
//REMOVE
void BST::remove(int e)
{
TNode* current, * previous;
current = root;
previous = current;
current = search(e, previous, current);
if (current != NULL)
{
previous = current->getparent();
}
if (current == NULL)
{
cout << " Student NOT FOUND !!" << endl;
return;
}
if (current == root && current->getleft() == NULL && current->getright() == NULL)
{
delete current;
root = NULL;
cout << " Student Removed Successfully !!" << endl;
return;
}
if (current == root )
{
current = current->getleft();
TNode* m = getMaximum(current);
current = m;
m->swap(root, m);
previous = m->getparent();
}
else
{
if (current->getrollNO() == e)
{
if (current != NULL)
{
//current is a leaf node
if (current->isLeafNode() == true)
{
if (current->isLeftChild() == true)
{
previous->setleft(NULL);
}
if (current->isRightChild() == true)
{
previous->setright(NULL);
}
//modify
asliModify(current->getparent());
cout << " Student Removed Successfully !!" << endl;
delete current;
}
//2 childs wale node
else if (current->isInternalwithTwoChildren())
{
TNode* m = current->getright();
while (m->getleft() != NULL)
{
m = m->getleft();
}
// Replaing data
current->swap(current, m);
// Check if the m node is a leaf or has a right child
if (m->isLeafNode())
{
if (m->isLeftChild())
{
m->getparent()->setleft(NULL);
}
else
{
m->getparent()->setright(NULL);
}
//modifying after deletion
asliModify(m->getparent());
cout << " Student Removed Successfully !!" << endl;
delete m;
}
else
{
// m node has a right child
if (m->isLeftChild())
{
m->getparent()->setleft(m->getright());
}
else
{
m->getparent()->setright(m->getright());
}
m->getright()->setparent(m->getparent());
//modifying after deletion
asliModify(m->getparent());
delete m;
cout << " Student Removed Successfully !!" << endl;
}
}
//current has one left child
else if (current->isInternalwithOneLeftChild())
{
if (current->isLeftChild())
{
previous->setleft(current->getleft());
}
if (current->isRightChild())//current is right child of its parent
{
previous->setright(current->getleft());
}
current->getleft()->setparent(previous);
//modify
asliModify(current->getparent());
delete current;
cout << " Student Removed Successfully !!" << endl;
}
//current has one right child
else if (current->isInternalwithOneRightChild())
{
if (current->isLeftChild())
{
previous->setleft(current->getright());
}
if (current->isRightChild())
{
previous->setright(current->getright());
}
current->getright()->setparent(previous);
//modify
asliModify(current->getparent());
cout << " Student Removed Successfully !!" << endl;
delete current;
}
}
}
else
{
asliModify(current);
}
}//else
}
BST:: ~BST()
{
TNode* current = root;
delet(current);
root = NULL;
}
//visit
void BST::visit(TNode* t)
{
cout << " ----------------STUDENT INFO---------------- " << endl;
cout << " Student Name = " << t->getsName() << " ." << endl;
cout << " Student RollNO. = " << t->getrollNO() << " ." << endl;
cout << " --------------ENROLLED COURSES-------------- " << endl;
t->printCourse();
}
void BST::searchingByName_terevasal(TNode*t ,string n)
{
if (t) {
searchingByName_terevasal(t->getleft() , n );
if (t->getsName() == n)
{
visit(t);
}
searchingByName_terevasal(t->getright(), n);
}
}
void BST::inorder(TNode* t)
{
if (t) {
inorder(t->getleft());
visit(t);
inorder(t->getright());
}
}
void BST::preorder(TNode* t)
{
if (t) {
visit(t);
preorder(t->getleft());
preorder(t->getright());
}
}
void BST::postorder(TNode* t)
{
if (t) {
postorder(t->getleft());
postorder(t->getright());
visit(t);
}
}
//destructor related
void BST::delet(TNode* t)
{
if (t == NULL) {
return;
}
delet(t->getleft());
delet(t->getright());
delete t;
}
//PRINT FUNCTIONS
void BST::printin()
{
inorder(root);
}
void BST::printpre()
{
preorder(root);
//printBinaryTree(root);
}
void BST::printpost()
{
postorder(root);
}
void BST::printTree()
{
printBinaryTree(root);
}
//BISMILLAH
//MODIFY
//----------------------------------------------------------------------------------------
//check style
bool BST::checkStyle(TNode* x)
{
TNode* p = x->getparent();
TNode* g = x->getparent()->getparent();
if (x->getparent()->getparent() != NULL)
{
if (x->isLeftChild() == true && p->isLeftChild() == true || x->isRightChild() == true && p->isRightChild() == true)
{
return true;
}
}
else return false;
}
//right Rotate
TNode* BST::rightRotate(TNode* x) {
bool check = checkStyle(x);
TNode* t = x->getright();
TNode* p = x->getparent();
TNode* g = x->getparent()->getparent();
x->setright(p);
p->setleft(t);
if (p->getparent() != NULL)
{
x->setparent(p->getparent());
if (check == true)
{
p->getparent()->setleft(x);
}
else
{
p->getparent()->setright(x);
}
}
else
{
x->setparent(NULL);
root = x;
}
p->setparent(x);
if (t != NULL)
{
t->setparent(p);
}
return x;
}
//left rotate
TNode* BST::leftRotate(TNode* x) {
bool check = checkStyle(x);
TNode* t = x->getleft();
TNode* p = x->getparent();
TNode* g = x->getparent()->getparent();
x->setleft(p);
p->setright(t);
if (p->getparent() != NULL)
{
x->setparent(p->getparent());
if (check == true)
{
p->getparent()->setright(x);
}
else
{
p->getparent()->setleft(x);
}
}
else
{
x->setparent(NULL);
root = x;
}
p->setparent(x);
if (t != NULL)
{
t->setparent(p);
}
return x;
}
//-----------------------------------------------------------------------------------------
void BST::printBinaryTree(TNode* root)
{
queue<TNode*> treeLevel, temp;
treeLevel.push(root);
int counter = 0;
int height = heightOfTree(root) - 1;
double numberOfElements = pow(2, (height + 1)) - 1;
while (counter <= height) {
TNode* removed = treeLevel.front();
treeLevel.pop();
if (temp.empty()) {
printSpace(numberOfElements
/ pow(2, counter + 1),
removed);
}
else {
printSpace(numberOfElements / pow(2, counter),
removed);
}
if (removed == nullptr) {
temp.push(nullptr);
temp.push(nullptr);
}
else {
temp.push(removed->getleft());
temp.push(removed->getright());
}
if (treeLevel.empty()) {
cout << endl << endl;
treeLevel = temp;
while (!temp.empty()) {
temp.pop();
}
counter++;
}
}
}
void BST::printSpace(double n, TNode* removed)
{
for (; n > 0; n--) {
cout << " ";
}
if (removed == nullptr) {
cout << ".";
}
else {
cout << removed->getrollNO();
cout << removed->getsName();
}
}
int BST::heightOfTree(TNode* root)
{
if (root == nullptr) {
return 0;
}
return 1
+ max(heightOfTree(root->getleft()),
heightOfTree(root->getright()));
}
//-----------------------------------------------------------------------------------------
//CASE 1 KA FUNCTION
TNode* BST::funcCase1(TNode* c)
{
if (c->getparent() != NULL && c->getparent()->getparent() == NULL)
{
if (c->isLeftChild() == true)
{
c = rightRotate(c);
return c;
}
else if (c->isRightChild() == true)
{
c = leftRotate(c);
return c;
}
}
}
//CASE 2 KA FUNCTION
TNode* BST::funcCase2(TNode* x)
{
TNode* p = x->getparent();
TNode* g = p->getparent();
if (x->isLeftChild() == true && p->isLeftChild() == true)
{
//1st rotation from parent
TNode* p1 = x->getparent();
bool check = checkStyle(p1);
TNode* t = p1->getright();
TNode* p = p1->getparent();
TNode* g = p1->getparent()->getparent();
p1->setright(p);
p->setleft(t);
if (p->getparent() != NULL)
{
p1->setparent(p->getparent());
if (check == true)
{
p->getparent()->setleft(p1);
}
else
{
p->getparent()->setright(p1);
}
}
else
{
p1->setparent(NULL);
root = p1;
}
p->setparent(p1);
if (t != NULL)
{
t->setparent(p);
}
//2nd time rotation code
check = checkStyle(x);
t = x->getright();
p = x->getparent();
g = x->getparent()->getparent();
x->setright(p);
p->setleft(t);
if (p->getparent() != NULL)
{
x->setparent(p->getparent());
if (check == true)
{
p->getparent()->setleft(x);
}
else
{
p->getparent()->setright(x);
}
}
else
{
x->setparent(NULL);
root = x;
//x->getparent()->setright(x);
}
p->setparent(x);
if (t != NULL)
{
t->setparent(p);
}
return x;
}
// n is right child of p and p is right child of GP
else if (x->isRightChild() == true && p->isRightChild() == true)
{
//FIRST ROTATION FROM PARENT
TNode* p1 = x->getparent();
bool check = checkStyle(p1);
TNode* t = p1->getleft();
TNode* p = p1->getparent();
TNode* g = p1->getparent()->getparent();
p1->setleft(p);
p->setright(t);
if (p->getparent() != NULL)
{
p1->setparent(p->getparent());
if (check == true)
{
p->getparent()->setright(p1);
}
else
{
p->getparent()->setleft(p1);
} //x->getparent()->setleft(x);
}
else
{
p1->setparent(NULL);
root = p1;
}
p->setparent(p1);
if (t != NULL)
{
t->setparent(p);
}
//<<<<<<<<<<<<<< 2nd time left rotate >>>>>>>>>>>>>>
check = checkStyle(x);
t = x->getleft();
p = x->getparent();
g = x->getparent()->getparent();
x->setleft(p);
p->setright(t);
if (p->getparent() != NULL)
{
x->setparent(p->getparent());
if (check == true)
{
p->getparent()->setright(x);
}
else
{
p->getparent()->setleft(x);
}
}
else
{
x->setparent(NULL);
root = x;
}
p->setparent(x);
if (t != NULL)
{
t->setparent(p);
}
return x;
}
}
//CASE 3 KA FUNCTION
TNode* BST::funcCase3V(TNode* c)
{
TNode* p = c->getparent();
TNode* g = p->getparent();
if (c->isLeftChild() == true && p->isRightChild() == true)
{
c = rightRotate(c);
c = leftRotate(c) ;
return c;
}
else if (c->isRightChild() == true && p->isLeftChild() == true)
{
c = leftRotate(c) ;
c = rightRotate(c);
return c;
}
}
//asli modify
void BST::asliModify(TNode* x)
{
if (x == root)
{
cout << " It is root node can't Modify it " << endl;
return;
}
else
{
while (x->getparent() != NULL)
{
TNode* g = x->getparent()->getparent();
if (x != root && x->getparent() != NULL && g == NULL)
{
x = funcCase1(x);
}
else if (x->isLeftChild() == true && x->getparent()->isLeftChild() == true || x->isRightChild() == true && x->getparent()->isRightChild() == true)
{
x = funcCase2(x);
if (x->getparent() == NULL)
{
root = x;
}
}
else if (x->isLeftChild() == true && x->getparent()->isRightChild() == true || x->isRightChild() == true && x->getparent()->isLeftChild() == true)
{
x = funcCase3V(x);
if (x->getparent() == NULL)
{
root = x;
}
}
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// project level 2 funcs
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
void BST::displayStudentRecord()
{
int check = 0;
cout << "Press 1 to search a student record by ROLL No. :" << endl;
cout << "Press 2 to search a student record by Name :" << endl;
cout << "Press 3 to see all students record :" << endl;
cout << "Press 4 to see record in ascending order:" << endl;
cout << "Press 5 to see tree :" << endl;
cin >> check ;
if (check == 1 )
{
int r = 0;
cout << "Enter Roll NO to search :" << endl;
cin >> r;
searchByRollNO(r);
}
else if(check == 2)
{
string n ;
cout << "Enter Name to search :" << endl;
cin >> n;
searchingByName_terevasal(root, n);
}
else if (check == 3)
{
cout << " < All students Records > " << endl;
preorder(root);
}
else if (check == 4)
{
cout << " < All students Records in Ascending > " << endl;
inorder(root);
}
else if (check == 5)
{
cout << " < All students Records in Tree > " << endl;
printTree();
}
else
{
cout << " invalid entry" << endl;
}
}
//search by roll no
void BST::searchByRollNO(int r)
{
TNode* current = root;
TNode* previous = current;
while (current != NULL)
{
if (current->getrollNO() == r)
{
break;
}
else if (current->getrollNO() > r)
{
previous = current;
current = current->getleft();
}
else
{
previous = current;
current = current->getright();
}
}
if (current != NULL)
{
cout << " Student Found " << endl;
visit(current);
asliModify(current);
//cout << " MODIFIED TREE AFTER SEARCH !!!! " << endl;
//printBinaryTree(root);
}
else
{
cout << "Student Not Found" << endl;
asliModify(previous);
}
}
//update record
void BST::updateStudentRecord()
{
int check = 0;
cout << "Press 1 to Edit Student Name :" << endl;
cout << "Press 2 to Edit a student Grade :" << endl;
cout << "Press 3 to Add a New Course for a specific Student :" << endl;
cin >> check;
if (check == 1)
{
editStudentName();
}
else if (check == 2 )
{
editStudentGrade();
}
else if (check == 3)
{
addNewCourse();
}
else
{
cout << " Invalid Entry " << endl;
}
}
void BST::editStudentName()
{
int r = 0;
cout << "Enter roll of a student for Change the name :" << endl;
cin >> r;
TNode* current = root;
TNode* previous = current;
while (current != NULL)
{
if (current->getrollNO() == r)
{
break;
}
else if (current->getrollNO() > r)
{
previous = current;
current = current->getleft();
}
else
{
previous = current;
current = current->getright();
}
}
if (current != NULL)
{
string newName;
cout << " Student Found " << endl;
visit(current);
cout << " Enter The New Name of student :" << endl;
cin >> newName;
current->setsName(newName);
asliModify(current);
//cout << " MODIFIED TREE AFTER SEARCH !!!! " << endl;
//printBinaryTree(root);
}
else
{
cout << "Student Not Found" << endl;
asliModify(previous);
}
}
void BST::editStudentGrade()
{
int r = 0;
cout << "Enter roll of a student for Change the Grade :" << endl;
cin >> r;
TNode* current = root;
TNode* previous = current;
while (current != NULL)
{
if (current->getrollNO() == r)
{
break;
}
else if (current->getrollNO() > r)
{
previous = current;
current = current->getleft();
}
else
{
previous = current;
current = current->getright();
}
}
if (current != NULL)
{
cout << " Student Found " << endl;
visit(current);
current->editGrade();
asliModify(current);
//cout << " MODIFIED TREE AFTER SEARCH !!!! " << endl;
//printBinaryTree(root);
}
else
{
cout << "Student Not Found" << endl;
asliModify(previous);
}
}
void BST::addNewCourse()
{
int r = 0;
cout << "Enter roll of a student for ADD a COURSE :" << endl;
cin >> r;
TNode* current = root;
TNode* previous = current;
while (current != NULL)
{
if (current->getrollNO() == r)
{
break;
}
else if (current->getrollNO() > r)
{
previous = current;
current = current->getleft();
}
else
{
previous = current;
current = current->getright();
}
}
if (current != NULL)
{
cout << " Student Found " << endl;
visit(current);
current->addNewCourse();
asliModify(current);
//cout << " MODIFIED TREE AFTER SEARCH !!!! " << endl;
//printBinaryTree(root);
}
else
{
cout << "Student Not Found" << endl;
asliModify(previous);
}
}
//update codes end
//delete record functions
void BST::deleteStudentByRollNO(int r)
{
remove(r);
}
void BST::deleteStudentByName(string n)
{
delByNameTrivarsal(root, n);
}
void BST::delByNameTrivarsal(TNode* t, string n)
{
if (t) {
if (t->getsName() == n)
{
remove(t->getrollNO());
cout << " Student removed Successfully !!" << endl;
return;
}
delByNameTrivarsal(t->getleft() , n);
delByNameTrivarsal(t->getright(), n);
}
}
void BST::deleteCourseof_W_Grade(int r)
{
TNode* current = root;
TNode* previous = current;
while (current != NULL)
{
if (current->getrollNO() == r)
{
break;
}
else if (current->getrollNO() > r)
{
previous = current;
current = current->getleft();
}
else
{
previous = current;
current = current->getright();
}
}
if (current != NULL)
{
cout << " Student Found " << endl;
visit(current);
current->del_W_courses();
asliModify(current);
cout << " Courses of this student with W grade has removed SUCCESSFULLY !!" << endl;
}
else
{
cout << "Student Not Found" << endl;
asliModify(previous);
}
}
void BST::deleteRecordsMenu()
{
int c = 0;
cout << " Press 1 to DELETE record by roll no :" << endl;
cout << " Press 2 to DELETE record by name :" << endl;
cout << " Press 3 to DELETE a course with W grade :" << endl;
cin >> c;
if (c == 1)
{
int r = 0;
cout << " Enter roll no of a student " << endl;
cin >> r;
deleteStudentByRollNO(r);
}
else if (c == 2)
{
string n;
cout << " Enter name of a student " << endl;
cin >> n;
deleteStudentByName(n);
}
else if (c == 3)
{
int r = 0;
cout << " Enter roll no of a student " << endl;
cin >> r;
deleteCourseof_W_Grade(r);
}
}
// deletion ended
//list of enrolled students
void BST::listOfStudentsInCourse()
{
string course;
cout << "Enter a to get the list of studnts enrolled in :" << endl;
cin >> course;
calc_Listof_EnrolledStudents(root, course);
}
void BST::calc_Listof_EnrolledStudents(TNode *t ,string c)
{
if (t) {
if (t->enrolledInCourse(c) == true)
{
cout << t->getsName() << endl;
}
calc_Listof_EnrolledStudents(t->getleft(), c);
calc_Listof_EnrolledStudents(t->getright(), c);
}
}
//cal GPA
void BST::calculateGpa()
{
int r = 0;
cout << " Enter the roll no of student to Calculate GPA :" << endl;
cin >> r;
TNode* current = root;
TNode* previous = current;
while (current != NULL)
{
if (current->getrollNO() == r)
{
break;
}
else if (current->getrollNO() > r)
{
previous = current;
current = current->getleft();
}
else
{
previous = current;
current = current->getright();
}
}
if (current != NULL)
{
cout << " Student Found " << endl;
visit(current);
float gpa = 0;
gpa = current->calGPA();
cout << "GPA of a student is :" << gpa << endl;
asliModify(current);
}
else
{
cout << "Student Not Found" << endl;
asliModify(previous);
}
}
//student with highest gpa
void BST::studentHighestGPA()
{
float g = 0;
TNode* highestGpaNode = highestGpaTraversal(root, g);
//visit(highestGpaNode);
cout <<"highsest gpa :"<< g <<endl;
}
TNode* BST::highestGpaTraversal(TNode* t , float &g)
{
if (t) {
highestGpaTraversal(t->getleft(), g);
if (t->calGPA() > g)
{
g = t->calGPA();
cout << "STUDENT WITH HIGHEST GPA" << endl;
visit(t);
return t;
}
highestGpaTraversal(t->getright(), g);
}
}
/// dean honor list
void BST::deansHonorList()
{
cout << " DEAN's HONOR LIST " << endl;
deanListTraversal(root);
}
void BST::deanListTraversal(TNode* t)
{
if (t)
{
deanListTraversal(t->getleft());
if (t->deanList() == true)
{
float gpa = t->calGPA();
cout << " GPA of Student :" << gpa << endl;
visit(t);
}
deanListTraversal(t->getright());
}
}
// file handling
void BST::writingtofile()
{
fileWritingTraversal(root);
}
void BST::fileWritingTraversal(TNode* t)
{
if (t)
{
visitforWriting(t);
fileWritingTraversal(t->getleft());
fileWritingTraversal(t->getright());
}
}
void BST::visitforWriting(TNode *t)
{
ofstream out("student.txt", ios::app);
out << t->getrollNO()<<" "<< t->getsName() << endl;
string cname = t->getsName() + ".txt";
t->writingCourses(cname);
}
//reading to file
void BST::readingfromFile()
{
int rollno = 0;
string n;
ifstream in("student.txt");
while (!in.eof())
{
in >> rollno >> n;
//cout << "Roll NO:" << rollno << " Name :" << n << endl;
addNewStudentFile(n, rollno);
}
}
#endif