修剪二叉树数据c

时间:2016-03-10 15:08:44

标签: c time-complexity binary-search-tree

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

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 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;
}

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

谢谢:)

1 个答案:

答案 0 :(得分:0)

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

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