欢迎来到天天文库
浏览记录
ID:59189671
大小:17.50 KB
页数:5页
时间:2020-10-30
《实验六二叉树.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、#include#includetypedefcharElemType;structBTreeNode{ElemTypedata;BTreeNode*left;BTreeNode*right;};voidInitBTree(BTreeNode*&BT){BT=NULL;}voidCreateBTree(BTreeNode*&BT,char*a){constintMaxSize=50;BTreeNode*s[MaxSize];inttop=-1;BT=NULL;BTreeNode*p;intk;inti=0;while(a[i]){switch(a[
2、i]){case'':break;case'(':if(top==MaxSize-1){cout<<""<data=a[i];p->left=p->right=NULL;if(BT==NULL)BT=p;else{if(k==1)s[top]->left=p;elses[top]->right=p;}}i++;}}bo
3、olFindBTree(BTreeNode*BT,ElemType&x){if(BT==NULL)returnfalse;else{if(BT->data==x){x=BT->data;returntrue;}else{if(FindBTree(BT->left,x))returntrue;if(FindBTree(BT->right,x))returntrue;returnfalse;}}}intDepthTree(BTreeNode*BT){if(BT==NULL)return0;else{intdep1=DepthTree(BT->left);intdep2=DepthTree(BT-
4、>right);if(dep1>dep2)returndep1+1;elsereturndep2+1;}}boolEmptyBTree(BTreeNode*BT){returnBT==NULL;}voidPrintBTree(BTreeNode*BT){if(BT==NULL){cout<data;if(BT->left!=NULL
5、
6、BT->right!=NULL){cout<<'(';PrintBTree(BT->left);if(BT->right!=NULL)cout<<',';PrintBTree(BT->right);cout<<')';}}}voidLevelOrde
7、r(BTreeNode*BT){constintMaxSize=30;BTreeNode*q[MaxSize];intfront=0;intrear=0;BTreeNode*p;if(BT==NULL){rear=(rear+1)%MaxSize;q[rear]=BT;}while(front!=rear){front=(front+1)%MaxSize;p=q[front];cout<data<<'';if(p->left!=NULL){rear=(rear+1)%MaxSize;q[rear]=p->left;}if(p->right!=NULL){rear=(rear+1)%M
8、axSize;q[rear]=p->right;}}}voidClearBTree(BTreeNode*&BT){if(BT!=NULL)ClearBTree(BT->left);ClearBTree(BT->right);deleteBT;BT=NULL;}voidmain(){BTreeNode*bt;InitBTree(bt);charb[50];cout<<"输入二叉树用广义表表示的字符串:"<9、r(bt);cout<>x;if(FindBTree(bt,x))cout<<"查找字符"<
9、r(bt);cout<>x;if(FindBTree(bt,x))cout<<"查找字符"<
此文档下载收益归作者所有