+ -
当前位置:首页 → 问答吧 → 二叉树

二叉树

时间:2011-12-08

来源:互联网

复制内容到剪贴板
代码:
#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;
}
数据结构3.cpp; y# c. h- x; F/ M
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
只是把错误改掉了,不知可以不
复制内容到剪贴板
代码:
#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

热门下载

更多