永发信息网

BTree 问题

答案:1  悬赏:50  手机版
解决时间 2021-07-19 00:54
  • 提问者网友:十年饮冰
  • 2021-07-18 00:51

#include<iostream>
using namespace std;
template<class T>class BTree;
template<class T>
class BTreeNode
{
friend class BTree<T>;
T data;
BTreeNode<T> *lchild,*rchild;
public:
BTreeNode(){lchild=NULL;rchild=NULL;}
BTreeNode(T d,BTreeNode<T> *l=NULL,BTreeNode<T> *r=NULL):data(d),rchild(r),lchild(l){}
T getdata(){return data;}
BTreeNode<T>* getleft(){return lchild;}
BTreeNode<T>* getright(){return rchild;}

};

template<class T>
class BTree
{
T *a;int n;
BTreeNode<T>* build0(int i);
protected:
BTreeNode<T>* root;
public:
BTree(BTreeNode<T>* p=NULL){copybt(root,p);}
BTree(T a[],int n);
void build(T a[],int n);
int num();
static int num(BTreeNode<T> *p);
int dep();
static int dep(BTreeNode<T>* p);
bool equal(BTree& bt);
static bool equal(BTreeNode<T>* p,BTreeNode<T>* q);
void copybt(BTree& bt);
static void copybt(BTreeNode<T>* &p,BTreeNode<T>* q);
void preorder(void visit(BTreeNode<T> *p));
static void preorder(BTreeNode<T>* p,void visit(BTreeNode<T> *p));
void inorder(void visit(BTreeNode<T>* p));
static void inorder(BTreeNode<T>* p,void visit(BTreeNode<T> *p));
void inorder1(void visit(BTreeNode<T>* p));
void postorder(void visit(BTreeNode<T>* p));
static void postorder(BTreeNode<T>* p,void visit(BTreeNode<T> *p));
void preorder(BTreeNode<T> *p);


static void prnt(BTreeNode<T> *p,int l);
void prnt();
void dest(BTreeNode<T> *p);
int nodenum(BTreeNode<T> *p);
void nodenum();
//void nodenum();
};

template<class T>
BTree<T>::BTree(T a[],int n)
{
this->a=a;this->n=n;
root=build0(1);
}
template<class T>
BTreeNode<T>* BTree<T>::build0(int i)
{
BTreeNode<T> *p;int l,r;
if((i<=n)&&(a[i]!=' '))
{
p=new BTreeNode<T>;p->data=a[i-1];
l=2*i;r=2*i+1;
p->lchild=build0(l);
p->rchild=build0(r);
return p;
}
else
return NULL;
}
template<class T>
int BTree<T>::num()
{
return num(root);
}
template<class T>
int BTree<T>::num(BTreeNode<T> *p)
{
if(p==NULL)return 0;
else
return (num(p->lchild)+num(p->rchild)+1);
}
template<class T>
int BTree<T>::dep()
{
return dep(root);
}
template<class T>
int BTree<T>::dep(BTreeNode<T> *p)
{
int max;
if(p==NULL)return 0;
else
{
max=dep(p->lchild);
if(dep(p->rchild)>max)max=dep(p->rchild);
return (max+1);
}
}
template<class T>
bool BTree<T>::equal(BTreeNode<T> *p,BTreeNode<T> *q)
{
bool b;
if(p==NULL&&q==NULL)b=true;
else(p==NULL||q==NULL)b=false;
else b=(p->data==q->data)&&equal(p->lchild,q->lchild)&&equal(p->rchild,q->rchild);
return b;
}
template<class T>
bool BTree<T>::equal(BTree<T>& bt)
{
return equal(root,bt.root);
}
template<class T>
void BTree<T>::copybt(BTree<T>& bt)
{
dest(root);
copybt(root,bt.root);
}
template<class T>
void BTree<T>::dest(BTreeNode<T> *p)
{
if(p!=NULL)
{
dest(p->lchild);
dest(p->rchild);
cout<<p->data<<" ",
delete p;
}
}
template<class T>
void BTree<T>::copybt(BTreeNode<T> * &p,BTreeNode<T> *q)
{
if(q==NULL)p=NULL;
else{
p=new BTreeNode<T>;
p->data=q->data;
copybt(p->lchild,q->lchild);
copybt(p->rchild,q->rchild);
}
}
template<class T>
void BTree<T>::inorder(void visit(BTreeNode<T> *p))
{
inorder(root,visit);cout<<endl;
}
template<class T>
void BTree<T>::inorder(BTreeNode<T>* p,void visit(BTreeNode<T> *p))
{
if(p!=NULL)
{
inorder(p->lchild,visit);
visit(p);
inorder(p->rchild,visit);
}
}
template<class T>
void BTree<T>::preorder(BTreeNode<T> *p)
{
if(p)
{
cout<<p->data<<" ";
preorder(p->lchild);
preorder(p->rchild);
}
}
template<class T>
void prnt(BTreeNode<T> *p,int l)
{
if(p)
{
prnt(p->rchild,l+1);
for(int i=0;i<6*(l-1);i++)cout<<" ";
cout<<"..."<<p->data<<endl;
prnt(p->lchild,l+1);
}
}
template<class T>
int nodenum(BTreeNode<T> *p)
{
if(p->rchild==NULL||p->lchild==NULL)return 0;
if(p->rchild==NULL&&p->lchild==NULL)return 1;
else
return (nodenum(p->lchild)+nodenum(p->rchild));
}
template<class T>
void BTree<T>::nodenum()
{
nodenum(root);
}

template<class T>
void BTree<T>::prnt()
{
prnt(root, 1);
}

/////////////////////////////////////////////////////////////

main()

#include<iostream>
#include"BTree.h"
//#include"BTreeNode.h"
using namespace std;

void func1(BTreeNode<char> *p){cout<<p->getdata()<<" ";}
void main()
{
char *str="-*cab";


BTree<char> bt(str,5);
bt.inorder(func1);
bt.nodenum();
//bt.inorder(func1);
char *str2="abcd f";
BTree<char> bt2(str2,6);
//bt2.prnt();
}

///////////////////////////////////////////////////

错误提示:

Linking...
test.obj : error LNK2001: unresolved external symbol "public: int __thiscall BTree<char>::nodenum(class BTreeNode<char> *)" (?nodenum@?$BTree@D@@QAEHPAV?$BTreeNode@D@@@Z)
Debug/二叉树(实验).exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

二叉树(实验).exe - 2 error(s), 0 warning(s)

最佳答案
  • 五星知识达人网友:杯酒困英雄
  • 2021-07-18 01:42
template<class T>
void prnt(BTreeNode<T> *p,int l)
{
if(p)
{
prnt(p->rchild,l+1);
for(int i=0;i<6*(l-1);i++)cout<<" ";
cout<<"..."<<p->data<<endl;
prnt(p->lchild,l+1);
}
}
template<class T>
int nodenum(BTreeNode<T> *p)
{
if(p->rchild==NULL||p->lchild==NULL)return 0;
if(p->rchild==NULL&&p->lchild==NULL)return 1;
else
return (nodenum(p->lchild)+nodenum(p->rchild));
}
这两个函数是类成员函数,函数名前面应该添加上类定义。
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯