一个简单的英语到汉语的翻译过程(搜索二叉树)

英语到汉语的翻译:

原理:在我看来,既可以运用数组,也可以运用搜索二叉树,或者是其它任意一种存储结构应该都可以完成这个操作,但应用搜索二叉树,效率会更高,因为每一次搜索,就会排除许多无用的数据。

    

例如,在上图中查找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;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值