#include "./01_heah.h"
#include <stdio.h>
#include <stdlib.h>
//创建节点
Btree* create_Btree()
{
int value;
scanf("%d",&value);
//判断是否结束递归
if(value==-1)
{
return NULL;
}
//创建
Btree*head=(Btree*)malloc(sizeof(Btree));
if(head==NULL)
{
printf("创建失败\n");
return NULL;
}
head->data=value;
head->lchild=create_Btree();
//左孩子结束,到右孩子
head->rchild=create_Btree();
return head;
}
//遍历-先序
Btree* show(Btree *head)
{
//判断是否结束递归
if(head==NULL)
{
return NULL;
}
printf("%4d",head->data);
head->lchild=show(head->lchild);
head->rchild=show(head->rchild);
}
//遍历-中序
Btree*show_z(Btree*head)
{
if(head==NULL)
{
return NULL;
}
head->lchild=show_z(head->lchild);
printf("%4d",head->data);
head->rchild=show_z(head->rchild);
}
//遍历-后序
Btree*show_w(Btree*head)
{
if(head==NULL)
{
return NULL;
}
head->lchild=show(head->lchild);
head->rchild=show(head->rchild);
printf("%4d",head->data);
}
数据结构第五天作业
最新推荐文章于 2024-07-17 12:13:29 发布