二叉树
时间:2011-12-08
来源:互联网
复制内容到剪贴板
using namespace std;
template<class T>
class BiTree;
template<class T>
class BTnode
{
friend class BiTree<T>;
private:
T data;
BTnode<T> *LC,*RC;//左指针,右指针
public:
BTnode(){LC=RC=NULL;}
};
template<class T>
class BiTree
{
private:
BTnode<T> *root;
T s;
void create(BTnode<T> * &t);
void visit(BTnode<T> * t);
int size(BTnode<T> *t);
public:
BiTree(){root=NULL;}
void createBT(){create(root);}//创建二叉树
void visitBT(){visit(root);}//先序遍历二叉树
int sizeBT()
{
int sum;
sum=size(root);
return sum;
}
};
template<class T>
void BiTree<T>:: create(BTnode <T>* &t)
{
T ch;
cin>>ch;
if(ch==' ')
t=NULL;//空格字符表示空树
else
{
t=new BTnode<T>;
t->data=ch;
create(t->LC);
create(t->RC);
}
}
template<class T>
void BiTree<T>:: visit(BTnode <T>* &t)
{
if(t=!NULL)
{
cout<<t->data<<" ";
visit(t->LC);
visit(t->RC);
}
}
template<class T>
int BiTree<T>::size(BTnode<T>*t)
{
if(t==NULL)
return 0;
else
return (1+size(t->LC)+size(t->RC));
}
int main()
{
BiTree<char> c1;
c1.createBT();
c1.visitBT();
cout<<endl;
int m;
m=c1.sizeBT();
cout<<"结点个数:"<<m;
return 0;
}
数据结构3.cpp; y# c. h- x; F/ M代码:
#include<iostream>using namespace std;
template<class T>
class BiTree;
template<class T>
class BTnode
{
friend class BiTree<T>;
private:
T data;
BTnode<T> *LC,*RC;//左指针,右指针
public:
BTnode(){LC=RC=NULL;}
};
template<class T>
class BiTree
{
private:
BTnode<T> *root;
T s;
void create(BTnode<T> * &t);
void visit(BTnode<T> * t);
int size(BTnode<T> *t);
public:
BiTree(){root=NULL;}
void createBT(){create(root);}//创建二叉树
void visitBT(){visit(root);}//先序遍历二叉树
int sizeBT()
{
int sum;
sum=size(root);
return sum;
}
};
template<class T>
void BiTree<T>:: create(BTnode <T>* &t)
{
T ch;
cin>>ch;
if(ch==' ')
t=NULL;//空格字符表示空树
else
{
t=new BTnode<T>;
t->data=ch;
create(t->LC);
create(t->RC);
}
}
template<class T>
void BiTree<T>:: visit(BTnode <T>* &t)
{
if(t=!NULL)
{
cout<<t->data<<" ";
visit(t->LC);
visit(t->RC);
}
}
template<class T>
int BiTree<T>::size(BTnode<T>*t)
{
if(t==NULL)
return 0;
else
return (1+size(t->LC)+size(t->RC));
}
int main()
{
BiTree<char> c1;
c1.createBT();
c1.visitBT();
cout<<endl;
int m;
m=c1.sizeBT();
cout<<"结点个数:"<<m;
return 0;
}
D:\我的C++\数据结构3.cpp(59) : error C2244: 'BiTree<T>::visit' : unable to resolve function overload) U8 F6 A, _; R0 n' u$ h4 u# E) u
D:\我的C++\数据结构3.cpp(60) : error C2954: template definitions cannot nest
& a6 C- F3 v: I7 Z执行 cl.exe 时出错.& {' ~& Q- d0 ~, a0 A* q
数据结构3.obj - 1 error(s), 0 warning(s)
% |% g, s- D3 _& j( Q$ I愣是没弄明白 求指点
7 A6 n5 p# @) t" S0 U# E& |) B s- l5 M0 e! G8 X% G! ~: F5 P, ?
[ 本帖最后由 月夜幻影 于 2011-12-8 22:13 编辑 ]
作者: 逍遥小仙 发布时间: 2011-12-08
还没有看过树3 _) R7 L3 M# m+ X9 T% h
只是把错误改掉了,不知可以不
只是把错误改掉了,不知可以不
复制内容到剪贴板
using namespace std;
template<class T>
class BiTree;
template<class T>
class BTnode
{
friend class BiTree<T>;
private:
T data;
BTnode<T> *LC,*RC;//左指针,右指针
public:
BTnode(){LC=RC=NULL;}
};
template<class T>
class BiTree
{
private:
BTnode<T> *root;
T s;
void create(BTnode<T> * &t);
void visit(BTnode<T> * t);
int size(BTnode<T> *t);
public:
BiTree(){root=NULL;}
void createBT(){create(root);}//创建二叉树
void visitBT(){visit(root);}//先序遍历二叉树
int sizeBT()
{
int sum;
sum=size(root);
return sum;
}
};
template<class T>
void BiTree<T>:: create(BTnode <T>* &t)
{
T ch;
cin>>ch;
if(ch==' ')
t=NULL;//空格字符表示空树
else
{
t=new BTnode<T>;
t->data=ch;
create(t->LC);
create(t->RC);
}
}
template<class T>
void BiTree<T>:: visit(BTnode <T>* t)//多了一个引用号
{
if(t!=NULL)//!=而不是=!
{
cout<<t->data<<" ";
visit(t->LC);
visit(t->RC);
}
}
template<class T>
int BiTree<T>::size(BTnode<T>*t)
{
if(t==NULL)
return 0;
else
return (1+size(t->LC)+size(t->RC));
}
int main()
{
BiTree<char> c1;
c1.createBT();
c1.visitBT();
cout<<endl;
int m;
m=c1.sizeBT();
cout<<"结点个数:"<<m;
return 0;
}
代码:
#include<iostream>using namespace std;
template<class T>
class BiTree;
template<class T>
class BTnode
{
friend class BiTree<T>;
private:
T data;
BTnode<T> *LC,*RC;//左指针,右指针
public:
BTnode(){LC=RC=NULL;}
};
template<class T>
class BiTree
{
private:
BTnode<T> *root;
T s;
void create(BTnode<T> * &t);
void visit(BTnode<T> * t);
int size(BTnode<T> *t);
public:
BiTree(){root=NULL;}
void createBT(){create(root);}//创建二叉树
void visitBT(){visit(root);}//先序遍历二叉树
int sizeBT()
{
int sum;
sum=size(root);
return sum;
}
};
template<class T>
void BiTree<T>:: create(BTnode <T>* &t)
{
T ch;
cin>>ch;
if(ch==' ')
t=NULL;//空格字符表示空树
else
{
t=new BTnode<T>;
t->data=ch;
create(t->LC);
create(t->RC);
}
}
template<class T>
void BiTree<T>:: visit(BTnode <T>* t)//多了一个引用号
{
if(t!=NULL)//!=而不是=!
{
cout<<t->data<<" ";
visit(t->LC);
visit(t->RC);
}
}
template<class T>
int BiTree<T>::size(BTnode<T>*t)
{
if(t==NULL)
return 0;
else
return (1+size(t->LC)+size(t->RC));
}
int main()
{
BiTree<char> c1;
c1.createBT();
c1.visitBT();
cout<<endl;
int m;
m=c1.sizeBT();
cout<<"结点个数:"<<m;
return 0;
}
作者: 月夜幻影 发布时间: 2011-12-08
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28