#include<stdio.h>#include<iostream.h>#include<string.h>#include<malloc.h>typedef struct node{char name;int age;struct node *lchild;struct node *rchild;}BTNode;struct data //定義輸入數(shù)據(jù)類型{int key;char n;};int InsertBT(BTNode *&p,data); //插入結點void CreateBTNode(BTNode *&b,int i); //創(chuàng)建二叉樹void DispBTNode(BTNode *b); //括號表示法輸出二叉樹bint BTNodeDepth(BTNode *b); //輸出二叉樹b的深度void AgeMin(BTNode *b); //查找并輸出年齡最小的元素void AgeMax(BTNode *b); //查找并輸出年齡最大的元素void main(){BTNode *b;data t;cout<<"(1)輸入12個數(shù)據(jù)并生成二叉樹b(先名后齡):"<<endl;CreateBTNode(b,12);cout<<"(2)括號表示法輸出二叉樹b:"<<endl;DispBTNode(b);cout<<"(3)二叉樹b的深度:"<<BTNodeDepth(b)<<endl;cout<<"(4)年齡最小的元素:";AgeMin(b);cout<<"(5)年齡最大的元素:";AgeMax(b);cout<<"(6)插入{'郭',40}:";cin>>t.key>>t.n;InsertBT(b,t);DispBTNode(b);}int InsertBT(BTNode *&p,data e){if(p==NULL){p=(BTNode*)malloc(sizeof(BTNode));p->age=e.key;p->name=e.n;p->lchild=p->rchild=NULL;return 1;}else if(e.key==p->age) return 0; //有相同關鍵字結點,返回0else if(e.key<=p->age) return InsertBT(p->lchild,e); //插入*p的左子樹中else return InsertBT(p->rchild,e); //插入*p的右子樹中}void CreateBTNode(BTNode *&p,int i){BTNode *b=NULL;int j,k=0;data d[12];for(j=0;j<i;j++)cin>>d[j].n>>d[j].key;while(k<i){InsertBT(b,d[k]);k++;}}void DispBTNode(BTNode *b){if(b!=NULL){cout<<b->name;if(b->lchild!=NULL||b->rchild!=NULL){cout<<"(";DispBTNode(b->lchild);if(b->rchild!=NULL)cout<<",";DispBTNode(b->rchild);cout<<")";}}}int BTNodeDepth(BTNode *b){int lchilddep,rchilddep;if(b==NULL) return(0);else{lchilddep=BTNodeDepth(b->lchild);rchilddep=BTNodeDepth(b->rchild);return(lchilddep>rchilddep)?(lchilddep+1):(rchilddep+1);}}void AgeMin(BTNode *b){if(b->lchild!=NULL)AgeMin(b->lchild);elsecout<<b->name<<" "<<b->age<<endl;}void AgeMax(BTNode *b){if(b->rchild!=NULL)AgeMax(b->rchild);elsecout<<b->name<<" "<<b->age<<endl;}如輸入:{“張”, 25}, {“王”, 18}, {“鐘”, 2}, {“陳”, 46}, {“李”,53}, {“吳”, 39}, {“劉”, 4}, {“鄧”, 74}, {“陸”, 32}, {“丁”, 11}, {“蔣”, 67}, {“洪”, 60},以age為關鍵字
想知道cout<<"(2)括號表示法輸出二叉樹b:"<<endl;之后為什么運行不下去?
滄海一幻覺
2023-04-02 21:17:23