#include <iostream> #include <malloc.h> #define MaxSize 100 using namespace std; typedef struct node { char data; struct node *lchild; struct node *rchild; } BTNode; void CreateBTNode(BTNode *&b,char *str) { BTNode *St[MaxSize],*p=NULL; int top=-1,k,j=0; char ch; b=NULL; ch=str[j]; while (ch!='\0') { switch(ch) { case '(': top++; St[top]=p; k=1; break; case ')': top--; break; case ',': k=2; break; default: p=(BTNode *)malloc(sizeof(BTNode)); p->data=ch; p->lchild=p->rchild=NULL; if (b==NULL) b=p; else { switch(k) { case 1: St[top]->lchild=p; break; case 2: St[top]->rchild=p; break; } } } j++; ch=str[j]; } } BTNode * FindNode(BTNode *b,char x) { BTNode *p; if (b==NULL) { return NULL; } else if (b->data==x) { return b; } else { p=FindNode(b->lchild,x); if (p!=NULL) return p; else return FindNode(b->rchild,x); } } BTNode *LchildNode(BTNode *p) { return p->lchild; } BTNode *RchildNode(BTNode *p) { return p->rchild; } int BTNodeHeight(BTNode *b) { int lchildh,rchildh; if (b==NULL) return 0; else { lchildh=BTNodeHeight(b->lchild); rchildh=BTNodeHeight(b->rchild); return (lchildh>rchildh)?(lchildh+1):(rchildh+1); } } void DispBTNode(BTNode *b) { if(b!=NULL) { cout<<b->data; if (b->lchild!=NULL||b->rchild!=NULL) { cout<<"("; DispBTNode(b->lchild); if (b->rchild!=NULL) cout<<","; DispBTNode(b->rchild); cout<<")"; } } } int BTWidth(BTNode *b) //求二叉树b的宽度 { struct { int lno; //节点的层次编号 BTNode *p; //节点指针 } Qu[MaxSize]; //定义顺序非循环队列 int front,rear; //定义队首和队尾指针 int lnum,max,i,n; front=rear=0; //置队列为空队 if (b!=NULL) { rear++; Qu[rear].p=b; //根节点指针入队 Qu[rear].lno=1; //根节点的层次编号为1 while (rear!=front) //队列不为空 { front++; b=Qu[front].p; //队头出队 lnum=Qu[front].lno; if (b->lchild!=NULL) //左孩子入队 { rear++; Qu[rear].p=b->lchild; Qu[rear].lno=lnum+1; } if (b->rchild!=NULL) //右孩子入队 { rear++; Qu[rear].p=b->rchild; Qu[rear].lno=lnum+1; } } max=0; lnum=1; i=1; while (i<=rear) { n=0; while (i<=rear && Qu[i].lno==lnum) { n++; i++; } lnum=Qu[i].lno; if (n>max) max=n; } return max; } else return 0; } int Nodes(BTNode *b) { int num1,num2; if (b==NULL) return 0; else if (b->lchild==NULL && b->rchild==NULL) return 1; else { num1=Nodes(b->lchild); num2=Nodes(b->rchild); return (num1+num2+1); } } int LeafNodes(BTNode *b) { int num1,num2; if (b==NULL) return 0; else if (b->lchild==NULL && b->rchild==NULL) return 1; else { num1=LeafNodes(b->lchild); num2=LeafNodes(b->rchild); return (num1+num2); } } void DestroyBTNode(BTNode *&b) { if (b!=NULL) { DestroyBTNode(b->lchild); DestroyBTNode(b->rchild); free(b); } } int main() { BTNode *b,*p,*lp,*rp; CreateBTNode(b,"A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))"); cout<<"(1)输出二叉树:"; DispBTNode(b); cout<<endl; cout<<"(2)输出H节点的左右孩子节点值:"<<endl; p=FindNode(b,'H'); if (p!=NULL) { lp=LchildNode(p); if (lp!=NULL) cout<<"左孩子为:"<<lp->data<<endl; else cout<<"无左孩子"<<endl; rp=RchildNode(p); if (rp!=NULL) cout<<"右孩子为:"<<rp->data<<endl; else cout<<"无左孩子"<<endl; } cout<<"(3)输出二叉树b的深度:"<<BTNodeHeight(b)<<endl; cout<<"(4)输出二叉树b的宽度:"<<BTWidth(b)<<endl; cout<<"(5)输出二叉树b的节点个数:"<<Nodes(b)<<endl; cout<<"(6)输出二叉树b的叶子节点个数:"<<LeafNodes(b)<<endl; cout<<"释放二叉树."; DestroyBTNode(b); return 0; }
时间: 2024-10-17 10:54:00