欢迎来到天天文库
浏览记录
ID:20223145
大小:42.00 KB
页数:3页
时间:2018-10-09
《二叉树遍历实例》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、#include#includeusingnamespacestd;classtree{public:intdata;classtree*left,*right;};typedefclasstreenode;typedefnode*btree;btreecreat_tree(btree,int);voidpre(btree);voidin(btree);voidpost(btree);intmain(void){intarr[]={1,2,34,4,46,46,6,46,4,4,5,6};btreeptr=NUL
2、L;cout<<"[原始数组内容]"<3、return0;}btreecreat_tree(btreeroot,intval){btreenewnode,current,backup;newnode=newnode;newnode->data=val;newnode->left=NULL;newnode->right=NULL;if(root==NULL){root=newnode;returnroot;}else{for(current=root;current!=NULL;){backup=current;if(current->data>val)current=current->lef4、t;elsecurrent=current->right;}if(backup->data>val)backup->left=newnode;elsebackup->right=newnode;}returnroot;}voidpre(btreeptr){if(ptr!=NULL){cout<<"["<data<<"]";pre(ptr->left);pre(ptr->right);}}voidin(btreeptr){if(ptr!=NULL){in(ptr->left);cout<<"["<5、->data<<"]";in(ptr->right);}}voidpost(btreeptr){if(ptr!=NULL){post(ptr->left);post(ptr->right);cout<<"["<data<<"]";}}运行结果:可根据实际情况改动!
3、return0;}btreecreat_tree(btreeroot,intval){btreenewnode,current,backup;newnode=newnode;newnode->data=val;newnode->left=NULL;newnode->right=NULL;if(root==NULL){root=newnode;returnroot;}else{for(current=root;current!=NULL;){backup=current;if(current->data>val)current=current->lef
4、t;elsecurrent=current->right;}if(backup->data>val)backup->left=newnode;elsebackup->right=newnode;}returnroot;}voidpre(btreeptr){if(ptr!=NULL){cout<<"["<data<<"]";pre(ptr->left);pre(ptr->right);}}voidin(btreeptr){if(ptr!=NULL){in(ptr->left);cout<<"["<5、->data<<"]";in(ptr->right);}}voidpost(btreeptr){if(ptr!=NULL){post(ptr->left);post(ptr->right);cout<<"["<data<<"]";}}运行结果:可根据实际情况改动!
5、->data<<"]";in(ptr->right);}}voidpost(btreeptr){if(ptr!=NULL){post(ptr->left);post(ptr->right);cout<<"["<data<<"]";}}运行结果:可根据实际情况改动!
此文档下载收益归作者所有