Commit 4043090fa8792612792fc3366326c47fc98c6fee
1 parent
d039b248
update2 withHash
Showing
2 changed files
with
71 additions
and
20 deletions
Show diff stats
treeh.c
1 | 1 | #include "treeh.h" |
2 | 2 | |
3 | +//initializers & destroyer | |
3 | 4 | tree make_empty_tree(){ |
4 | 5 | return NULL; |
5 | 6 | } |
6 | - | |
7 | -void addto_tree(tree,char*,int){ | |
8 | - if | |
9 | - | |
7 | +node* make_empty_node(){ | |
8 | + node*n=malloc(sizeof(node)); | |
9 | + n->letter='\0'; | |
10 | + n->isEnd=false; | |
11 | + for(int i=0;i<NBCHAR;i++) | |
12 | + n->next[i]=make_empty_tree(); | |
13 | +} | |
14 | +node* make_node(char l,bool end){ | |
15 | + node*n=malloc(sizeof(node)); | |
16 | + n->letter=l; | |
17 | + n->isEnd=end; | |
18 | + for(int i=0;i<NBCHAR;i++) | |
19 | + n->next[i]=make_empty_tree(); | |
20 | +} | |
21 | +void delete_tree(tree t){ | |
22 | + if(is_empty(t))return; | |
23 | + for(int i=0;i<NBCHAR;i++) | |
24 | + delete_tree(t->next[i]); | |
25 | + free(t); | |
10 | 26 | } |
11 | -void loadfrom_file(tree,FILE*){} | |
12 | -void loadfrom_keyboard(tree){} | |
13 | 27 | |
14 | -char is_empty(tree){ | |
28 | + | |
29 | + | |
30 | +//Casual functions | |
31 | +bool is_empty(tree t){ | |
32 | + return t==NULL; | |
33 | +} | |
34 | +bool is_followed(tree t){ | |
15 | 35 | int i; |
16 | 36 | for(i=0;i<NBCHAR;i++){ |
17 | 37 | if(t->next[i]!=NULL) |
18 | - return 0; | |
38 | + return true; | |
19 | 39 | } |
20 | - return 1; | |
40 | + return false; | |
21 | 41 | } |
22 | -char is_end(tree t){ | |
23 | - return t->isEnd!=0; | |
42 | +bool is_end(tree t){ | |
43 | + return t->isEnd; | |
24 | 44 | } |
25 | - | |
26 | 45 | int hash(char c){ |
27 | - //needs to check c isalpha or '; | |
46 | + //needs to check c wether isalpha or '\'' | |
28 | 47 | if(c='\'') |
29 | 48 | return 0; |
30 | 49 | return c%64; |
31 | 50 | } |
51 | + | |
52 | + | |
53 | + | |
54 | +// | |
55 | +bool addto_tree(tree t,string s,bool isIn){ | |
56 | + //recursive, when called : set isIn to true | |
57 | + //return wether s is already in t or not | |
58 | + if(s[0]=='\0'){ | |
59 | + t->isEnd=true; | |
60 | + return isIn; | |
61 | + } | |
62 | + if(t->next[hash(s[0])]==NULL) | |
63 | + t->next[hash(s[0])]=make_node(s[0],false); | |
64 | +} | |
65 | +void addto_tree2(tree t,string s){ | |
66 | + //faster than addto_tree, used when it is knowned the word is not yet in dictionnary | |
67 | + if(s[0]=='\0'){ | |
68 | + t->isEnd=true; | |
69 | + return; | |
70 | + } | |
71 | + t->next[hash(s[0])]=make_node(s[0],false); | |
72 | + addto_tree2(t->next[hash(s[0])], | |
73 | +} | |
74 | +void loadfrom_file(tree,FILE*){} | |
75 | +void loadfrom_keyboard(tree){} | |
76 | + | ... | ... |
treeh.h
... | ... | @@ -4,25 +4,31 @@ |
4 | 4 | #include <stdlib.h> |
5 | 5 | #include <stdio.h> |
6 | 6 | #include <string.h> |
7 | +#include <stdbool.h> | |
7 | 8 | |
8 | -#define NBCHAR 27 //a-z + ' | |
9 | +#define NBCHAR 27 //A-Z + ' | |
9 | 10 | |
10 | 11 | |
11 | -typedef | |
12 | +typedef struct _node node, *tree; | |
12 | 13 | struct node{ |
13 | 14 | char letter; |
14 | - char isEnd; | |
15 | - node* next[NBCHAR] | |
16 | -} *ptNode,*tree; | |
15 | + bool isEnd; | |
16 | + node* next[NBCHAR]; | |
17 | +}; | |
17 | 18 | |
18 | 19 | tree make_empty_tree(); |
20 | +node* make_empty_node(); | |
21 | +node* make_node(char,bool); | |
22 | +void delete_tree(tree); | |
23 | + | |
24 | +bool is_empty(tree); | |
25 | +bool is_end(tree); | |
26 | +bool is_followed(tree);//tells if tree has following letters | |
19 | 27 | |
20 | 28 | void addto_tree(tree,char*,int); |
21 | 29 | void loadfrom_file(tree,FILE*); |
22 | 30 | void loadfrom_keyboard(tree); |
23 | 31 | |
24 | -char is_empty(tree); | |
25 | -char is_end(tree); | |
26 | 32 | |
27 | 33 | int hash(char); |
28 | 34 | ... | ... |