二叉树链表的创建,以及先序中序后序遍历
功能函数
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "./func.h"
head* create_tree()
{
int data=0;
scanf("%d",&data);
if(-1==data)
{
return NULL;
}
head* roots=(head*)malloc(sizeof(head)
if(NULL==roots)
{
printf("创建根失败");
return NULL;
}
roots->data=data;
roots->Ltree=create_tree();
roots->Rtree=create_tree();
return roots;
}
//先序遍历
void pre_order(head* roots)
{
if(NULL==roots)
{
return;
}
printf("%d\t",roots->data);
pre_order(roots->Ltree);
pre_order(roots->Rtree);
}
//中序遍历
void mid_order(head* roots)
{
if(NULL==roots)
{
return;
}
mid_order(roots->Ltree);
printf("%d\t",roots->data);
mid_order(roots->Rtree);
}
//后序遍历
void after_order(head* roots)
{
if(NULL==roots)
{
return;
}
//左右根
after_order(roots->Ltree);
after_order(roots->Rtree);
printf("%d\t",roots->data);
}
#ifndef __FUNC_H
#define __FUNC_H
typedef int typedata;
typedef struct tree
{
typedata data;
struct tree* Ltree;
struct tree* Rtree;
}head;
head* create_tree();
void pre_order(head* roots);
void mid_order(head* roots);
void after_order(head* roots);
#endif
结果