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