欢迎来到天天文库
浏览记录
ID:47488662
大小:30.00 KB
页数:3页
时间:2020-01-12
《二叉树的类定义及实现》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、二叉树的类定义#includeclassnode{public:chardata;node*left,*right;node(){left=right=NULL;}node(charinfo){data=info;left=right=NULL;}};classtree{private:node*root;public:tree(){root=NULL;}node*&Root(){returnroot;}//要非常说明的是'&'的作用,着实重要,否则实现不了"建树"的功能voidvisit(node*root);voidpreorder(n
2、ode*root);voidpostorder(node*);};voidcreat(node*&p)//'&'引用符很重要,能否成功建树的关键{charx;cin>>x;//普通函数,不属于tree的成员函数if(x!='#'){p=newnode(x);creat(p->left);creat(p->right);}elsep=NULL;}voidtree::visit(node*root){cout<data<<'t';}voidtree::preorder(node*root){if(root!=NULL){visit(root);preo
3、rder(root->left);preorder(root->right);}}voidtree::postorder(node*root){if(root!=NULL){postorder(root->left);visit(root);postorder(root->right);}}voidmain(){cout<<"请输入二叉树的结点,以#号结束,回车确认:"<<'';treet;creat(t.Root());//普通函数的调用cout<<"前序遍历结果为:";t.preorder(t.Root());cout<4、果为:";t.postorder(t.Root());cout<
4、果为:";t.postorder(t.Root());cout<
此文档下载收益归作者所有