树的高度函数C语言 树的高度怎么求

用c语言求树的高度(数据结构)#includestdio.h
#includestdlib.h
typedef struct node
{
int data;
struct node *next;
}Link;
void insertNode(Link *head, int data) {
Link *p = head;
Link *q = (Link *)malloc(sizeof(Link));
q-data = https://www.04ip.com/post/data;
q-next = NULL;
while(p-next != NULL) p=p-next;
p-next = q;
}
void freeNode(Link *head) {
Link *p = head-next;
Link *q;
head-next = NULL;
while(p != NULL){
q = p;
p=p-next;
free(q);
}
}
int deep(Link ** tree, int start) {
int depth = 1;
Link *p;
if(tree-next == NULL) {
return depth;
}
p= tree-next;
while(p!= NULL){
int tmp = deep(tree, p-data - 1);
if(tmpdepth) depth = tmp;
p=p-next;
}
return depth + 1;
}
int main(){
int count, i;
int a, b;
Link **tree;
scanf("%d", count);
tree = (Link **)malloc(sizeof(Link*)*count);
for(i=0;icount;++i) {
tree[i] = (Link *)malloc(sizeof(Link));
tree[i]-next = NULL;
}
while((scanf("%d%d",a, b))!=EOF){
if(a0b0) {
insertNode(tree[a-1], b);
}
}
printf("%d\n", deep(tree, 0));
for(i=0;icount;++i) {
freeNode(tree[i]);
free(tree[i]);
}
free(tree);
return 0;
}
请用C语言编写一个函数,实现求二叉树高度的算法,并给出结点结构typedef int Status;
typedef char TElemType;
typedef struct BiTNode{
TElemType data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
int GetDepth(BiTree T){
if(!T) return 0;
else{
int depthLeft = GetDepth( T-lchild );
int depthRight= GetDepth( T-rchild );
return (depthLeftdepthRight?depthLeft:depthRight)+1;
}
}
树的高度 , 用C语言编写程序#include stdio.h
int main ()
{
int a, b, n, m = 0;
int tree[1000] = {0}; /* 最大结点数+1 */
scanf("%d", n);
while (n--1) //根节点计算在节点个数内,就是用1,否则使用0
{
scanf("%d %d", a, b);
tree[b] = tree[a] + 1;
if (mtree[b]) m = tree[b];
}
printf("%d", m + 1);
return 0;
}
【树的高度函数C语言 树的高度怎么求】树的高度函数C语言的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于树的高度怎么求、树的高度函数C语言的信息别忘了在本站进行查找喔 。

    推荐阅读