回复 5楼 会分手的空气
我需要打开一个文件,然后将文件构造成以下的二叉树然后进行操作
这是要求:题目.查找算法实现与性能分析
功能要求:根据菜单选择相应算法,分别实现顺序查找、折半查找、二叉排序树(建立、删除、查找)算法。存储结构自己选择。
输入:data.txt(文件名,从文件读取待查找数据序列)
待查找的键值
输出:各种算法的查找结果。
(1)查找结果(查找到的位置或者找不到)
(2)查找次数
我已经把前面的顺序和折半查找都做完了,但是二叉树那个不会啊!
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#define
INFMT
"%d"
#define
OUTFMT "%d "
/* #define
NULL 0L */
#define
BOOL int
#define
TRUE 1
#define
FALSE 0
#define
LEN
10000
typedef int ElemType;
typedef struct BSTNode
{
ElemType data;
struct BSTNode *lchild, *rchild;
} BSTNode, *BSTree;
/* 插入新节点 */
void Insert(BSTree *tree, ElemType item)
{
BSTree node = (BSTree)malloc(sizeof(BSTNode));
node->data = item;
node->lchild = node->rchild = NULL;
if (!*tree)
*tree = node;
else
{
BSTree cursor = *tree;
while (1)
{
if (item < cursor->data)
{
if (NULL == cursor->lchild)
{
cursor->lchild = node;
break;
}
cursor = cursor->lchild;
}
else
{
if (NULL == cursor->rchild)
{
cursor->rchild = node;
break;
}
cursor = cursor->rchild;
}
}
}
return;
}
/* 查找指定值 */
BSTree Search(BSTree tree, ElemType item)
{
BSTree cursor = tree;
while (cursor)
{
if (item == cursor->data)
return cursor;
else if ( item < cursor->data)
cursor = cursor->lchild;
else
cursor = cursor->rchild;
}
return NULL;
}
/* 中缀遍历 */
void Inorder(BSTree tree)
{
BSTree cursor = tree;
if (cursor)
{
Inorder(cursor->lchild);
printf(OUTFMT, cursor->data);
Inorder(cursor->rchild);
}
}
/* 回收资源 */
void Cleanup(BSTree tree)
{
BSTree cursor = tree, temp = NULL;
if (cursor)
{
Cleanup(cursor->lchild);
Cleanup(cursor->rchild);
free(cursor);
}
}
/* 产生一组随机数 */
void randnum(int *a, int s)
{
int i, j, mod = s * 10;
srand(time(NULL));
for (i = 0; i < s; ++i)
{
a[i] = rand() % mod + 1;
for (j = 0; j < i; ++j)
{
if (a[i] == a[j])
{
a[i] = rand() % mod + 1;
j = -1;
continue;
}
}
}
}
int main()
{
int i;
//char ch [100] = {0};
char find;
long num = 0;
nihao:
printf ( "请输入文件所在目录及文件名:\n");
gets (ch); //scanf ("%s",ch);
FILE *fp;
fp = fopen ("a.txt","r");
if (!fp)
{
printf
( "打开文件%s失败!");
printf
( "打开文件%s失败!\n\n请重新输入:\n",ch);
goto nihao ;
}
int x1 = 1;
SeqList R;
while ( !feof ( fp ) )
{
fscanf (fp,"%c", &R[x1].key);
x1++;
}
num = x1 - 2;//num用来统计文件中的字符个数
//我的问题就是这一块怎么将他们串联起来!
ElemType item;
char choice;
BSTree root = NULL, ret; /* 必须赋予NULL值,否则出错 */
BOOL finish = FALSE;
printf("***欢迎使用二叉排序树演示程序***\n\n");
printf("请选择创建树的方式:\n");
printf("1. 手动输入数据创建二叉排序树\n");
printf("2. 自动产生数据创建二叉排序树\n");
do
{
scanf("%c", &choice);
getchar();
if (choice == '1' || choice == '2')
finish = TRUE;
} while (FALSE == finish);
switch (choice)
{
case '1':
{
printf("请输入数据(-10000结束):\n");
while (1)
{
scanf(INFMT, &item);
if (-10000 != item)
Insert(&root, item);
else
break;
}
break;
}
case '2':
{
int ia[LEN], i = 0, loop = LEN;
randnum(ia, LEN);
while (loop--)
{
Insert(&root, ia[i++]);
}
break;
}
}
printf("\n\n创建完成...\n");
Inorder(root);
printf("\n\n");
/* 二叉排序树的查找测试 */
do
{
printf("\n请输入查找数据:");
scanf("%d", &item);
getchar();
printf("Searching...\n");
ret = Search(root, item);
if (NULL == ret)
printf("查找失败!");
else
printf("查找成功!");
printf("\n继续测试按y,退出按其它键。\n");
choice = getchar();
} while (choice=='y'||choice=='Y');
Cleanup(root);
fclose(fp);
return 0 ;
}