思路:
firstchild对应lchild
nextsibling对应rchild
其他操作和普通二叉树的操作一样
代码:
int getHeight(BTNode *t){//递归算法
if(!t)
return 0;
int hc=getHeight(t->firstchild);
int hs=getHeight(t->nextsibling);
return (hc+1)>hs?hc+1:hs;
}
int getHeight_(BTNode *t){//非递归算法,层次遍历思想
BTNode *que[MAXSIZE];
int front=-1,rear=-1;
int level=0,last=0;
rear=(rear+1)%MAXSIZE;
que[rear]=t;
BTNode *p;
while(front!=rear){
front=(front+1)%MAXSIZE;
p=que[front];
if(p->firstchild){
rear=(rear+1)%MAXSIZE;
que[rear]=p->firstchild;
}
if(p->nextsibling){
rear=(rear+1)%MAXSIZE;
que[rear]=p->nextsibling;
}
if(front==last){//最右结点出队
level++;
last=rear;//当这一层结点都入队后,把rear赋值给last
}
}
return level;
}
测试:
#include<stdio.h>
#include <stdlib.h>
#inc