修剪二叉树数据 c.



我目前正在开发一些使用二叉树的方法。这是我正在使用的结构。

struct node {
    char entry[40];
    char translation[100];
    int views;
    bool marker;
    node* left;
    node* right;
};

当我提交代码时,我必须向评估它的平台提交代码,返回接受、错误答案、演示错误、运行时错误、编译错误等......我收到演示错误。

根据平台,当我将单词加载到树上时,我会在开头和结尾插入一些空格。

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>

using namespace std;
struct node {
    char entry[40];
    char translation[100];
    int views;
    bool marker;
    node* left;
    node* right;
};

bool search(node* root, char entry[40], bool acrescenta);
node* createNode(char entry[40], char translation[40], int views, bool marker) {
    node* newNode = new node();
    strcpy(newNode->entry, entry);
    strcpy(newNode->translation, translation);
    newNode->views = 0;
    newNode->marker = false;
    newNode->left = newNode->right = NULL;
    return newNode;
}
node* insert(node* root, char entry[40], char translation[40], int views, bool marker, bool acrescenta) {
    if(search(root, entry, acrescenta)) {
        printf("PALAVRA JA EXISTENTEn");
        return root;
        } else if(root == NULL && acrescenta) {
        root = createNode(entry, translation, views, marker);
        printf("PALAVRA ACRESCENTADAn");
    } else if(root == NULL && !acrescenta) {
                root = createNode(entry, translation, views, marker);
    } else if(strcmp(entry, root->entry) < 0) {
                root->left = insert(root->left, entry, translation, views, marker, acrescenta);
    } else {
                root->right = insert(root->right, entry, translation, views, marker, acrescenta);
    }
    return root;
}

bool search(node* root, char entry[40], bool acrescenta) {
    if(root == NULL) return false;
    if((strcmp(entry, root->entry) == 0) && acrescenta) {
            return true;
    } else if(strcmp(entry, root->entry) == 0) {
                printf("%s %sn", root->entry, root->translation);
                return true;
    } else if(strcmp(entry, root->entry) < 0) {
                return search(root->left, entry, acrescenta);
    } else {
                return search(root->right, entry, acrescenta);
    }
}

void markWord(node *root, char entry[40]){
    if(root == NULL) return;
    markWord(root->left, entry);
    if(strcmp(root->entry, entry) == 0) {
        root->marker = true;
        printf("%s MARCADAn", root->entry);
    }
    markWord(root->right, entry);
}

void printTree(node* root){
    if(root == NULL) return;
    printTree(root->left);
    printf("%sn", root->entry);
    printTree(root->right);
}

void printMarkedWords(node *root){
    if(root == NULL) return;
    printMarkedWords(root->left);
    if(root->marker == true) printf("%sn", root->entry);
    printMarkedWords(root->right);
}
int main() {
    node* root = NULL;
    char option[20];
    char entry[40];
    char translation[100];
    char line[175];
        while(fgets(line, 175, stdin) != NULL && line[0] != 'n') {
        sscanf(line, "%s %s %[^tn]", option, entry, translation);
        if(strcmp(option, "CARREGA") == 0) {
            while(1) {
                fgets(line, 175, stdin);
                line[strlen(line) - 1] = '';
                if(strcmp(line, "fim$dicionario") == 0) {
                    printf("DICIONARIO CARREGADOn");
                    break;
                } else {
                    sscanf(line, "%s %[^tn]", entry, translation);
                    root = insert(root, entry, translation, 0, false, false);
                }
            }
        } else if(strcmp(option, "PESQUISA") == 0) {
            if(!search(root, entry, false)) printf("PALAVRA NAO EXISTENTEn");
        } else if(strcmp(option, "ACRESCENTA") == 0) {
            root = insert(root, entry, translation, 0, false, true);
        } else if(strcmp(option, "MARCA") == 0) {
            if(!search(root, entry, true)) printf("PALAVRA NAO EXISTENTEn");
            else (markWord(root, entry));
        } else if(strcmp(option, "LISTA_MARCADAS") == 0) {
            printMarkedWords(root);
            printf("FIM MARCADASn");
        } else if(strcmp(option, "LISTA_ALFANUM") == 0) {
            printTree(root);
            printf("FIM LISTAn");
        }
    }
    return 0;
}

似乎我做对了,但由于某种原因,我的数据开头的一些随机空格(输入和翻译)给了我演示错误。对不起,如果我的解释不是最正确的,但不想写一篇长篇不可读的帖子。

谢谢:)

问题在于平台输入为单词添加了一些空格,所以我不得不使用一些简单的修剪功能来处理它。

感谢所有花时间帮助我的人。

最新更新