gpt4 book ai didi

c - 修剪二叉树数据c

转载 作者:行者123 更新时间:2023-11-30 17:04:07 24 4
gpt4 key购买 nike

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

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

看来我做对了,但由于某种原因,我的数据(输入和翻译)开头的一些随机空格给了我演示错误。很抱歉,如果我的解释不是最正确的,但我不想发表一篇长而难以阅读的帖子。

谢谢:)

最佳答案

问题在于平台输入在单词中添加了一些空格,因此我必须使用一些简单的修剪功能来处理它。

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

关于c - 修剪二叉树数据c,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35920245/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com