24,855
社区成员
发帖
与我相关
我的任务
分享
"BinaryTree<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::PrintByLine()", referenced from:
_main in main.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
#include <iostream>
#include "Binary.h"
#include <string>
using namespace std;
int main() {
// insert code here...
BinaryTree <string> tree("this is a root");
BinaryNode <string> * p,*q;
p=new BinaryNode<string>("root's left");
q=tree.GetRoot();
q -> SetLeft(p);
p=new BinaryNode<string>("root's right");
q -> SetRight(p);
tree.PrintByLine();
return 0;
}
/*
* Binary.h
* tree
*/
#ifndef _BINARY_LAOHYX
#define _BINARY_LAOHYX
#include <iostream>
#include <queue>
using namespace std;
template <class type>
class BinaryNode {
private:
BinaryNode<type> *left,*right;
type data;
public:
BinaryNode():left(NULL),right(NULL){} //二叉树的结点构造函数
BinaryNode(type item,BinaryNode<type> *L=NULL,BinaryNode<type> *R=NULL):data(item),left(L),right(R){} //带参数的构造函数
type GetData() const {return data;} //return this tree's data
~BinaryNode(){}
BinaryNode <type> * GetLeft() const {return left;}
BinaryNode <type> * GetRight() const {return right;}
void SetData (const type & item) {data=item;}
void SetLeft (BinaryNode<type> *L) {left=L;}
void SetRight (BinaryNode<type> *R) {right=R;}
int Size(const BinaryNode<type> *T) const; //返回以T为根的树结点个数
int Height(const BinaryNode<type> *T) const; //返回以T为根的树的高度
};
template <class type>
class BinaryTree {
private:
BinaryNode <type> * root; //the root of a tree
BinaryTree (const BinaryTree<type> &);
void DelTree (const BinaryNode<type> *T);
public:
BinaryTree():root(NULL){}
BinaryTree(const type &value)
{root = new BinaryNode<type> (value);}
int IsEmpty() const {return root==NULL;}
BinaryNode<type> * GetRoot() const {return root;}
void MakeEmpty(){DelTree(root);root=NULL;}
void PrintByLine();
//const BinaryTree <type> &operator =(const BinaryTree<type> &T);
};
#endif
/*
* Binary.cpp
* tree
*
*/
#include "Binary.h"
#include <iostream>
using namespace std;
template <class type>
type Max (const type u, const type v)
{
if (u>v)
return u;
else
return v;
}
template <class type>
int BinaryNode <type> :: Size(const BinaryNode<type> *T)const
{
if (T == NULL)
return 0;
else
return 1+Size(T->left)+Size(T->right);
}
template <class type>
int BinaryNode <type> :: Height(const BinaryNode<type> *T)const
{
if (T == NULL)
return 0;
else
return 1+Max(Size(T->left),Size(T->right));
}
template <class type>
void BinaryTree <type> :: DelTree(const BinaryNode<type> *T)
{
if (T != NULL) {
DelTree(T -> left);
DelTree(T -> right);
delete T;
}
}
/****************************************************
按行进行树的输出
****************************************************/
template <class type>
void BinaryTree <type> :: PrintByLine()
{
queue<BinaryNode<type>*> que;
cout << "Print by line :";
que.push(NULL);
que.push(root);
int layer=0;
while (!que.empty()) {
if (que.front()==NULL)
{
que.push(NULL);
que.pop();
if (que.front()==NULL) {
break;
}
layer++;
cout << "\nLayer " << layer << ":\n";
}
cout << que.front() -> GetData() << " ";
que.push(que.front() -> GetLeft());
que.push(que.front() -> GetRight());
que.pop();
}
}