作业1:二叉树的操作
1.二叉树递归创建
函数代码:
#include "head.h"
//创建新节点
Btree create_Node()
{
Btree s=(Btree)malloc(sizeof(struct Node));
if(s==NULL)
return NULL;
s->data='\0';//新节点数据域清0
s->lchild=s->rchild=0;//新节点两个指针域清0
return s;
}
//创建二叉树
Btree create_tree()
{
datatype element;
printf("please enter element:");
scanf(" %c",&element);//输入二叉树节点的数据元素
if('#'==element)//用'#'提示NULL,标志着递归出口
return NULL;
//创建新节点
Btree tree=create_Node();
tree->data=element;
//递归创建左孩子
puts("left");
tree->lchild=create_tree();
//递归创建右孩子
puts("right");
tree->rchild=create_tree();
return tree;
}
效果图:
2.二叉树先中后序遍历
函数代码:
//二叉树的前序遍历
void front(Btree tree)
{
if(NULL==tree)
return;
//前序遍历的顺序是根、左、右
//先遍历根节点
printf("%c",tree->data);
//再遍历左孩子
front(tree->lchild);
//最后遍历右孩子
front(tree->rchild);
}
//二叉树的中序遍历
void middle(Btree tree)
{
if(NULL==tree)
return;
//中序遍历顺序是左、根、右
//先遍历左孩子
middle(tree->lchild);
//再遍历根
printf("%c",tree->data);
//最后是右孩子
middle(tree->rchild);
}
//二叉树的后序遍历
void back(Btree tree)
{
if(NULL==tree)
return;
//后序遍历顺序是左右根
//先遍历左孩子
back(tree->lchild);
//再遍历右孩子
back(tree->rchild);
//最后遍历根
printf("%c",tree->data);
}
效果图:
3.二叉树计算节点
函数代码:
//二叉树计算节点个数
void Count(Btree tree,int *n0,int *n1,int *n2)
{
if(NULL==tree)
return;
if(tree->lchild==NULL && tree->rchild==NULL)//计算度为0的节点个数
{
++*n0;
}
else if(tree->lchild!=NULL && tree->rchild!=NULL)//计算度为2的节点个数
{
++*n2;
}
else//计算度为1的节点个数
{
++*n1;
}
//遍历左孩子,计算各个度节点个数
Count(tree->lchild,n0,n1,n2);
//遍历右孩子,计算各个度节点个数
Count(tree->rchild,n0,n1,n2);
}
效果图:
4.二叉树计算深度。
函数代码:
//计算二叉树深度
int high(Btree tree)
{
if(NULL==tree)
return 0;
//计算二叉树左子树深度
int left_high=1+high(tree->lchild);
//计算二叉树右子树深度
int right_high=1+high(tree->rchild);
return left_high>right_high?left_high:right_high;
}
效果图:
总函数代码:
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 len=high(tree);
printf("high=%d\n",len);
return 0;
}
test.c:
#include "head.h"
//创建新节点
Btree create_Node()
{
Btree s=(Btree)malloc(sizeof(struct Node));
if(s==NULL)
return NULL;
s->data='\0';//新节点数据域清0
s->lchild=s->rchild=0;//新节点两个指针域清0
return s;
}
//创建二叉树
Btree create_tree()
{
datatype element;
printf("please enter element:");
scanf(" %c",&element);//输入二叉树节点的数据元素
if('#'==element)//用'#'提示NULL,标志着递归出口
return NULL;
//创建新节点
Btree tree=create_Node();
tree->data=element;
//递归创建左孩子
puts("left");
tree->lchild=create_tree();
//递归创建右孩子
puts("right");
tree->rchild=create_tree();
return tree;
}
//二叉树的前序遍历
void front(Btree tree)
{
if(NULL==tree)
return;
//前序遍历的顺序是根、左、右
//先遍历根节点
printf("%c",tree->data);
//再遍历左孩子
front(tree->lchild);
//最后遍历右孩子
front(tree->rchild);
}
//二叉树的中序遍历
void middle(Btree tree)
{
if(NULL==tree)
return;
//中序遍历顺序是左、根、右
//先遍历左孩子
middle(tree->lchild);
//再遍历根
printf("%c",tree->data);
//最后是右孩子
middle(tree->rchild);
}
//二叉树的后序遍历
void back(Btree tree)
{
if(NULL==tree)
return;
//后序遍历顺序是左右根
//先遍历左孩子
back(tree->lchild);
//再遍历右孩子
back(tree->rchild);
//最后遍历根
printf("%c",tree->data);
}
//二叉树计算节点个数
void Count(Btree tree,int *n0,int *n1,int *n2)
{
if(NULL==tree)
return;
if(tree->lchild==NULL && tree->rchild==NULL)//计算度为0的节点个数
{
++*n0;
}
else if(tree->lchild!=NULL && tree->rchild!=NULL)//计算度为2的节点个数
{
++*n2;
}
else//计算度为1的节点个数
{
++*n1;
}
//遍历左孩子,计算各个度节点个数
Count(tree->lchild,n0,n1,n2);
//遍历右孩子,计算各个度节点个数
Count(tree->rchild,n0,n1,n2);
}
//计算二叉树深度
int high(Btree tree)
{
if(NULL==tree)
return 0;
//计算二叉树左子树深度
int left_high=1+high(tree->lchild);
//计算二叉树右子树深度
int right_high=1+high(tree->rchild);
return left_high>right_high?left_high:right_high;
}
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 high(Btree tree);
#endif
作业2:编程实现快速排序降序
函数代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/*
* function: 确定一个基准值
* @param [ in]
* @param [out] arr,low,high
* @return 返回基准值下标
*/
int one_sort(int *p,int low,int high)
{
//确定基准值
int key=*(p+low);
//当low==high结束
while(low<high)
{
//从high开始比较
while(low<high && key>=*(p+high))
{
high--;
}
*(p+low)=*(p+high);
//从low开始比较
while(low<high && key<=*(p+low))
{
low++;
}
*(p+high)=*(p+low);
}
*(p+low)=key;
return low;
}
/*
* function: 快排
* @param [ in]
* @param [out] arr low high
* @return 无
*/
void quick_sort(int *p,int low,int high)
{
//没有元素或只有一个元素
if(low>=high)
return;
//一次排序
int mid=one_sort(p,low,high);
//递归左子序列
quick_sort(p,low,high-1);
//递归右子序列
quick_sort(p,mid+1,high);
}
int main(int argc, const char *argv[])
{
int arr[]={1,15,54,48,65,2,7};
int len=sizeof(arr)/sizeof(arr[0]);
quick_sort(arr,0,len-1);
for(int i=0;i<len;i++)
{
printf("%-4d",arr[i]);
}
puts("");
return 0;
}
效果图: