我需要在二叉树的特定位置插入一个节点。为此,我首先需要找到父节点(使用其名称)。因此,我的计划是对所有节点进行搜索,直到找到名称正确的节点(假设名称是唯一的)。
这是我的节点定义:
typedef struct NO* ArvBin;
struct NO{
char nome_do_no[5];
char tipo_do_no[2];
char semantica_do_no[100];
float confianca_do_no;
float valor_teste_do_no;
struct NO *esq;
struct NO *dir;
};
这是我用来搜索树的函数(键是一个字符串,我将其与节点名称(node-> nome_do_no)进行比较:
struct NO* busca(ArvBin *raiz, char pesquisa[]){
printf("\n\n%s", (*raiz)->nome_do_no);
if(!strcmp((*raiz)->nome_do_no,pesquisa)){
return raiz;
}
if((*raiz)->esq != NULL){
busca(&((*raiz)->esq),pesquisa);
}
if((*raiz)->dir != NULL){
busca(&((*raiz)->esq),pesquisa);
}
}
PS:esq表示左,dir表示右。 pesquisa []也是我要在节点名称上搜索的字符串。
尝试运行此搜索时出现细分错误。
先感谢您
编辑:
我已经更改了指出的错误。但是仍然出现SegFault。
我只发布了我的代码的一小部分,因此您可以根据需要复制它。
执行代码时,首先插入一个数字(条目数),然后以格式插入条目
B001“ Sytax” B000 D
其中B001是节点的名称,语法只是一个字符串,B000是父节点,D是应在其中添加新节点的那一侧(D代表右侧,E代表左侧)。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct NO* ArvBin;
struct NO{
char nome_do_no[5];
char tipo_do_no[2];
char semantica_do_no[100];
float confianca_do_no;
float valor_teste_do_no;
struct NO *pai;
struct NO *esq;
struct NO *dir;
};
//Function that creates a binary tree
ArvBin* cria_ArvBin(){
ArvBin* raiz = (ArvBin*) malloc(sizeof(ArvBin));
if(raiz != NULL)
*raiz = NULL;
return raiz;
}
//Function that searches for a node with name given by pesquisa[]
struct NO* busca(ArvBin *raiz, char pesquisa[]){
printf("\n\n%s", (*raiz)->nome_do_no);
if(!strcmp((*raiz)->nome_do_no,pesquisa)){
return raiz;
}
if((*raiz)->esq != NULL){
busca(&((*raiz)->esq),pesquisa);
}
if((*raiz)->dir != NULL){
busca(&((*raiz)->dir),pesquisa);
}
}
//Function that inserts the first element of the tree
int insereInicio_ArvBin(ArvBin* raiz, char nome_no[], char semantica[], float valor){
struct NO* novo;
novo = (struct NO*)malloc(sizeof(struct NO));
if(novo == NULL){
return 0;
}
novo->valor_teste_do_no = valor;
strcpy(novo->nome_do_no,nome_no);
strcpy(novo->semantica_do_no, semantica);
novo->dir = NULL;
novo->esq = NULL;
if(*raiz == NULL){
*raiz = novo;
}
return 1;
}
//Function that inserts other nodes in the tree (after the first one)
int insere_ArvBin(ArvBin* raiz, char nome_no[], float valor, char semantica[], float confianca, char nopai[], char lado_inserir[]){
if(raiz == NULL)
return 0;
struct NO* novo;
novo = (struct NO*) malloc(sizeof(struct NO));
if(novo == NULL)
return 0;
novo->valor_teste_do_no = valor;
novo->confianca_do_no = confianca;
strcpy(novo->nome_do_no,nome_no);
strcpy(novo->semantica_do_no, semantica);
novo->dir = NULL;
novo->esq = NULL;
if(lado_inserir[0] == 'D'){
busca(raiz, nopai)->dir = novo;
}
if(lado_inserir[0] == 'E'){
busca(raiz, nopai)->esq = novo;
}
return 1;
}
int main(){
char teste[200];
char nome_no[5];
char tipo_no[2];
char no_pai[5];
char lado[2];
char semantica[200];
int n;
float valorTeste;
float confianca;
ArvBin arvore_decisoes = cria_ArvBin();
scanf("%d", &n);
getchar();
for(int i = 0;i<n;i++){
fgets(teste, 200, stdin);
tipo_no[0] = teste[0];
if(tipo_no[0] == 'B'){
if(i==0){
sscanf(teste, "%s \"%[^\"] \" %s", nome_no,semantica,no_pai);
insereInicio_ArvBin(arvore_decisoes,nome_no, semantica, 0);
}
else{
sscanf(teste, "%s \"%[^\"] \" %s %s", nome_no,semantica,no_pai,lado);
insere_ArvBin(arvore_decisoes,nome_no,0,semantica,0,no_pai,lado);
}
}
else if(tipo_no[0] == 'N'){
if(i==0){
sscanf(teste, "%s \"%[^\"] \" %f %s", nome_no,semantica,&valorTeste,no_pai);
insereInicio_ArvBin(arvore_decisoes,nome_no, semantica, valorTeste);
}
else{
sscanf(teste, "%s \"%[^\"] \" %f %s %s", nome_no,semantica,&valorTeste,no_pai,lado);
insere_ArvBin(arvore_decisoes,nome_no,valorTeste,semantica,0,no_pai,lado);
}
}
else if(tipo_no[0] == 'C'){
sscanf(teste, "%s \"%[^\"] \" %s %s %f", nome_no,semantica,no_pai,lado, &confianca);
insere_ArvBin(arvore_decisoes,nome_no,0,semantica,confianca,no_pai,lado);
}
}
return 1;
}
最佳答案
错误在这里:
if((*raiz)->dir != NULL){
busca(&((*raiz)->esq),pesquisa); // <- should be dir, not esq
}
关于c - 递归搜索二叉树中的元素时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59144179/