C ++如何使用二进制搜索树创建字典

时间:2018-06-13 16:25:55

标签: c++

我想创建一个字典,但我无法在代码中输出2个树 我不知道下一步该做什么:

WORD *t1 = NULL;
WORD *t2 = NULL;
WORD *t = NULL;

void InsertWordEN(WORD *t1, TD x)
{
    if(t1 == NULL)
    {
        WORD *temp;
        temp = new WORD;
        strcpy(temp->data.EN, x.EN);
        temp->left = NULL;
        temp->right = NULL;
        t1 = temp;
    }
    else 
    {
        if (strcmp(t1->data.EN, x.EN) == 1)
        {
            InsertWordEN(t1->left,x);
        }
        else if(strcmp(t1->data.EN, x.EN) == 0)
        {
            InsertWordEN(t1->right,x);
        }
    }
}
void InsertWordVN(WORD *t2, TD x)
{
    if(t2 == NULL)
    {
        WORD *temp;
        temp = new WORD;
        strcpy(temp->data.VN, x.VN);
        temp->left = NULL;
        temp->right = NULL;
        t2 = temp;
    }
    else 
    {
        if (strcmp(t2->data.VN, x.VN) == 1)
        {
            InsertWordVN(t2->left,x);
        }
        else if(strcmp(t2->data.VN, x.VN) == 0)
        {
            InsertWordVN(t2->right,x);
        }
    }
}

void InsertWord(TD &x)
{
    cout<<"\EN: "; cin>>x.EN;
    InsertWordEN(t1,x);
    cout<<"\nVN: "; cin>>x.VN;
    InsertWordEN(t2,x);
}

void OutputWord_NLR(WORD *t)
{
    if(t1 != NULL)
    {
        cout<<t1->data.EN<<" "; 
        OutputWord_NLR(t1->left);
        OutputWord_NLR(t1->right);
    }
    else if(t2 != NULL)
    {
        cout<<t2->data.VN<<" "; 
        OutputWord_NLR(t2->left);
        OutputWord_NLR(t2->right);
    }
}   
int main()
{
    int select; TD x;
    InsertWord(x);
    cout<<"\nOutput Tree EN and VN";
    OutputWord_NLR(t);  
    getch();
    return 0;
}

我认为OutputWord_NLR函数错了? BTS。这对我来说太难了。我试图理解它:) 对不起!这似乎太长了。所以我无法发送所有代码

0 个答案:

没有答案