完整二叉树实例程序.docx
- 文档编号:26445500
- 上传时间:2023-06-19
- 格式:DOCX
- 页数:11
- 大小:47.69KB
完整二叉树实例程序.docx
《完整二叉树实例程序.docx》由会员分享,可在线阅读,更多相关《完整二叉树实例程序.docx(11页珍藏版)》请在冰豆网上搜索。
完整二叉树实例程序
包含二叉树的顺序存储,插入,删除结点,以及设置root结点,和前序、中序、后序、按层次遍历的完成代码。
#include
#include
#include
#defineQUEUE_MAXSIZE50
usingnamespacestd;
typedefstructCTree
{
chardata;
structCTree*left;
structCTree*right;
}CBTree;
CBTree*BTreeInit(CBTree*node)
{
if(node!
=NULL)
returnnode;
else
returnNULL;
}
intBTreeAddNode(CBTree*bt,CBTree*node,intn)
{
if(bt==NULL)
{
cout<<"noparent!
"< return0; } switch(n) { case1: if(bt->left) { cout<<"theleftisexisting."< return0; } else bt->left=node; break; case2: if(bt->right) { cout<<"therightisexisting."< return0; } else bt->right=node; break; default: cout<<"it'swrong! "< return0; } return1; } CBTree*BTreeLeft(CBTree*bt) { if(bt) returnbt->left; else returnNULL; } CBTree*BTreeright(CBTree*bt) { if(bt) returnbt->right; else returnNULL; } intBTreeIsempty(CBTree*bt) { if(bt) return0; else return1; } intBTreeDepth(CBTree*bt) { intdep1,dep2; if(bt==NULL) return0; else { dep1=BTreeDepth(bt->left); dep2=BTreeDepth(bt->right); if(dep1>dep2) returndep1+1; else returndep2+1; } } CBTree*BTreeFind(CBTree*bt,chardata) { CBTree*p; if(bt==NULL) returnNULL; else { if(bt->data==data) returnbt; else { if(p=BTreeFind(bt->left,data)) returnp; elseif(p=BTreeFind(bt->right,data)) returnp; else returnNULL; } } } voidBTreeClear(CBTree*bt) { if(bt) { BTreeClear(bt->left); BTreeClear(bt->right); free(bt); bt=NULL; } return; } voidBTree_pre(CBTree*bt) { if(bt) { cout< BTree_pre(bt->left); BTree_pre(bt->right); } return; } voidBTree_mid(CBTree*bt) { if(bt) { BTree_mid(bt->left); cout< BTree_mid(bt->right); } return; } voidBTree_post(CBTree*bt) { if(bt) { BTree_post(bt->left); BTree_post(bt->right); cout< } return; } voidBTree_level(CBTree*bt) { CBTree*p,*q[QUEUE_MAXSIZE]; inthead=0,tail=0; if(bt) { tail=(tail+1)%QUEUE_MAXSIZE; q[tail]=bt; } while(head! =tail) { head=(head+1)%QUEUE_MAXSIZE; p=q[head]; cout< if(p->left! =NULL) { tail=(tail+1)%QUEUE_MAXSIZE; q[tail]=p->left; } if(p->right! =NULL) { tail=(tail+1)%QUEUE_MAXSIZE; q[tail]=p->right; } } return; } CBTree*InitRoot() { CBTree*node; if(node=(CBTree*)malloc(sizeof(CBTree))) { cout<<"inputtheroot: "; cin>>node->data; node->left=NULL; node->right=NULL; returnnode; } returnNULL; } voidAddNode(CBTree*bt) { CBTree*node,*parent; chardata,select; if(node=(CBTree*)malloc(sizeof(CBTree))) { cout<<"pleaseinputthedata: "; cin>>node->data; node->left=NULL; node->right=NULL; cout<<"pleaseinputtheparent: "; cin>>data; parent=BTreeFind(bt,data); if(! parent) { cout<<"noparent."< free(node); return; } cout<<"1.toleft;2.toright"; do { cin>>select; select=select-'0'; if(select==1||select==2) BTreeAddNode(parent,node,select); }while(select! =1&&select! =2); } return; } 主函数调用为: intmain() { CBTree*root=NULL; charselect; do { cout<<"1.setroot2.addnode3.pre_scan"< cout<<"4.min_scan5.post_scan6.level_scan"< cout<<"7.thedepth0.exit"< cin>>select; switch(select) { case'1': root=InitRoot(); break; case'2': AddNode(root); break; case'3': cout<<"thepre_scanis: "; BTree_pre(root); cout< break; case'4': cout<<"themid_scanis: "; BTree_mid(root); cout< break; case'5': cout<<"thepost_scanis: "; BTree_post(root); cout< break; case'6': cout<<"thelevel_scanis: "; BTree_level(root); cout< break; case'7': cout<<"thedepthis"< break; case'0': break; } }while(select! ='0'); BTreeClear(root); root=NULL; return0; } 实例程序:
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 完整 二叉 实例 程序
![提示](https://static.bdocx.com/images/bang_tan.gif)