树的存储结构
1、parent storage structure
typedef struct {
ElemType data;
int parent;
}PTree[MAXSIZE];
2、child chain storage structure
typedef struct node {
ElemType data;
struct node* sons[MAXSONS];
}TSonNode;
求树高
int TreeHeight1(TSonNode* t)
{
int h, maxh = 0;
TSonNode* p;
if (t == NULL)
return 0;
else
{
for (int i = 0; i < MAXSONS; i++)
{
p = t->sons[i];
if (p != NULL)
{
h = TreeHeight1(p);
}
if (h > maxh)
maxh = h;
}
}
return(maxh + 1);
}
3、child brother chain storage structure
typedef struct tnode {
ElemType data;
struct tnode* hp;//指向兄弟
struct tnode* vp;//指向孩子
}TSBNode;
求树高
int TreeHeight2(TSBNode* t)
{
TSBNode* p;
int h, maxh = 0;
if (t == NULL)
return 0;
else
{
p = t->vp;
while (p != NULL)
{
h = TreeHeight2(p);
if (maxh < h)
maxh = h;
p = p->hp;
}
}
return(maxh + 1);
}