1、已知一个个数为12的数据元素序列为{Dec,Feb,Nov,Oct,June,Sept,Aug,Apr,May, July,Jan,Mar},要求:
(1)按各数据元素的顺序(字母大小顺序)构造一棵二叉排序数,并中序打印排序结果。
(2)查找数据”Sept”是否存在。
#include “stdio.h”
#include “stdlib.h”
#include “malloc.h”
#include “string.h”
#define maxsize 15
typedef char KeyType;
typedef struct
{
KeyType key[maxsize];
}DataType;
typedef struct node
{ DataType data;
struct node * leftchild;
struct node * rightchild;
}BiTreeNode;
//查找算法
int Search (BiTreeNode *root,DataType item)
{
BiTreeNode *p;
if (root!=NULL)
{
p=root;
while (p!=NULL)
{
if (strcmp(p->data.key,item.key)==0) return 1;/*查找成功*/
if (strcmp(item.key,p->data.key)>0) p=p->rightchild;
else p=p->leftchild;
}
}
return 0; /*查找失败*/
}
//插入算法
int Insert(BiTreeNode **root,DataType item)
{
BiTreeNode *current,*parent=NULL,*p;
current=*root;
while(current!=NULL)
{
if(strcmp(current->data.key,item.key)==0)return 0;/*调用比较字符串函数*/
parent=current;
if(strcmp(current->data.key,item.key)<0)
current=current->rightchild;
// else if(strcmp(current->data.key,item.key)==1)
else current=current->leftchild;
}
p=(BiTreeNode *)malloc(sizeof(BiTreeNode));
if(p==NULL)
{
printf(“空间不够”);
exit(1);
}
p->data=item;
p->leftchild=NULL;
p->rightchild=NULL;
if(parent==NULL)
*root=p;
else if(strcmp(item.key,parent->data.key)<0)
parent->rightchild=p;
else
parent->rightchild=p;
return 1;
}
//中序遍历显示二叉排序树结点信息函数
void InTraverse (BiTreeNode *root) /*该函数使用递归调用*/
{
if (root==NULL) return;
if (root->leftchild!=NULL)
InTraverse (root->leftchild);
printf (“%s “,root->data.key);
if (root->rightchild!=NULL)
InTraverse (root->rightchild);
}
//主函数
void main (void)
{
DataType test[] = {“Dec”,”Feb”,”Nov”,”Oct”,”June”,”Sept”,”Aug”,”Apr”,”May”,”July”,”Jan”,”Mar”}, x = {“Sept”};
int n=12,i,s;
BiTreeNode *root =NULL;
for (i=0;i<n;i++)
{
Insert (&root,test[i]);
}
InTraverse (root);/*调用中序遍历函数*/
s=Search(root,x);
if (s==1)
printf (“/n数据元数%s存在”,x.key);
else
printf (“/n数据元数不存在!”);
}
以上是我自己写的代码,但是运行结果老是错误的。
以下是老师给的代码,好像跟我的没什么不同啊。
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include”string.h”
#define Max 10
typedef char KeyType;
typedef struct
{
KeyType key[Max];
}DataType;
typedef struct node
{
DataType data;
struct node *leftChild;
struct node *rightChild;
} BiTreeNode;
int Search(BiTreeNode *root, DataType item)
{
BiTreeNode *p;
if(root != NULL)
{
p = root;
while(p != NULL)
{
if(strcmp(p->data.key , item.key)==0) return 1;
if(strcmp(item.key , p->data.key)>0) p = p->rightChild;
else p = p->leftChild;
}
}
return 0;
}
int Insert(BiTreeNode **root, DataType item)
{
BiTreeNode *current, *parent = NULL, *p;
current = *root;
while(current != NULL)
{
if(strcmp(current->data.key , item.key)==0) return 0;
parent = current;
if(strcmp(current->data.key , item.key)<0) current = current->rightChild;
else current = current->leftChild;
}
p = (BiTreeNode *)malloc(sizeof(BiTreeNode));
if(p == NULL)
{
printf(“空间不够!”);
exit(1);
}
p->data = item;
p->leftChild = NULL;
p->rightChild = NULL;
if(parent == NULL) *root = p;
else if(strcmp(item.key , parent->data.key)<0)
parent->leftChild = p;
else
parent->rightChild = p;
return 1;
}
void InTraverse(BiTreeNode *root)
{
if(root == NULL) return;
if(root->leftChild != NULL)
InTraverse(root->leftChild);
printf(“%s “, root->data.key);
if(root->rightChild != NULL)
InTraverse(root->rightChild);
}
void main(void)
{
DataType test[] = {“Dec”,”Feb”,”Nov”,”Oct”,”June”,”Sept”,”Aug”,”Apr”,”May”,”July”,”Jan”,”Mar”}, x = {“Sept”};
int n = 12, i, s;
BiTreeNode *root = NULL;
for(i = 0; i < n; i++)
{
Insert(&root, test[i]);
}
// PreTraverse(root);
InTraverse(root);
s = Search(root, x);
if(s == 1)
printf(“/n数据元素%s存在!”, x.key);
else
printf(“/n数据元素不存在!”);
}