// 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 to
BinarySearchTree::print(BinaryNode)' Executive.o: In functionBinarySearchTree<DictionaryEntry, std::string>::preorderTraverse(BinaryNode<DictionaryEntry>*)': /home/chen/Desktop/Lab10/BinarySearchTree.cpp:142: undefined reference to
BinarySearchTree::print(BinaryNode)' Executive.o: In functionBinarySearchTree<DictionaryEntry, std::string>::postorderTraverse(BinaryNode<DictionaryEntry>*)': /home/chen/Desktop/Lab10/BinarySearchTree.cpp:155: undefined reference to
BinarySearchTree::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.