probleme arbre binaire de chaine
Bonjour,
je fais un arbre tout simple, arbre binaire qui ajoute des chaines de caracteres et les tris selons l'ordre alphabetique,le probleme, c'est que mon arbre ne marche pas, et je n'arrive pas a savoir pourquoi :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| #include <stdio.h>
#include <assert.h>
typedef struct node{
struct node* left;
struct node* right;
char* value;
}Node;
Node* noeud(char* s, Node* r, Node* l){
Node* res = malloc(sizeof(Node));
assert( res != NULL);
res->value = s;
res->left = l;
res->right = r;
return res;
}
void insert(char* str, Node* tree){
if(tree == NULL){
tree = noeud(str,NULL,NULL);
printf("%s\n", tree->value);
}
else if (strcmp(str,tree->value) != 0) {
if(strcmp(str,tree->value) < 0) insert(str,tree->left);
if(strcmp(str,tree->value) < 0) insert(str,tree->right);
}
}
Node* search(char* str, Node* tree){
while(tree != NULL){
if(strcmp(str,tree->value) ==0) return tree;
else{
if(strcmp(str,tree->value) < 0) tree = tree->left;
else tree = tree->right;
}
}
return tree;
}
void parcours(Node* tree){
if(tree != NULL){
parcours(tree->left);
printf("%s ", tree->value);
parcours(tree->right);
}
printf("tree is null");
}
int main(void){
Node* root= NULL;
int i, x;
char* s="hello";
insert(s,root);
insert("ron",root);
insert("jo",root);
insert("david",root);
parcours(root);
printf("\n");
for (i = 0; i < 100; i++)
if (search("hello", root) != NULL) printf("%d", i);
printf("\n");
return 0;
} |
enfin je sais ou ça plante, ça plante a la methode insert ou il n'arrive pas a comparer tree avec NULL, donc ma question est : peut on comparer tree avec NULL de cette façon :
Code:
1 2 3 4
|
void insert(char* str, Node* tree){
if(tree == NULL){ |
merci beaucoup