0

A partner of mine and I are working on an assignment for a class of ours and we have ran into a problem with one our cpp files. The purpose of the assignment is to use a generic binary search tree to implement a student/faculty database. A problem we have run into is that when we try to compile we run into an error on our delete function for the binary search tree. It's telling me that the TreeNode struct I have to create my nodes is an undefined type. However, for the other methods we get no errors. It's specifically for the delete method. Here's some the code of the cpp file with the methods of the BST:

    #include <iostream>
    #include <cstdlib>

    #include "BinarySearchTree.h"

    using namespace std;


    //template <class T> BST<T>::BST():root(NULL){}

    template <class T> 
    void BST<T>::freeMemory(BST::TreeNode *node)
    {
        if(node == nullptr)
            return;
        freeMemory(node->left);
        freeMemory(node->right);
        delete node;
    }

    template <class T>
    void BST<T>::insert(T value)
    {
        TreeNode *treeNode = NULL;

        /*try
        {

        }
        catch()
        */

        TreeNode *temp = NULL;
        TreeNode *prev = NULL;

        temp = root;
        while(temp)
        {
            prev = temp;
            if(temp->data < treeNode->data)
                temp = temp->right;
            else
                temp = temp->left;
        }
        if(prev == NULL)
           root = treeNode;
        else 
        {
            if(prev->data < treeNode->data)
                prev->right = treeNode;
            else
                prev->left = treeNode;
        }

    }

    template<class T>
    void BST<T>::print(TreeNode *root)
    {
        if(root == NULL)
            return;
        print(root->left);
        cout << root->data << endl;
        print(root->right);
    }

    template<class T>
    void BST<T>:: print()
    {
         print(root);
    }

    template<class T>
    void BST<t>:: deleteNode(TreeNode *n, T item)
    {
        bool found = false;
        TreeNode *prev = nullptr;
        TreeNode *current = n;

        if(current == nullptr)
                cout<<"There is nothing in your tree."<< endl;
                return;
        while(current != nullptr)
        {
            if(current->data == item)
            {
                found = true;
                break;
            }
            else
            {
                prev = current;
                if(item > (current->data))
                     current = current->right;
                else
                     current = current->left;
            }
        }
        if (!found)
        {
            cout << item <<" not in Tree."<< endl;
            return;
        }
        if((current->left==nullptr && current->right!= nullptr ||         (current->left != nullptr && current->right == nullptr))
        {
            if(current->left == nullptr && current->right != nullptr)
            {
               if(prev->left == current)
               {
                   prev->left = current->right;
                   delete current;
                   current = nullptr;
                   cout << item <<" has been removed from the tree. "<<endl;
                 }
               else
               {
                  prev->right = current->right;
                  delete current;
                  current = nullptr;
                  cout << item <<" has been removed from the tree. "<<endl;
                }

            }
            else
            {
                if(prev->left == current)
                {
                    prev->left = current->left;
                    delete current;
                    current = nullptr;
                    cout << item <<" has been removed from the tree. "<<endl;
                }
                else
                {
                    prev->right = current->left;
                    delete current;
                    current = nullptr;
                    cout << item <<" has been removed from the tree. "<<endl;
                }
             }
             return;
        }
        if(current->left == nullptr && current->right == nullptr)
        {
            if(prev->left == current)
                prev->left = nullptr;
            else
                prev->right = nullptr;
            delete current;

            cout << item <<" has been removed from the tree. "<<endl;
            return;
          }

        if(current->left != nullptr && current->right != nullptr)
        {
            TreeNode *check = current->right;
            if((current->left == nullptr) && (current->right != nullptr))
            {
                current = check;
                delete check;
                current->right == nullptr;
                cout << item <<" has been removed from the tree. "<<endl;
            }
            else
            {
                if((current->right)->left != nullptr)
                {
                    TreeNode *leftCurrent;
                    TreeNode *leftCurrentprev;
                    leftCurrentprev = current->right;
                    leftCurrent = (current->right)->left;
                    while(leftCurrent->left != nullptr)
                    {
                        leftCurrentprev = leftCurrent;
                        leftCurrent = leftCurrent->left;
                    }
                    current->data = leftCurrent->data;
                    delete leftCurrent;
                    leftCurrentprev-?left == nullptr;
                    cout << item << " has been removed from the tree. "<<endl;
                }
                else
                {
                    TreeNode *temp = current->right;
                    current->data = temp->data;
                    current->right = temp->right;
                    delete temp;
                    cout << item <<" has been removed from the tree. "<<endl;
                }
            }
            return;
        }
    }

Again we have an Unknown identifier type of "TreeNode" that keeps coming up as an error which I'm confused about since it doesn't come up for any other methods.

  • 3
    Please tell me you aren't trying to [compile templates separately](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – user657267 Nov 19 '15 at 06:05
  • Welcome to SO! Show use also "BinarySearchTree.h", please. And, in addition the comment by @user657267, please tell me that your main() is the same file you posted above. No you did just truncate it, right? – decltype_auto Nov 19 '15 at 06:06

1 Answers1

0

Possible case sensitivity related Typo? You have a small 't' where you need a capital 'T' in:

template<class T>   void BST<t>:: deleteNode(TreeNode *n, T item)
RichardPlunkett
  • 2,998
  • 14
  • 14