简体   繁体   中英

Trimming binary tree data c

I'm currently developing some methods to work with binary trees. This is the structure I'm using.

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

When I submit the code I have to a platform that evaluates it, returning Accepted, Wrong Answer, Presentation Error, Runtime Error, Compile error, etc... I'm getting the Presentation Error.

According to the platform, when I load the words to the tree, I'm inserting them with some spaces at the beginning and at the end.

Here's the code:

#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 EXISTENTE\n");
        return root;
        } else if(root == NULL && acrescenta) {
        root = createNode(entry, translation, views, marker);
        printf("PALAVRA ACRESCENTADA\n");
    } 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 %s\n", 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 MARCADA\n", root->entry);
    }
    markWord(root->right, entry);
}


void printTree(node* root){
    if(root == NULL) return;

    printTree(root->left);
    printf("%s\n", root->entry);
    printTree(root->right);
}


void printMarkedWords(node *root){
    if(root == NULL) return;

    printMarkedWords(root->left);
    if(root->marker == true) printf("%s\n", 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 %[^\t\n]", option, entry, translation);

        if(strcmp(option, "CARREGA") == 0) {
            while(1) {
                fgets(line, 175, stdin);
                line[strlen(line) - 1] = '\0';
                if(strcmp(line, "fim$dicionario") == 0) {
                    printf("DICIONARIO CARREGADO\n");
                    break;
                } else {
                    sscanf(line, "%s %[^\t\n]", entry, translation);
                    root = insert(root, entry, translation, 0, false, false);
                }
            }
        } else if(strcmp(option, "PESQUISA") == 0) {
            if(!search(root, entry, false)) printf("PALAVRA NAO EXISTENTE\n");
        } 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 EXISTENTE\n");
            else (markWord(root, entry));
        } else if(strcmp(option, "LISTA_MARCADAS") == 0) {
            printMarkedWords(root);
            printf("FIM MARCADAS\n");
        } else if(strcmp(option, "LISTA_ALFANUM") == 0) {
            printTree(root);
            printf("FIM LISTA\n");
        }
    }

    return 0;
}

It seems that I got it right, but for some reason, some random spaces at the beginning of my data (entry and translation) are giving me the Presentation Error. Sorry if my explanation is not the most correct, but didn't want to make a long unreadable post.

Thanks :)

The problem was with the platform input that was adding some spaces to the words, so I had to take care of it with some simple trim functions.

Thanks everyone who took time of their day to help me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM