二叉树示意图
# 二叉树的创建
二叉树具有递归特性,所以利用递归的方式实现二叉树的创建。(使用链式存储结构)
/*利用先序遍历的结构进行递归*/
linktree tree_creart(){
data_s ch;
linktree T;
scanf("%c",&ch);
if(ch == '#'){ //输入‘#’代表当前节点为空,即说明某个分支没有
return NULL;
}
T = (linktree)malloc(sizeof(tree));
if(T == NULL){ //递归终止条件,最后一个节点没有分支
printf("T malloc is failed\n");
return NULL;
}
/*根节点,左子树,右子树的顺序递归输入*/
T->data = ch;
T->lchild = tree_creart();
T->rchild = tree_creart();
return T;
}
二叉树的遍历方法
遍历利用递归的方法,遍历二叉树;
先序遍历
先访问根节点,再访问左子树,后访问右子树;
示例二叉树遍历结果应为:A B C D E F G H K L;
int tree_show_first(linktree T){
if(T == NULL){
return ;
}
printf("%c",T->data);
tree_show_first(T->lchild);
tree_show_first(T->rchild);
}
中序遍历
先访问左子树,再访问根节点,后访问右子树;
示例二叉树遍历结果应为:C B E D A F K H L G;
int tree_show_middle(linktree T){
if(T == NULL){
return;
}
tree_show_middle(T->lchild);
printf("%c",T->data);
tree_show_middle(T->rchild);
}
后序遍历
先访问左子树,再访问右子树,后访问根节点;
示例二叉树遍历结果应为:C E D B K L H G F A;
int tree_show_back(linktree T){
if(T == NULL){
return;
}
tree_show_back(T->lchild);
tree_show_back(T->rchild);
printf("%c",T->data);
}
层遍历
依次遍历每层的兄弟节点;
示例二叉树遍历结果应为:A B F C D G E H K L;
将每层节点存入队列中,然后在进行出队的方式遍历每层;
int tree_show_layer(linktree T){
linkqueue Q;
Q = queue_creart();
if(T == NULL){
return;
}
printf("%c",T->data);
puts("");
queue_push(Q,T);
while(Q->front != Q->reat){
T = queue_pop(Q);
if(T->lchild){
printf("%c",T->lchild->data);
queue_push(Q,T->lchild);
}
if(T->rchild){
printf("%c",T->rchild->data);
queue_push(Q,T->rchild);
}
puts("");
}
queue_free(Q);
}
实现代码展示
二叉树
tree.h
#ifndef _TREE_H_
#define _TREE_H_
typedef char data_s;
typedef struct node_t{
data_s data;
struct node_t *lchild, *rchild;
}tree,*linktree;
linktree tree_creart();
//int tree_insert(linktree T,data_t data);
int tree_show_first(linktree T);
int tree_show_middle(linktree T);
int tree_show_back(linktree T);
int tree_show_layer(linktree T);
linktree tree_free(linktree T);
#endif
tree.c
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include"linkqueue.h"
linktree tree_creart(){ //二叉树创建
data_s ch;
linktree T;
scanf("%c",&ch);
if(ch == '#'){
return NULL;
}
T = (linktree)malloc(sizeof(tree));
if(T == NULL){
printf("T malloc is failed\n");
return NULL;
}
T->data = ch;
T->lchild = tree_creart();
T->rchild = tree_creart();
return T;
}
int tree_show_first(linktree T){ //二叉树先序遍历
if(T == NULL){
return ;
}
printf("%c",T->data);
tree_show_first(T->lchild);
tree_show_first(T->rchild);
}
int tree_show_middle(linktree T){ //二叉树中序遍历
if(T == NULL){
return;
}
tree_show_middle(T->lchild);
printf("%c",T->data);
tree_show_middle(T->rchild);
}
int tree_show_back(linktree T){ //二叉树后序遍历
if(T == NULL){
return;
}
tree_show_back(T->lchild);
tree_show_back(T->rchild);
printf("%c",T->data);
}
int tree_show_layer(linktree T){ //二叉树层遍历
linkqueue Q;
Q = queue_creart();
if(T == NULL){
return;
}
printf("%c",T->data);
puts("");
queue_push(Q,T);
while(Q->front != Q->reat){
T = queue_pop(Q);
if(T->lchild){
printf("%c",T->lchild->data);
queue_push(Q,T->lchild);
}
if(T->rchild){
printf("%c",T->rchild->data);
queue_push(Q,T->rchild);
}
puts("");
}
queue_free(Q);
}
linktree tree_free(linktree T){ //二叉树内存释放
linktree p,q;
p = T;
linkqueue Q;
Q = queue_creart();
if(T == NULL){
return;
}
printf("%c",T->data);
puts("");
queue_push(Q,T);
while(Q->front != Q->reat){
T = queue_pop(Q);
if(T->lchild){
// printf("%c",T->lchild->data);
queue_push(Q,T->lchild);
}
if(T->rchild){
// printf("%c",T->rchild->data);
queue_push(Q,T->rchild);
}
printf("%c is free",T->data);
free(T);
puts("");
}
return NULL;
}
队列
lqueue.h
#include"tree.h"
typedef linktree data_t;
typedef struct node{
data_t data;
struct node *next;
}node,*linknode;
typedef struct{
linknode front;
linknode reat;
}queue,*linkqueue;
linkqueue queue_creart();
int queue_push(linkqueue Q,data_t data);
data_t queue_pop(linkqueue Q);
linkqueue queue_free(linkqueue Q);
lqueue.c
#include"linkqueue.h"
#include"tree.h"
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
linkqueue queue_creart(){
linknode H;
linkqueue Q;
H = (linknode)malloc(sizeof(node));
if(H == NULL){
printf("H malloc is filed\n");
return NULL;
}
Q = (linkqueue)malloc(sizeof(queue));
if(Q == NULL){
printf("Q malloc is filed\n");
return NULL;
}
H->data = 0;
H->next = NULL;
Q->front = Q->reat = H;
return Q;
}
int queue_push(linkqueue Q,data_t data){
linknode H;
H = (linknode)malloc(sizeof(node));
if(H == NULL){
printf("H malloc is failed\n");
return 0;
}
H->data = data;
H->next = Q->reat->next;
Q->reat->next = H;
Q->reat = H;
return 0;
}
data_t queue_pop(linkqueue Q){
linknode p;
if(Q->front == Q->reat){
printf("queue is NULL\n");
return NULL;
}
p = Q->front;
Q->front = Q->front->next;
free(p);
return (Q->front->data);
}
linkqueue queue_free(linkqueue Q){
linknode p;
while(Q->front){
p = Q->front;
// printf("free %d\n",p->data);
Q->front = Q->front->next;
free(p);
p = NULL;
}
free(Q);
Q = NULL;
return NULL;
}
主函数
main.c
#include<stdio.h>
#include"tree.h"
int main(){
linktree T;
printf("input data:i\n");
T = tree_creart();
tree_show_first(T);
puts("");
tree_show_middle(T);
puts("");
tree_show_back(T);
puts("");
tree_show_layer(T);
puts("");
T = tree_free(T);
tree_show_first(T);
puts("");
return 0;
}
执行结果
input data:
ABC##DE###F#GHK##L###
ABCDEFGHKL
CBEDAFKHLG
CEDBKLHGFA
ABFCDGEHKL
代码有诸多不足之处,请各位大神指教;