0
//  Subsetted from:
//  Created by Frank M. Carrano and Tim Henry.
//  Copyright (c) 2013 __Pearson Education__. All rights reserved.

/** @file BinarySearchTree.cpp */
#include <iostream>
#include "BinarySearchTree.h"
// PRIVATE HELPER METHODS - IMPLEMENT THESE
template<typename ItemType, typename KeyType>
BinarySearchTree<ItemType, KeyType>::BinarySearchTree() : rootPtr(nullptr)
{
}

template<typename ItemType, typename KeyType>
BinarySearchTree<ItemType, KeyType>::~BinarySearchTree()
{
    if(rootPtr!=nullptr)
    {
        this->destroyTree(rootPtr); // Call inherited method
        rootPtr=nullptr;
    }
}  // end destructor

template<typename ItemType, typename KeyType>
void BinarySearchTree<ItemType, KeyType>::destroyTree(BinaryNode<ItemType>* subTreePtr)
{
    if(subTreePtr->getLeftChildPtr()!=nullptr)
    {
        destroyTree(subTreePtr->getLeftChildPtr());
    }
    if(subTreePtr->getRightChildPtr()!=nullptr)
    {
        destroyTree(subTreePtr->getRightChildPtr());
    }
    delete subTreePtr;
    subTreePtr=nullptr;
}

template<typename ItemType, typename KeyType>
BinaryNode<ItemType>* BinarySearchTree<ItemType,KeyType>::insertInorder(BinaryNode<ItemType>* subTreePtr, BinaryNode<ItemType>* newNode)
{
    BinaryNode<ItemType>* ptr=nullptr;
    if(subTreePtr==nullptr)
    {
        return newNode;
    }
    if(newNode->getItem() > subTreePtr->getItem())
    {
        ptr=insertInorder(subTreePtr->getRightChildPtr(), newNode);
        subTreePtr->setRightChildPtr(ptr);
    }
    else if(newNode->getItem() < subTreePtr->getItem())
    {
        ptr=insertInorder(subTreePtr->getLeftChildPtr(), newNode);
        subTreePtr->setLeftChildPtr(ptr);
    }

    return subTreePtr;
}

template<typename ItemType, typename KeyType>
BinaryNode<ItemType>* BinarySearchTree<ItemType, KeyType>::findNode(BinaryNode<ItemType>* subTreePtr, const KeyType& target) const
{
    if(subTreePtr=nullptr)
    {
        return subTreePtr;
    }
    else if(target>subTreePtr)
    {
        findNode(subTreePtr->getRightChildPtr(), target);
    }
    else if(target<subTreePtr)
    {
        findNode(subTreePtr->getLeftChildPtr(), target);
    }
    else
    {
        return subTreePtr;
    }
}


//////////////////////////////////////////////////////////////
//      PUBLIC METHODS BEGIN HERE
//////////////////////////////////////////////////////////////




//////////////////////////////////////////////////////////////
//      Public BinaryTreeInterface Methods Section - IMPLEMENT THESE
//////////////////////////////////////////////////////////////

template<typename ItemType, typename KeyType>
bool BinarySearchTree<ItemType, KeyType>::add(const ItemType& newEntry)
{
    BinaryNode<ItemType>* ptr=new BinaryNode<ItemType>(newEntry);
    rootPtr=insertInorder(rootPtr, ptr);
    return false;
}

template<typename ItemType, typename KeyType>
ItemType BinarySearchTree<ItemType, KeyType>::getEntry(const KeyType& aKey) const throw(NotFoundException)
{
    return findNode()->getItem();
}

template<typename ItemType, typename KeyType>
bool BinarySearchTree<ItemType, KeyType>::contains(const KeyType& aKey) const
{
    if(findNode(rootPtr, aKey)==nullptr)
    {
        return false;
    }
    else
    {
        return true;
    }
}


//////////////////////////////////////////////////////////////
//      Public Traversals Section - IMPLEMENT THESE
//////////////////////////////////////////////////////////////

template<typename ItemType, typename KeyType>
void BinarySearchTree<ItemType, KeyType>::inorderTraverse(BinaryNode<ItemType>* subTreeptr)
{
    if(subTreeptr!=nullptr)
    {
        inorderTraverse(subTreeptr->getLeftChildPtr());
        print(subTreeptr);
        inorderTraverse(subTreeptr->getRightChildPtr());
    }
}

template<typename ItemType, typename KeyType>
void BinarySearchTree<ItemType, KeyType>::preorderTraverse(BinaryNode<ItemType>* subTreeptr)
{
    if(subTreeptr!=nullptr)
    {
        print(subTreeptr);
        preorderTraverse(subTreeptr->getLeftChildPtr());
        preorderTraverse(subTreeptr->getRightChildPtr());
    }
}

template<typename ItemType, typename KeyType>
void BinarySearchTree<ItemType, KeyType>::postorderTraverse(BinaryNode<ItemType>* subTreeptr)
{
    if(subTreeptr!=nullptr)
    {
        postorderTraverse(subTreeptr->getLeftChildPtr());
        postorderTraverse(subTreeptr->getRightChildPtr());
        print(subTreeptr);
    }
}

template<typename ItemType, typename KeyType>
void BinarySearchTree<ItemType, KeyType>::print(BinaryNode<ItemType>* subTreeptr)
{
    cout<<subTreeptr->getItem().getword()<<"  "<<subTreeptr->getItem().getdefn()<<endl;
}

template<typename ItemType, typename KeyType>
BinaryNode<ItemType>* BinarySearchTree<ItemType, KeyType>::getrootPtr()
{
    return rootPtr;
}
/*void testAdds(BinarySearchTree<DictionaryEntry, std::string> dictionary)
{

}

void testRemoves(BinarySearchTree<DictionaryEntry, std::string> dictionary)
{

}

void testWriteToFile(BinarySearchTree<DictionaryEntry, std::string> dictionary)
{

}*/

I have been trying to fix this forever, but I can't figure out, please help!!! Thank you. What I am trying to do is a dictionary by using binary search tree, the error is

Executive.o: In function BinarySearchTree<DictionaryEntry, std::string>::inorderTraverse(BinaryNode<DictionaryEntry>*)': /home/chen/Desktop/Lab10/BinarySearchTree.cpp:132: undefined reference toBinarySearchTree::print(BinaryNode)' Executive.o: In function BinarySearchTree<DictionaryEntry, std::string>::preorderTraverse(BinaryNode<DictionaryEntry>*)': /home/chen/Desktop/Lab10/BinarySearchTree.cpp:142: undefined reference toBinarySearchTree::print(BinaryNode)' Executive.o: In function BinarySearchTree<DictionaryEntry, std::string>::postorderTraverse(BinaryNode<DictionaryEntry>*)': /home/chen/Desktop/Lab10/BinarySearchTree.cpp:155: undefined reference toBinarySearchTree::print(BinaryNode*)' collect2: error: ld returned 1 exit status make: *** [Lab10] Error 1

Please help, thank you very much Update:Even when I delete the print() method, it would give me exactly the same error. Therefore, we could consider the program couldn't even find my "print()" method in my binarysearchtree class, which is really weird, I do have this method declared in my header file.

Chen Long
  • 69
  • 6
  • There was an error, I fixed it but the problem keeps the same... I'm dying for this error.. – Chen Long May 02 '16 at 05:31
  • Need more information. Show the commands you use to compile this program. Make sure you are compiling your main file along with this file you just posted – smac89 May 02 '16 at 05:41
  • 2
    `BinarySearchTree.cpp ` ?? - My crystal ball tells me you need to read this: [**"Why can templates only be implemented in the header file?"**](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file). Short answer: there should be no `BinarySearchTree.cpp` file; all this code should be in the *header file*. – WhozCraig May 02 '16 at 06:42

1 Answers1

0

Look at the definition of postorderTraverse

template<typename ItemType, typename KeyType>
void BinarySearchTree<ItemType, KeyType>::postorderTraverse(BinaryNode<ItemType>* subTreeptr)

now look at your definition of print

template<typename ItemType, typename KeyType>
void print(BinaryNode<ItemType>* subTreeptr)

See the difference? You are missing BinarySearchTree<ItemType, KeyType>:: after the void, but before the print

The Dark
  • 8,453
  • 1
  • 16
  • 19
  • Oh, I see it, but problem still exist... This is an error but the problem is still there even when I fix this. Thank you anyway!!!! – Chen Long May 02 '16 at 05:28