二进制搜索树疯狂

时间:2014-05-30 23:10:02

标签: c binary-search-tree

我必须编写一个可以处理库存的二叉搜索树的实现。它读取包含所有书籍的文本文件,并按字母顺序将书籍添加到树中。我一直在与DAYS的Insertar()函数代码进行斗争,我无法使其正常工作,它基本上会收到与该书相关的所有数据的树根的指针。如果根是NULL,那么它将进入一个节点,该节点具有在函数中输入的所有值,并将内存方向作为空节点。问题是,它在本地进行,最终它没有分配它。有人可以帮我纠正这个具体的功能吗?

功能和结构:

nodoArbol:节点
ArbolBin:Binary Tree,它有一个指向根节点的指针和一个带有元素数量的int InitNodo:在​​节点中,返回指向节点
的指针 Raiz:返回指向二叉树根的指针 清除,Clear_Aux:清除树 Ingresar:Insert()函数和问题的来源
Imprimir:刻划节点的元素。

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

typedef struct nodoArbol {
    char nombre[51],autor[51];
    int valor,stock,anno;
    struct nodoArbol *der;
    struct nodoArbol *izq;
} tNodoArbol;

typedef struct {
    tNodoArbol *root;
    int n;
} ArbolBin;

tNodoArbol* InitNodo(char *nombre,char *autor, int stock, int valor, int anno){
    tNodoArbol *p;
    p= (tNodoArbol*)malloc(sizeof(tNodoArbol));
    strcpy(p->nombre, nombre);
    strcpy(p->autor, autor);
    p->stock = stock;
    p->anno = anno;
    p->valor = valor;
    p->izq = NULL;
    p->der = NULL;
    return p;
}

tNodoArbol* Raiz(ArbolBin p){
    return (&p)-> root;
}

void Init(ArbolBin *p){
    p->root = NULL;
    p->n = 0;
}

void clear_aux(tNodoArbol *nodo){
    if (nodo == NULL){
        return;
    }
    clear_aux(nodo->izq);
    clear_aux(nodo->der);
    free((void *) nodo);
}

void Clear(ArbolBin *p){
    clear_aux(p->root);
    p->root = NULL;
    p->n = 0;
}

void Insertar (tNodoArbol *nodo, char *nombre,char *autor, int stock, int valor, int anno){

    if (nodo == NULL){
        nodo = (InitNodo(nombre,autor,stock,valor,anno));
    }
    else{
        int result;
        result = strcmp(nodo->nombre,nombre);
        if (result>0){
            Insertar (nodo->der, nombre,autor,stock,valor,anno);
        }
        else if (result<0){
            Insertar (nodo->izq, nombre,autor,stock,valor,anno);
        }
    }
}

void Imprimir(tNodoArbol *nodo){
    printf("Nombre:%s \n",nodo->nombre);
    printf("Autor:%s \n",nodo->autor);
    printf("Stock:%d \n",nodo->stock);
    printf("Valor:%d \n",nodo->valor);
    printf("anno:%d \n",nodo->anno);
}

int main(){

char a[50]= "holi",b[50] ="asdasdasd";
ArbolBin Tree;
tNodoArbol *Root;

Init(&Tree);
Root = Raiz(Tree);
Insertar(Root,a,b,2,1000,2014);
Imprimir(Root);
return 0;
}

1 个答案:

答案 0 :(得分:0)

tNodoArbol *Root;

Insertar(Root,a,b,2,1000,2014);

void Insertar (tNodoArbol *nodo, char *nombre,char *autor, int stock, int valor, int anno){

    if (nodo == NULL){
        nodo = (InitNodo(nombre,autor,stock,valor,anno));
    }
    else{
        int result;
        result = strcmp(nodo->nombre,nombre);
        if (result>0){
            Insertar (nodo->der, nombre,autor,stock,valor,anno);/*nodo just a pointer,node->der  is illeagl*/
        }
        else if (result<0){
            Insertar (nodo->izq, nombre,autor,stock,valor,anno);/*the same error */
        }
    }
}
-----------------------------------------------------------------------------------------
your declaration a pointer, you want through the Insertar()  change the Root, you need use 
Insertar(&Root,a,b,2,1000,2014), because the Root in the Insertar() is not the Root in the main() ,they just have the same value,we just copy the value of Root(main)  to Root(Insertar).
---------------------------------------------------------------------------------------
void Insertar (tNodoArbol **nodo, char *nombre,char *autor, int stock, int valor, int anno){

    if (*nodo == NULL){
        *nodo = (InitNodo(nombre,autor,stock,valor,anno));
    }
    else{
        int result;
        result = strcmp((*nodo)->nombre,nombre);
        if (result>0){
            Insertar ((*nodo)->der, nombre,autor,stock,valor,anno);
        }
        else if (result<0){
            Insertar ((*nodo)->izq, nombre,autor,stock,valor,anno);
        }
    }
}
相关问题