当前位置首页 > 建筑/施工 > 其它相关建筑施工文档
搜柄,搜必应! 快速导航 | 使用教程  [会员中心]

二叉树的遍历实例

文档格式:DOCX| 3 页|大小 11.85KB|积分 20|2022-11-14 发布|文档ID:169224671
第1页
下载文档到电脑,查找使用更方便 还剩页未读,继续阅读>>
1 / 3
此文档下载收益归作者所有 下载文档
  • 版权提示
  • 文本预览
  • 常见问题
  • iiiclude# iiicludeusing namespace std;class tree{public:int data;class tree};tvpedef class tree node;tvpedef node *btree;btiee creat_tiee(btree.iiit);void pre(btree);void m(btiee);void post(btiee);iiit mam(void){int an[]={ 12,34,4,46,46,6,46,4,4,5,6}; btiee ptr=NULL;coutvv” [原始数组内容]',«endl; fbr(int i=0;idata=\ral;newnode->left=NULL;newnode->nght=NULL; if(root==NULL)iroot=newnode;retuni root;}elseifor(cuiTent=root;cunent!=NULL;) {backup=cuiTent;if(cunent->data>val) cunent=cunent->left;elsecunent=cunent->nght;}if(backup->data>val) backup->left=newnode;elsebackup->right=newnode;}return root;}void pre(btree ptr){if(ptr !=NULL)icout«M[H«senv(2)«ptr->data«H]H; pre(ptr->left);pre(ptr->right);}}void in(btiee pti){if(ptr!=NULL)im(ptr->left);cout«M[H«senv(2)«ptr->data«H]H; m(ptr->iight);}}void post(btree pti)if(ptr!=NULL)post(ptr->left);post(ptr->iight);cout«,,[H«senv(2)«ptr->data«H]H;}} 运行结果::\Docu>ent s and Sett ingsVAdainist rat or\桌面'Debug\树.exe*[原始数组内容][HL 21L341L 41L461L461L 61L461L 41L 41L 5][HL 21L341L 41L 61L 41L 41L 5 ] [46 ] [46 ] [46 ]中序遍历结果[HL 21L 41L 41L 41L 51L 6] L34] L46] L46] L46]后序遍历结果[51L 41L 41L 61L 4]L46]L46]L46]L34] L 21L 1]情按任意键继续• • •可根据实际情况改动!。

    点击阅读更多内容
    卖家[上传人]:taidaqu
    资质:实名认证