实现满二叉树的节点查找 书上的,竟然还错了,我给他改了

#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
	int date;
	struct node * left;
	struct node * right;
}node;

int n;

node * Creattree(struct node * tree,int i);
node * findtree(struct node * tree,int x);
int main()
{
	struct node * b_tree=NULL,*tp=NULL;
	printf("请输入层数!\n");
	scanf("%d",&n);
	b_tree=Creattree(b_tree,1);
	tp=findtree(b_tree,2);
	if(tp==NULL)
	printf("非常抱歉,没有找到\n");
	else
	printf("找到了%d\n",tp->date);
	return 0;
}

node * Creattree(struct node * tree,int i)  //我依然认为这种构造二叉树的思想需要时不时的来反思一下 
{
	if(tree == NULL){
		tree=(struct node *)malloc(sizeof(node));
		tree->left=NULL;
		tree->right=NULL;
	} 
	if(i==n){
		printf("到了叶子了,请输入一个数据\n");
		scanf("%d",&(tree->date));
		return tree;
	}
	else
	{
		printf("到了第%d个节点,请输入一个数据\n",i);
		scanf("%d",&(tree->date));
		tree->left=Creattree(tree->left,i+1);
		tree->right=Creattree(tree->right,i+1);
	}
	return tree;
}
node * findtree(struct node *tree,int x)
{
	node * tmp=NULL; 
	if(tree == NULL)
	return NULL;
	else if(x == tree->date)
	return tree;
	else
	{
		tmp=findtree(tree->left,x);
		if(tmp!=NULL)
		return tmp;
		tmp=findtree(tree->right,x);
		if(tmp!=NULL)
		return tmp;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值