天天看點

二叉樹鍊式存儲-二叉連結清單

二叉樹的鍊式存儲有:二叉連結清單   三叉連結清單    雙親連結清單    線索連結清單 

二叉連結清單容易找到孩子,但是不易找到雙親,三叉連結清單則彌補這一缺點

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

繼續閱讀