如何用c语言的数据结构做一个英语词典,要求是:用二叉树,能实现建立词典,查找插入删除单词

2025-02-27 07:08:53
推荐回答(1个)
回答1:

#include

#include

#include

#include

#define
MAXWORD
100
typedef
struct
tnode{
char
*word;
int
count;
struct
tnode
*left;
struct
tnode
*right;
//
tnode(char
*s,
int
w,
tnode
*p,
tnode
*q){
//
//
}
}*tnodeptr;
struct
tnode
*addtree(struct
tnode
*,
char
*);
void
deltree(struct
tnode
*,
char
*);
void
treeprint(struct
tnode*,
FILE
*fp,
int
n,
int
&m);
void
showMenu();
void
main()
{
int
N,
k;
FILE
*fp;
struct
tnode
*root;
char
word[MAXWORD],
txt[25];
root=NULL;
while
(1)
{
showMenu();
scanf("%d",
&N);
switch(N)
{
case
1:
printf("连续输入,字符
0
结束:\n");
while
(scanf("%s",
word)!=EOF)
{
if(word[0]=='0')
break;
if(isalpha(word[0]))
root=addtree(root,
word);
}
printf("单词已插入\n");
break;
case
2:
if(root==NULL)
{printf("NULL字典\n");
break;}
printf("字典:\n");
treeprint(root,
fp,
0,k
=
1);
break;
case
3:
printf("输入删除的单词\n");
scanf("%s",
word);
deltree(root,
word);
break;
case
4:
printf("输入保存文件名:
");
scanf("%s",
txt);
strcat(txt,
".txt");
fp=fopen(txt,
"w");
if(fp==NULL)
{printf("文件写错误!");
break;}
treeprint(root,
fp,
1,
k
=
1);
fclose(fp);
printf("以保存到%s
\n",
txt);
break;
case
0:
return
;
default:
printf("输入错误!");
break;
}
}
return
;
}
struct
tnode*
talloc();
char
*strdup(char*);
struct
tnode
*addtree(struct
tnode
*p,
char
*w)
{
int
cond;
if
(p==NULL)
{
p=talloc();
p->word=strdup(w);
p->count=1;
p->left=p->right=NULL;
}
else
if((cond=strcmp(w,
p->word))==0)
p->count++;
else
if
(cond<0)
p->left=addtree(p->left,
w);
else
p->right=addtree(p->right,
w);
return
p;
}
/**************************************************************************
*
分4种情况:
*
1.
叶子结点:直接删除;
*
2.
结点只有左孩子:将该左孩子连接到该结点的双亲;
*
3.
结点只有右孩子:将该右孩子连接到该结点的双亲;
*
4.
结点有左右孩子:
*
a.
将该结点左子树的最右结点与该结点互换,然后删除左子树的最右结点;
*
或者
*
b.
将该结点右子树的最左结点与该结点互换,然后删除右子树的最左结点。
***************************************************************************/
void
deltree(struct
tnode
*p,
char
*w)
{
int
co,
t=0;
struct
tnode
*q=NULL,
*r=NULL;
while
(p!=NULL
&&
(co=strcmp(w,
p->word))!=0)
{
if(co
<
0)
{q
=p;
p=p->left;
t
=1;}
else
{q
=p;
p=p->right;
t
=0;}
}
if(p==NULL)
printf("没有此单词!\n");
else
if
(p->left==NULL
&&
p->right==NULL)
//<1>
{
if(t==1)q->left=
NULL;
else
q->right=NULL;
}
else
if(p->left
&&
p->right==NULL)
//<2>
{
if(t==1)
q->left=p->left;
else
q->right
=p->left;
}
else
if(p->left==NULL
&&
p->right)
//<3>
{
if(t==1)q->left=p->right;
else
q->right=p->right;
}
else
//<4>
{
r=p->left;
while(r->right)r
=r->right;
r->right
=p->right;
if(t==1)q->left
=
r;
else
q->right
=
r;
}
printf("已删除:%s
\n",
w);
return
;
}
//------------------------
//print
//------------------------
void
treeprint(struct
tnode*
p,FILE
*fp,
int
n,
int
&m)
{
if(p!=NULL)
{
treeprint(p->left,
fp,
n,m);
if(n)fprintf(fp,
"%-4d%-4d%s\n",m++,p->count,p->word);
else
printf("%-4d%-4d%s\n",m++,p->count,p->word);
treeprint(p->right,
fp,
n,m);
}
}
struct
tnode
*talloc()
{
return
(struct
tnode*)malloc(sizeof(struct
tnode));
}
char
*strdup(char*s)
{
char
*p;
p=(char*)malloc(strlen(s)+1);
if(p!=NULL)
strcpy(p,
s);
return
p;
}
void
showMenu()
{
printf("\n
输入选择\t
\
\n
1.
输入单词
\
\n
2.
查看字典
\
\n
3.
删除单词
\
\n
4.
保存字典
\
\n
0.
退出\n");
}