前言
这就是放鸽子的感觉嘛......
前面几天休息了一下,今天回到正事上来。上一篇博客讲到了二叉树,递归与快速排序的相关理论。今天讲的是二叉树与快速排序的代码实现。另外讲一下插入排序的实现。
依旧是之前的多文件编译的格式:Makefile, fun.c, head.h, main.c。
概述
1.二叉树的代码实现
2.快速排序的代码实现
3.插入排序的代码实现
1.二叉树的代码实现
1.1.Makefile
EXE=01_tree
Objs=$(patsubst %.c,%.o,$(wildcard *.c))
CC=gcc
CFlags=-c -o
all:$(EXE)
$(EXE):$(Objs)
$(CC) $^ -o $@
%.o:%.c
$(CC) $^ $(CFlags) $@
#伪目标
.PHONY:clean
clean:
rm $(EXE) $(Objs)
另外,对于Objs=$(patsubst %.c,%.o,$(wildcard *.c)),如果理解得不好,可以用:
Objs+=main.o
Objs+=fun.o
1.2.head.h
#ifndef _HEAD_H_
#define _HEAD_H_
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct node_tree{
char data;
struct node_tree* lchild;
struct node_tree* rchild;
}node_tree,*tree_p;
//fun
tree_p create_node(char data);
tree_p create_tree();
void pre_show(tree_p T);
void mid_show(tree_p T);
void rear_show(tree_p T);
#endif
二叉树的每一个结点都是一个结构体:里面除开存储自身的数据外,还需要存储左右子树的地址,以便能够与左右子树建立联系。
1.3.fun.c
#include "tree.h"
//创建结点:注意树的结点创建必须也要返回地址
tree_p create_node(char data){
tree_p T = (tree_p)malloc(sizeof(node_tree));
if(T == NULL)return NULL;
T->data = data;
T->lchild = NULL;
T->rchild = NULL;
return T;
}
//创建树:自身结点加左右孩子(子树)结点
tree_p create_tree(){
char data;
scanf(" %c",&data);
if(data == '#')return NULL;
tree_p T = create_node(data);
T->lchild = create_tree();
T->rchild = create_tree();
return T;
}
//前序遍历
void pre_show(tree_p T){
if(T == NULL)return;
printf("%c",T->data);
pre_show(T->lchild);
pre_show(T->rchild);
return;
}
//中序遍历
void mid_show(tree_p T){
if(T == NULL)return;
mid_show(T->lchild);
printf("%c",T->data);
mid_show(T->rchild);
return;
}
//后序遍历
void rear_show(tree_p T){
if(T == NULL)return;
rear_show(T->lchild);
rear_show(T->rchild);
printf("%c",T->data);
return;
}
具体的理论在上一篇博客中有讲到,这里直接贴图:

二叉树的创建与遍历都需要用到递归,因为二叉树的子树也是二叉树。
1.4.main.c
#include "tree.h"
int main(int argc, const char *argv[])
{
printf("请输入树:\n");
tree_p T = create_tree();
printf("前序遍历:");
pre_show(T);
printf("\n");
printf("中序遍历:");
mid_show(T);
printf("\n");
printf("后序遍历:");
rear_show(T);
printf("\n");
return 0;
}
1.5.代码执行:

ubuntu@ubuntu:~/data_structre/review/tree$ make
gcc main.c -c -o main.o
gcc main.o fun.o -o 01_tree
ubuntu@ubuntu:~/data_structre/review/tree$ ./01_tree
请输入树:
A
C
E
#
#
#
D
B
#
#
#
前序遍历:ACEDB
中序遍历:ECABD
后序遍历:ECBDA
通过代码实现的遍历结果与通过逻辑推导出的一致,说明代码逻辑是正确的。
2.快速排序的代码实现
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//快速排序代码实现的总体思路是先实现换位、一次快速排序
//再通过递归实现多次排序(当然递归的本质是循环,
//只是快排规模较大,因此用递归比较合适。)
void swap(int* a, int* b){
int temp = *a;*a = *b; *b = temp;
}
int one_sort(int* arr, int low, int high){
int base = arr[low], left = low+1, right = high; //确定基准及左右指示位
//碰头法
while(left<=right){ //这一行的意思是只要没碰头
//只要符合基准判定的条件(左小右大),不管。
while(left<=right && arr[left]<=base)left++;
while(left<=right && arr[right]>=base)right--;
//如果没碰头,说明右边有大过基准的值,左边有小过基准的值
//将二者互换:
if(left<right)swap(&arr[left],&arr[right]);
}
swap(&arr[low], &arr[right]);//将基准放入碰头点
//注意一定写arr[low],不能写base:因为base是局部变量,传不出去
return right;//返回碰头点
}
void quick_sort(int* arr, int low, int high){
if(low>=high)return;
int mid = one_sort(arr,low,high);
quick_sort(arr, low, mid-1);
quick_sort(arr, mid+1, high);
}
int main(int argc, const char *argv[])
{
int arr[]={5,9,1,8,2,7,3,6,4,10};
int len = sizeof(arr)/sizeof(int);
quick_sort(arr, 0, len-1);
for(int i=0; i<=len-1; ++i){
printf("%d ",arr[i]);
}
printf("\n");
return 0;
}
运行结果:
ubuntu@ubuntu:~/data_structre/review/others$ ./quick_sort
1 2 3 4 5 6 7 8 9 10
3.插入排序的代码实现
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void insert_sort(int* arr, int len){
for(int i=1; i<=len-1; ++i){
//注意,i是循环的局部变量,后面不能写arr[i],因此用temp保留arr[i]
int temp = arr[i],j=0;
for(j=i-1; j>=0 && temp<arr[j]; j--)arr[j+1] = arr[j];
arr[j+1] = temp;
}
}
int main(int argc, const char *argv[])
{
int arr[]={5,9,1,8,2,7,3,6,4,10};
int len = sizeof(arr)/sizeof(int);
insert_sort(arr,len);
for(int i=0; i<=len-1; ++i){
printf("%d ",arr[i]);
}
printf("\n");
return 0;
}
运行结果:
ubuntu@ubuntu:~/data_structre/review/others$ ./insert_sort
1 2 3 4 5 6 7 8 9 10
结语
二叉树与递归虽然逻辑上并不复杂,但实现起来非常的繁琐,十分需要耐心,也十分考验编程人员对于编程的热情。

被折叠的 条评论
为什么被折叠?



