二叉树的实现-C语言

创建 | 销毁 | 遍历 | 树的高度 | 叶子节点数 | 打印叶子节点

目录

整体代码

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#include<assert.h>

typedef struct treeNode {
	int data;
	struct treeNode* lchild;
	struct treeNode* rchild;
}Tnode,Tree;

//创建二叉树
void binarytree_create(Tree** root) {
	int a = 0;
	printf("输入接节点数值 (当输入100,当前节点创建完成)\n");     
	scanf_s("%d", &a);
	if (a == 100) {
		*root = NULL;
	}
	else {
		*root = (Tnode*)malloc(sizeof(Tnode));
		if (*root == NULL) {
			return;
		}
		(*root)->data = a;
		printf("create %d 的左孩子", a);
		binarytree_create(&(*root)->lchild);
		printf("create %d 的右孩子", a);
		binarytree_create(&(*root)->rchild);
	}
}

//销毁二叉树
void binarytree_destory(Tree* root) {
	if (root == NULL) {
		return;
	}
	binarytree_destory(root->lchild);
	binarytree_destory(root->rchild);
	free(root);
}

//先序遍历 根->左->右
void binarytree_preorder(Tree* root) {
	if (root == NULL) {
		return;
	}
	printf(" %d ", root->data);
	binarytree_preorder(root->lchild);
	binarytree_preorder(root->rchild);
	return;
}

//中序遍历 左->根->右
void binarytree_inorder(Tree* root) {
	if (root == NULL) {
		return;
	}
	binarytree_inorder(root->lchild);
	printf(" %d ", root->data);
	binarytree_inorder(root->rchild);
	return;
}

//后序遍历 左->右->根
void binarytree_postorder(Tree* root) {
	if (root == NULL) {
		return;
	}
	binarytree_postorder(root->lchild);
	binarytree_postorder(root->rchild);
	printf(" %d ", root->data);
	return;
}

//广度  -- 需要用到队列
/*创建队,入队,出队*/
/*
void binarytree_levelorder(Tree* root){
	list_queue* queue = NULL;
	Tnode* node = NULL;
	if(root == NULL){
	return;
	}
	queue = list_queue_create();
	//根节点先入队
	list_queue_enqueue(queue, root);
	while(!list_queue_is_empty(queue)){
		list_queue_dequeue(queue,&node);
		if(node->lchild != NULL){
			list_queue_enqueue(queue,node->lchild);
		}
		if(node->rchild != NULL){
			list_queue_enqueue(queue,node->rchild);
		}
	}
	free(queue);
}
*/

//打印叶子节点
void binarytree_printfleaf(Tree* root) {
	if (root == NULL) {
		return;
	}
	if ((root->lchild == NULL) && (root->rchild == NULL)) {
		printf(" %d ", root->data);
	}
	else {
		binarytree_printfleaf(root->lchild);
		binarytree_printfleaf(root->rchild);
	}
}

//打印叶子节点的个数
int binarytree_getleafnum(Tree* root) {
	if (root == NULL) {
		return 0;
	}
	if (root->lchild == NULL && root->rchild == NULL) {
		return 1;
	}
	return binarytree_getleafnum(root->lchild) + binarytree_getleafnum(root->rchild);
}

//打印树的高度
int binarytree_gethigh(Tree* root) {
	int lhigh = 0;
	int rhigh = 0;
	if (root == NULL) {
		return 0;
	}
	lhigh = binarytree_gethigh(root->lchild);
	rhigh = binarytree_gethigh(root->rchild);

	return ((lhigh > rhigh) ? (lhigh + 1) : (rhigh + 1));
}

int main() {
	Tree* root = NULL;

	binarytree_create(&root);
	binarytree_preorder(root);
	binarytree_destory(root);

	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值