2011华中科技大学研究生复试机试之三

1 (1)无冗余的接受键盘输入的n个字符串,并将其无冗余的放入对应的字符数组中,再按照每行一串的格式输出这些字符串。
(2)以单个字符串为数据域,按字典顺序将n个字符串生成为一棵二叉搜索树,并且规定左子树(数据域)小于右子树(数据域)。

(3)先序遍历该二叉树,并输出结果。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct node{
	char* data;
	struct node *lchild,*rchild;
}BNode,*BTree;

void insert(BTree* t,char* data)
{
	if(!(*t))
	{
		(*t)=(BTree)malloc(sizeof(BNode));
		(*t)->data=data;
		(*t)->lchild=NULL;
		(*t)->rchild=NULL;
	}
	else
	{
		if(strcmp(data,((*t)->data))<0)	insert(&(*t)->lchild,data);
		else				insert(&(*t)->rchild,data);
	}
}

void PreOrder(BTree t)
{
	if(t)
	{
		printf("%s\n",t->data);
		PreOrder(t->lchild);
		PreOrder(t->rchild);
	}
}

int main()
{
	int i;
	
	int n;
	scanf("%d",&n);

	char** a=(char**)malloc(n*sizeof(char*));
	if(a==NULL)
	{
		puts("malloc failed");
		return 0;
	}
	
	for(i=0;i<n;i++)
	{
		int count=0;
		char c;
		a[i]=(char*)malloc(sizeof(char));
		a[i][count++]=getchar();
		while((c=getchar())!='\n')
		{
            a[i]=(char*)realloc(a[i],count*(sizeof(char)));
			a[i][count++]=c;
		}
		a[i][count]='\0';
	}
	
	BTree t = NULL;
	for(i=0;i<n;i++)
                    insert(&t,a[i]);
		
	PreOrder(t);
	system("pause");
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值