英语到汉语的翻译:
原理:在我看来,既可以运用数组,也可以运用搜索二叉树,或者是其它任意一种存储结构应该都可以完成这个操作,但应用搜索二叉树,效率会更高,因为每一次搜索,就会排除许多无用的数据。
例如,在上图中查找1,只需要通过上图的3次比较就可以得出结果,每一次排除了多种无效数据。
方法步骤:
1)应用到搜索二叉树的每个节点,节点信息包括英文,汉语,左孩子,右孩子。
2)建立搜索二叉树(详情参照博客:https://blog.csdn.net/xinger_28/article/details/82941358)
3)查找数据
头文件:英汉词典.h
#pragma once
#include<stdio.h>
#include<Windows.h>
#include<stdlib.h>
#include<string.h>
typedef struct English_Chinese{
char* array1;
char*array2;
struct English_Chinese*left;
struct English_Chinese*right;
}En_Ch;
//创建,插入,a->英文,b->汉语
int CreatTree(En_Ch**tree, char*a, char*b)
{
En_Ch*root = *tree;
En_Ch*parent = NULL;
while (root != NULL)//如果节点不为空
{
if (strncmp((root->array1), a,20) == 0)//由于二叉树不允许存在两个关键码相同的数
return -1;
parent = root;//父节点指向节点
if (strncmp(root->array1, a,20) ==1)//如果节点的关键码大于a的值,节点指向它的左孩子
root = root->left;
else //否则指向它的右孩子
root = root->right;
}
En_Ch*node = (En_Ch*)malloc(sizeof(En_Ch));//创建新节点
(node->array1) = a;
(node->array2 )= b;
node->left = NULL;
node->right = NULL;
if (parent == NULL)//如果父节点为空,表明为空树,将新节点作为搜索二叉树的根节点。
{
*tree = node;
return 0;
}
if (strncmp(parent->array1, a,20) ==1)//如果父节点的关键码大于a的值,父节点的左孩子指向新节点。
parent->left = node;
else//否则右孩子指向新节点
parent->right = node;
return 0;
}
//查找
int Look(En_Ch*tree, char *a)
{
En_Ch*root = tree;
while (root != NULL){
if (strncmp(root->array1, a, 20) == 0)
{
printf("%s: %s\n", root->array1, root->array2);
return 0;
}
if (strncmp(root->array1, a, 20) > 0)
root = root->left;
else
root = root->right;
}
if (root == NULL)
{
printf("%s: 没找到!\n",a);
return -1;
}
return 0;
}
void test()
{
En_Ch*tree = NULL;
CreatTree(&tree, "hehe", "呵呵");
CreatTree(&tree, "apple", "苹果");
CreatTree(&tree, "blana", "香蕉");
CreatTree(&tree, "like", "喜欢");
CreatTree(&tree, "love", "爱");
CreatTree(&tree, "pink", "粉红");
Look(tree, "love");
Look(tree, "abfhk");
printf("oo");
}
源文件:
#include"英汉词典.h"
int main()
{
test();
system("pause");
return 0;
}