Commit 435232c3dda9a8736f3c370777e1559a002a7b9b
1 parent
395ede86
update4 withHash
Showing
2 changed files
with
21 additions
and
12 deletions
Show diff stats
treeh.c
@@ -27,6 +27,8 @@ void delete_tree(tree t){ | @@ -27,6 +27,8 @@ void delete_tree(tree t){ | ||
27 | 27 | ||
28 | 28 | ||
29 | 29 | ||
30 | + | ||
31 | + | ||
30 | //Casual functions | 32 | //Casual functions |
31 | bool is_empty(tree t){ | 33 | bool is_empty(tree t){ |
32 | return t==NULL; | 34 | return t==NULL; |
@@ -51,26 +53,36 @@ int hash(char c){ | @@ -51,26 +53,36 @@ int hash(char c){ | ||
51 | 53 | ||
52 | 54 | ||
53 | 55 | ||
54 | -// | ||
55 | -bool addto_tree(tree t,string s,unsigned int index,bool isIn){ | ||
56 | - //recursive, when called : set isIn to true | 56 | + |
57 | + | ||
58 | +//loading functions | ||
59 | +bool addto_tree(tree t,string s){ | ||
60 | + //recursive, when called : set isIn to true, and reset index | ||
57 | //return wether s is already in t or not | 61 | //return wether s is already in t or not |
62 | + bool ret; | ||
58 | if(s[0]=='\0'){ | 63 | if(s[0]=='\0'){ |
64 | + ret=t->isEnd; | ||
59 | t->isEnd=true; | 65 | t->isEnd=true; |
60 | - return isIn; | 66 | + return ret; |
67 | + } | ||
68 | + if(t->next[hash(s[0])]==NULL){ | ||
69 | + t->next[hash(s[0])]=make_node(s[0],false); | ||
70 | + addto_tree2(t->next[hash(s[0])],s+1); | ||
71 | + return false; | ||
61 | } | 72 | } |
62 | - if(t->next[hash(s[0])]==NULL) | ||
63 | - t->next[hash(s[0])]=make_node(s[0],false); | 73 | + else |
74 | + addto_tree(t->next[hash(s[0])],s+1,isIn); | ||
64 | } | 75 | } |
65 | void addto_tree2(tree t,string s){ | 76 | void addto_tree2(tree t,string s){ |
66 | - //faster than addto_tree, used when it is knowned the word is not yet in dictionnary | 77 | + //faster than addto_treeused when it is knowned the word is not yet in dictionnary |
67 | if(s[0]=='\0'){ | 78 | if(s[0]=='\0'){ |
68 | t->isEnd=true; | 79 | t->isEnd=true; |
69 | return; | 80 | return; |
70 | } | 81 | } |
71 | t->next[hash(s[0])]=make_node(s[0],false); | 82 | t->next[hash(s[0])]=make_node(s[0],false); |
72 | - addto_tree2(t->next[hash(s[0])], | 83 | + addto_tree2(t->next[hash(s[0])],s+1); |
73 | } | 84 | } |
85 | + | ||
74 | void loadfrom_file(tree,FILE*){} | 86 | void loadfrom_file(tree,FILE*){} |
75 | void loadfrom_keyboard(tree){} | 87 | void loadfrom_keyboard(tree){} |
76 | 88 |
treeh.h
@@ -24,14 +24,11 @@ void delete_tree(tree); | @@ -24,14 +24,11 @@ void delete_tree(tree); | ||
24 | bool is_empty(tree); | 24 | bool is_empty(tree); |
25 | bool is_end(tree); | 25 | bool is_end(tree); |
26 | bool is_followed(tree);//tells if tree has following letters | 26 | bool is_followed(tree);//tells if tree has following letters |
27 | +int hash(char); | ||
27 | 28 | ||
28 | void addto_tree(tree,char*,int); | 29 | void addto_tree(tree,char*,int); |
29 | void loadfrom_file(tree,FILE*); | 30 | void loadfrom_file(tree,FILE*); |
30 | void loadfrom_keyboard(tree); | 31 | void loadfrom_keyboard(tree); |
31 | 32 | ||
32 | 33 | ||
33 | -int hash(char); | ||
34 | - | ||
35 | - | ||
36 | - | ||
37 | #endif //TREEH_H | 34 | #endif //TREEH_H |