可以给出算法代码不
追答双亲表示的情况参见严慰敏《数据结构》教材
#define MAX_TREE_SIZE 500
typedef struct PTNode
{ // 双亲表示结点
TElemType data;
int parent; // 双亲位置
} PTNode;
typedef struct
{ // 树
PTNode nodes[MAX_TREE_SIZE];
int r, n; // 根的位置与结点数
} PTree;
int Height(PTree *t)
{
int i, j, hmax = 0, h;
for (i = t->n - 1; i >= 0; i --)
{ // 双亲表示中的下标从0 开始
h = 0;
for (j = i; j != -1; j = t->nodes[j].parent)
h ++;
if (h > hmax)
hmax = h;
}
return hmax;
}
其实如果严格按照双亲表示的按树的层次序存放,这个外层循环可以去掉的,因为最后一个元素的高度肯定最大