二叉树链式存储-二叉链表

二叉树的链式存储有:二叉链表   三叉链表    双亲链表    线索链表 

二叉链表容易找到孩子,但是不易找到双亲,三叉链表则弥补这一缺点

#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");
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值