// 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)
{
}*/
我一直在努力解决这个问题,但我想不通,请帮助!谢谢。我想要做的是使用二叉搜索树的字典,错误是
Executive.o:在函数
BinarySearchTree<DictionaryEntry, std::string>::inorderTraverse(BinaryNode<DictionaryEntry>*)': /home/chen/Desktop/Lab10/BinarySearchTree.cpp:132: undefined reference to
BinarySearchTree :: print(BinaryNode )' Executive.o:在函数BinarySearchTree<DictionaryEntry, std::string>::preorderTraverse(BinaryNode<DictionaryEntry>*)': /home/chen/Desktop/Lab10/BinarySearchTree.cpp:142: undefined reference to
BinarySearchTree :: print(BinaryNode )' Executive.o:在函数BinarySearchTree<DictionaryEntry, std::string>::postorderTraverse(BinaryNode<DictionaryEntry>*)': /home/chen/Desktop/Lab10/BinarySearchTree.cpp:155: undefined reference to
BinarySearchTree :: print(BinaryNode *)' collect2:错误:ld返回1退出状态 make:*** [Lab10]错误1
请帮助,非常感谢 更新:即使我删除print()方法,它也会给我完全相同的错误。因此,我们可以认为程序甚至无法在我的binarysearchtree类中找到我的“print()”方法,这真的很奇怪,我确实在我的头文件中声明了这个方法。
答案 0 :(得分:0)
查看postorderTraverse
template<typename ItemType, typename KeyType>
void BinarySearchTree<ItemType, KeyType>::postorderTraverse(BinaryNode<ItemType>* subTreeptr)
现在查看您对print
template<typename ItemType, typename KeyType>
void print(BinaryNode<ItemType>* subTreeptr)
看到区别?您在BinarySearchTree<ItemType, KeyType>::
之后,void
print