作业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;
}