数据结构12.7

#include "stdio.h"
#include "stdlib.h"
#include "./3_func.h"


int main(int argc, const char *argv[])
{
	tree* head=create();
	xian(head);
	printf("\n");
	zhong(head);
	printf("\n");
	hou(head);
	printf("\n");
	return 0;
}

#include "stdio.h"
#include "stdlib.h"
#include "./3_func.h"


tree* create()
{
	printf("请输入数据>>>");
	int sj;
	scanf("%d",&sj);
	if( 0 == sj)
	{
		return NULL;
	}

	tree *head=(tree*)malloc(sizeof(tree));
	if(NULL==head)
	{
		printf("创建结点失败\n");
		return NULL;
	}

	head->data=sj;
	head->left=create();

	head->right=create();
	return head;

}



//先序遍历
void xian(tree* head)
{

	if(NULL==head)
	{
		return ;
	}
	printf("%d ",head->data);
	xian(head->left);
	xian(head->right);

	return ;
}

//中序遍历
void zhong(tree* head)
{

	if(NULL==head)
	{
		return ;
	}
	zhong(head->left);
	printf("%d ",head->data);
	zhong(head->right);

	return ;
}

//后序遍历
void hou(tree* head)
{

	if(NULL==head)
	{
		return ;
	}
	hou(head->left);
	hou(head->right);
	printf("%d ",head->data);

	return ;
}



#ifndef __FUNC_H__
#define __FUNC_H__

typedef int DT;
typedef struct Tree
{
	DT data;
	struct Tree* left;
	struct Tree* right;
}tree;

void xian(tree* head);

tree* create();

void zhong(tree* head);

void hou(tree* head);
#endif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值