Blame view

treeh.c 1.61 KB
a4ef278c   bjeanlou   init withHash
1
2
  #include "treeh.h"
  
4043090f   bjeanlou   update2 withHash
3
  //initializers & destroyer
a637cab8   bjeanlou   update1 withHash
4
5
6
  tree make_empty_tree(){
    return NULL;
  }
4043090f   bjeanlou   update2 withHash
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  node* make_empty_node(){
    node*n=malloc(sizeof(node));
    n->letter='\0';
    n->isEnd=false;
    for(int i=0;i<NBCHAR;i++)
      n->next[i]=make_empty_tree();
  }
  node* make_node(char l,bool end){
    node*n=malloc(sizeof(node));
    n->letter=l;
    n->isEnd=end;
    for(int i=0;i<NBCHAR;i++)
      n->next[i]=make_empty_tree();
  }
  void delete_tree(tree t){
    if(is_empty(t))return;
    for(int i=0;i<NBCHAR;i++)
      delete_tree(t->next[i]);
    free(t);
a637cab8   bjeanlou   update1 withHash
26
  }
a637cab8   bjeanlou   update1 withHash
27
  
4043090f   bjeanlou   update2 withHash
28
29
  
  
435232c3   bjeanlou   update4 withHash
30
31
  
  
4043090f   bjeanlou   update2 withHash
32
33
34
35
36
  //Casual functions
  bool is_empty(tree t){
    return t==NULL;
  }
  bool is_followed(tree t){
a637cab8   bjeanlou   update1 withHash
37
38
39
    int i;
    for(i=0;i<NBCHAR;i++){
      if(t->next[i]!=NULL)
4043090f   bjeanlou   update2 withHash
40
        return true;
a637cab8   bjeanlou   update1 withHash
41
    }
4043090f   bjeanlou   update2 withHash
42
    return false;
a637cab8   bjeanlou   update1 withHash
43
  }
4043090f   bjeanlou   update2 withHash
44
45
  bool is_end(tree t){
    return t->isEnd;
a637cab8   bjeanlou   update1 withHash
46
  }
a637cab8   bjeanlou   update1 withHash
47
  int hash(char c){
4043090f   bjeanlou   update2 withHash
48
    //needs to check c wether isalpha or '\''
a637cab8   bjeanlou   update1 withHash
49
50
    if(c='\'')
      return 0;
395ede86   bjeanlou   update3 withHash
51
    return c%32;
a637cab8   bjeanlou   update1 withHash
52
  }
4043090f   bjeanlou   update2 withHash
53
54
55
  
  
  
435232c3   bjeanlou   update4 withHash
56
57
58
59
60
  
  
  //loading functions
  bool addto_tree(tree t,string s){
    //recursive, when called : set isIn to true, and reset index
4043090f   bjeanlou   update2 withHash
61
    //return wether s is already in t or not
435232c3   bjeanlou   update4 withHash
62
    bool ret;
4043090f   bjeanlou   update2 withHash
63
    if(s[0]=='\0'){
435232c3   bjeanlou   update4 withHash
64
      ret=t->isEnd;
4043090f   bjeanlou   update2 withHash
65
      t->isEnd=true;
435232c3   bjeanlou   update4 withHash
66
67
68
69
70
71
      return ret;
    }
    if(t->next[hash(s[0])]==NULL){
      t->next[hash(s[0])]=make_node(s[0],false);
      addto_tree2(t->next[hash(s[0])],s+1);
      return false;
4043090f   bjeanlou   update2 withHash
72
    }
435232c3   bjeanlou   update4 withHash
73
74
    else    
      addto_tree(t->next[hash(s[0])],s+1,isIn);
4043090f   bjeanlou   update2 withHash
75
76
  }
  void addto_tree2(tree t,string s){
435232c3   bjeanlou   update4 withHash
77
    //faster than addto_treeused when it is knowned the word is not yet in dictionnary
4043090f   bjeanlou   update2 withHash
78
79
80
81
82
    if(s[0]=='\0'){
      t->isEnd=true;
      return;
    }
    t->next[hash(s[0])]=make_node(s[0],false);
435232c3   bjeanlou   update4 withHash
83
    addto_tree2(t->next[hash(s[0])],s+1);
4043090f   bjeanlou   update2 withHash
84
  }
435232c3   bjeanlou   update4 withHash
85
  
4043090f   bjeanlou   update2 withHash
86
87
  void loadfrom_file(tree,FILE*){}
  void loadfrom_keyboard(tree){}