I am working on a binary search tree and have came across a few problems that I can not figure out how to correct and was wondering how to fix them.

In my program I am adding, deleting, printing, updating, and counting the total number of nodes. The only things I am having trouble with are the last the the updating the record and counting the nodes. Below is my code for the two functions and main where I am calling them.

this is the input file:
A 444 3.33 CS M A adds to the tree
A 777 2.75 IS F
A 1000 3.21 CS X
A 222 2.88 IS M
A 111 3.39 IS M
U 444 3.35 CS M U update the record
D 111 D deletes from the tree
P P prints the tree
C C prints the number of nodes


this is what the output should be:
<~~~~~~~~~~~~~~GPA Report ~~~~~~~~~~~~~~~>
1000 3.21 CS X **** Invalid data
Inorder:
STUDENT ID GPA MAJOR SEX
222 2.88 IS M
444 3.35 CS M
777 2.75 IS F

Preorder:
STUDENT ID GPA MAJOR SEX
444 3.35 CS M
222 2.88 IS M
777 2.75 IS F

Postorder:
STUDENT ID GPA MAJOR SEX
222 2.88 IS M
777 2.75 IS F
444 3.35 CS M

Number of students: 3

>>> END <<<

This is the code for the number of nodes function:

//TreeType.cxx

void TreeType::numberOfNodes(ofstream& outFile) const
{
  numberOfNodes(root, outFile);
}

void numberOfNodes(TreeNode* tree, ofstream& outFile)
//finds and prints the number of nodes in the tree
{
  if(tree == NULL)
    outFile << "Number of students: " << 0 << endl;
  else
    outFile << "Number of students: " << (numberOfNodes(tree->left, outFile) 
              + 1  numberOfNodes(tree->right, outFile)) << endl;
}

This is my main:

#include "TreeType.h"

int main()
{
  ifstream inFile;
  ofstream outFile;
  inFile.open("in.data");
  outFile.open("out.data");

  if(inFile.fail() || outFile.fail())
    outFile << "Input or Output file FAILED!" << endl;

  ItemType item;
  TreeType tree;
  char command;
  string major;
  char gender;
  bool found;
  int nodes;

  outFile << "<~~~~~~~~~~~~~~GPA Report ~~~~~~~~~~~~~~~>" << endl;

  inFile >> command;
  while(inFile)
    {
      switch (command)
        {
          case 'A':
            item.GetItemFromFile(inFile);

          if(item.ValidItem())
            {
              if(!tree.IsFull())
                tree.InsertItem(item);
              else
                outFile << "~ Tree is Full! No Add! ~" << endl;
            }
          else
            item.WriteInvalidItemToFile(outFile);
          break;

          case 'D':
            item.GetIdFromFile(inFile);
            tree.RetrieveItem(item, found);

            if(!tree.IsEmpty())
              {
                if(found)
                  tree.DeleteItem(item);
              }
            else
              outFile << "~ Tree is Empty! No Delete! ~" << endl;
          break;
          case 'P':
            if(!tree.IsEmpty())
              {
                outFile.setf(ios::showpoint);
                outFile.precision(3);
                outFile.setf(ios::left);
                outFile << endl;
                outFile << "Inorder:" << endl;
                item.PrintHeader(outFile);
                tree.PrintInorder(outFile);
                outFile << endl;
                outFile << "Preorder:" << endl;
                item.PrintHeader(outFile);
                tree.PrintPreorder(outFile);
                outFile << endl;
                outFile << "Postorder:" << endl;
                item.PrintHeader(outFile);
                tree.PrintPostorder(outFile);

              }
            else
              outFile << "~ List is empty! No Print!" << endl;
          break;
          case 'R':
            if(!tree.IsEmpty())
              {
                outFile.setf(ios::showpoint);
                outFile.precision(3);
                outFile.setf(ios::left);
                outFile << endl;
                outFile << "Reverse Inorder:" << endl;
                item.PrintHeader(outFile);
                tree.PrintInorderTraversal(outFile);
                outFile << endl;
                outFile << "Reverse Preorder:" << endl;
                item.PrintHeader(outFile);
                tree.PrintPreorderTraversal(outFile);
                outFile << endl;
                outFile << "Reverse Postorder:" << endl;
                item.PrintHeader(outFile);
                tree.PrintPostorderTraversal(outFile);
              }
            else
              outFile << "~ List is empty! No Print! ~" << endl;
          break;
          case 'C':
            if(!tree.IsEmpty())
              tree.numberOfNodes(outFile);
            else
              outFile << "No nodes in the tree!" << endl;
          break;
          case 'U':
            item.GetIdFromFile(inFile);
            tree.RetrieveItem(item, found);

            if(!tree.IsEmpty())
              {
                if(found)
                  tree.DeleteItem(item);
              }
            else
              outFile << "Tree is empty! No Update!" << endl;

            item.GetItemFromFile(inFile);
            if(item.ValidItem())
              {
                if(!tree.IsFull())
                  tree.InsertItem(item);
              }

        }
      inFile >> command;
    }
  outFile << ">>> END <<<";
  return 0;
}

These are the errors I am getting:

In function ‘int main()’:
error: invalid conversion from ‘void*’ to ‘TreeNode*’
error: initializing argument 1 of ‘void TreeType::numberOfNodes(TreeNode*)’
error: prototype for ‘void TreeType::numberOfNodes(std::ofstream&) const’ does not match any in class ‘TreeType’
error: candidate is: void TreeType::numberOfNodes(TreeNode*)
In function ‘void numberOfNodes(TreeNode*, std::ofstream&)’:
error: void value not ignored as it ought to be

The error I am getting for the updating of the records is that it is deleting the record correctly, but it it printing the old record not the updated one.
Thanks for looking and for your help.

I was able to fix all of my errors that I was getting before but I am still getting an output problem. In the update part of my main it is only deleting the record and it is not adding the record back to the tree.

case 'U':
            item.GetIdFromFile(inFile);
            tree.RetrieveItem(item, found);

            if(!tree.IsEmpty())
              {
                if(found)
                  tree.DeleteItem(item);
              }
            else
              outFile << "Tree is empty! No Update!" << endl;

            item.GetItemFromFile(inFile);
            if(item.ValidItem())
              {
                if(!tree.IsFull())
                  tree.InsertItem(item);
              }

I'm not sure how to fix this so that it will update the record and not just delete the record.

I'm not sure what I should do to make this work, should I make another function to update the records or is there a way that I can change the way I am trying to do it in the main so it works? Thanks for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.