二叉树的链式存储有:二叉链表 三叉链表 双亲链表 线索链表
二叉链表容易找到孩子,但是不易找到双亲,三叉链表则弥补这一缺点
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#pragma warning (disable:4996)
#define OK 0
#define ERROR -1
typedef int status;
typedef int elemtype;
typedef struct bitnode {
elemtype data;
struct bitnode *lchild;
struct bitnode *rchild;
}bitnode,*bitree;
status initbitree(bitree *t) {
if (t == NULL)return ERROR;
*t = NULL;
return OK;
}
void destroybitree(bitree *t) { //递归调用清空树
if (t == NULL) return;
if (*t) {
if ((*t)->lchild) destroybitree(&(*t)->lchild);
if ((*t)->rchild) destroybitree(&(*t)->rchild);
free(*t);
*t = NULL;
}
}
void createbitree(bitree *t) {
elemtype e;
scanf("%d", &e);
if (e == 0)
*t = NULL;
else {
*t = (bitree)malloc(sizeof(bitnode));
if (!*t)return;
(*t)->data = e;
createbitree(&(*t)->lchild);
createbitree(&(*t)->rchild);
}
}
int bitreedpeth(bitree t) {
if (t == NULL) return 0;
int i, j;
i = j = 0;
if (t->lchild) i=bitreedpeth(t->lchild);
else i = 0;
if (t->rchild) i=bitreedpeth(t->rchild);
else j = 0;
return i > j ? i + 1 : j + 1;
}
int main() {
bitree bt;
initbitree(&bt);
createbitree(&bt);
int len = bitreedpeth(bt);
printf("%d\n", len);
destroybitree(&bt);
system("pause");
}