寒假作业:2024/2/7

作业1:编程实现二叉树的操作

1.1二叉树的创建

函数代码:

/*
 * function:    创建新节点
 * @param [ in] 
 * @param [out] 
 * @return      节点
 */
Btree create_Node()
{
	Btree s=(Btree)malloc(sizeof(struct Node));
	if(NULL==s)
		return NULL;
	s->data='\0';
	s->lchild=s->rchild=NULL;
	return s;
}
/*
 * function:    创建二叉树
 * @param [ in] 
 * @param [out] 
 * @return      二叉树
 */
Btree create_tree()
{
	//输入数据域的值
	datatype element;
	printf("please enter element:");
	scanf(" %c",&element);
	//用'#'表示空的孩子节点
	if('#'==element)
		return NULL;
	//创建新节点
	Btree tree=create_Node();
	tree->data=element;
	//递归创建节点的左孩子
	tree->lchild=create_tree();
	//递归创建节点的右孩子
	tree->rchild=create_tree();
	return tree;
}

1.2二叉树的先序遍历

函数代码:

/*
 * function:    二叉树的前序遍历
 * @param [ in] 
 * @param [out] 二叉树
 * @return      无
 */
void front(Btree tree)
{
	if(NULL==tree)
		return;
	//按照根左右的顺序开始遍历
	printf("%c",tree->data);
	//左子树
	front(tree->lchild);
	//右子树
	front(tree->rchild);
}

1.3二叉树的中序遍历

函数代码:

/*
 * function:    二叉树的中序遍历
 * @param [ in] 
 * @param [out] 二叉树
 * @return      无
 */
void middle(Btree tree)
{
	if(NULL==tree)
		return;
	//按照左根右的顺序开始遍历
	//左子树
	middle(tree->lchild);
	//根
	printf("%c",tree->data);
	//右子树
	middle(tree->rchild);
}

1.4二叉树的后序遍历

函数代码:

/*
 * function:    二叉树的后序遍历
 * @param [ in] 
 * @param [out] 二叉树
 * @return      无
 */
void back(Btree tree)
{
	if(NULL==tree)
		return;
	//按照左右根的顺序开始遍历
	//左子树
	back(tree->lchild);
	//右子树
	back(tree->rchild);
	//根
	printf("%c",tree->data);
}

1.5二叉树各个节点度的个数

函数代码:

/*
 * function:    计算各个度节点个数
 * @param [ in] 
 * @param [out] 二叉树,度节点
 * @return      无
 */
void Count(Btree tree,int *n0,int *n1,int *n2)
{
	if(NULL==tree)
		return;
	if(NULL==tree->lchild && NULL==tree->rchild){
		++*n0;
	}else if(NULL!=tree->lchild && NULL!=tree->rchild){
		++*n2;
	}else{
		++*n1;
	}
	//左子树
	Count(tree->lchild,n0,n1,n2);
	//右子树
	Count(tree->rchild,n0,n1,n2);
}

1.6二叉树的深度

函数代码:

/*
 * function:    计算二叉树深度
 * @param [ in] 
 * @param [out] 二叉树
 * @return      深度
 */
int Deep(Btree tree)
{
	if(NULL==tree)
		return 0;
	//计算左子树深度
	int left_deep=1+Deep(tree->lchild);
	//计算右子树深度
	int right_deep=1+Deep(tree->rchild);
	//返回较大的深度
	return left_deep>right_deep?left_deep:right_deep;

}

效果图:

总代码:

head.h:
#ifndef __HEAD_H__
#define __HEAD_H__
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//树节点结构体的创建
typedef char datatype;
typedef struct Node
{
	//数据域
	datatype data;
	//左子树指针域
	struct Node *lchild;
	//右子树指针域
	struct Node *rchild;
}*Btree;
//函数声明
Btree create_Node();
Btree create_tree();
void front(Btree tree);
void middle(Btree tree);
void back(Btree tree);
void Count(Btree tree,int *n0,int *n1,int *n2);
int Deep(Btree tree);

#endif
main.c:
#include "head.h"

int main(int argc, const char *argv[])
{
	//定义一个二叉树
	Btree tree=create_tree();
	//二叉树的前序遍历
	front(tree);
	puts("");
	//二叉树的中序遍历
	middle(tree);
	puts("");
	//二叉树的后序遍历
	back(tree);
	puts("");
	//二叉树各个度节点个数
	int n0=0,n1=0,n2=0;
	Count(tree,&n0,&n1,&n2);
	printf("n0=%d n1=%d n2=%d n=%d\n",n0,n1,n2,n0+n1+n2);
	//二叉树深度
	int deep=Deep(tree);
	printf("deep=%d\n",deep);
	return 0;
}
test.c:
#include "head.h"
/*
 * function:    创建新节点
 * @param [ in] 
 * @param [out] 
 * @return      节点
 */
Btree create_Node()
{
	Btree s=(Btree)malloc(sizeof(struct Node));
	if(NULL==s)
		return NULL;
	s->data='\0';
	s->lchild=s->rchild=NULL;
	return s;
}
/*
 * function:    创建二叉树
 * @param [ in] 
 * @param [out] 
 * @return      二叉树
 */
Btree create_tree()
{
	//输入数据域的值
	datatype element;
	printf("please enter element:");
	scanf(" %c",&element);
	//用'#'表示空的孩子节点
	if('#'==element)
		return NULL;
	//创建新节点
	Btree tree=create_Node();
	tree->data=element;
	//递归创建节点的左孩子
	tree->lchild=create_tree();
	//递归创建节点的右孩子
	tree->rchild=create_tree();
	return tree;
}
/*
 * function:    二叉树的前序遍历
 * @param [ in] 
 * @param [out] 二叉树
 * @return      无
 */
void front(Btree tree)
{
	if(NULL==tree)
		return;
	//按照根左右的顺序开始遍历
	printf("%c",tree->data);
	//左子树
	front(tree->lchild);
	//右子树
	front(tree->rchild);
}
/*
 * function:    二叉树的中序遍历
 * @param [ in] 
 * @param [out] 二叉树
 * @return      无
 */
void middle(Btree tree)
{
	if(NULL==tree)
		return;
	//按照左根右的顺序开始遍历
	//左子树
	middle(tree->lchild);
	//根
	printf("%c",tree->data);
	//右子树
	middle(tree->rchild);
}
/*
 * function:    二叉树的后序遍历
 * @param [ in] 
 * @param [out] 二叉树
 * @return      无
 */
void back(Btree tree)
{
	if(NULL==tree)
		return;
	//按照左右根的顺序开始遍历
	//左子树
	back(tree->lchild);
	//右子树
	back(tree->rchild);
	//根
	printf("%c",tree->data);
}
/*
 * function:    计算各个度节点个数
 * @param [ in] 
 * @param [out] 二叉树,度节点
 * @return      无
 */
void Count(Btree tree,int *n0,int *n1,int *n2)
{
	if(NULL==tree)
		return;
	if(NULL==tree->lchild && NULL==tree->rchild){
		++*n0;
	}else if(NULL!=tree->lchild && NULL!=tree->rchild){
		++*n2;
	}else{
		++*n1;
	}
	//左子树
	Count(tree->lchild,n0,n1,n2);
	//右子树
	Count(tree->rchild,n0,n1,n2);
}
/*
 * function:    计算二叉树深度
 * @param [ in] 
 * @param [out] 二叉树
 * @return      深度
 */
int Deep(Btree tree)
{
	if(NULL==tree)
		return 0;
	//计算左子树深度
	int left_deep=1+Deep(tree->lchild);
	//计算右子树深度
	int right_deep=1+Deep(tree->rchild);
	//返回较大的深度
	return left_deep>right_deep?left_deep:right_deep;

}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值