| 12
 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
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 
 |  
    public partial class Café : Form
    {
        public Café()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            Dictionnaire dc = new Dictionnaire();
            dc.AjouterMot("bonjour");
            dc.AjouterMot("banjul");
            dc.AjouterMot("angers");
 
            string[] rs = dc.Possibilités("b");
            string msg = Colle(rs);
            MessageBox.Show(msg);
 
            rs = dc.Possibilités("ban");
            msg = Colle(rs);
            MessageBox.Show(msg);
 
            rs = dc.Possibilités("an");
            msg = Colle(rs);
            MessageBox.Show(msg);
 
            rs = dc.Possibilités("n");
            msg = Colle(rs);
            MessageBox.Show(msg);
 
            rs = dc.Possibilités("");
            msg = Colle(rs);
            MessageBox.Show(msg);
 
            Close();
        }
 
        private static string Colle(string[] rs)
        {
            string msg = "";
            foreach (string r in rs)
            {
                msg += r + "\n";
            }
            return msg;
        }
    }
 
    class Section : IComparable<Section>
    {
        public bool terminal = false;
 
        public char c = '\0';
 
        public List<Section> next = new List<Section>();
 
        public Section this[char key]
        {
            get
            {
                Section result = TryGet(key);
 
                if (result == null)
                {
                    result = new Section();
                    result.c = key;
                    next.Add(result);
                }
 
                return result;
            }
        }
 
        public Section TryGet(char key)
        {
            Section result = next.Find(new Predicate<Section>(delegate(Section test)
            {
                return test != null && test.c == key;
            }));
            return result;
        }
 
        #region IComparable<Section> Membres
 
        public int CompareTo(Section other)
        {
            if (other == null)
                return 1;
            else
                return c.CompareTo(other.c);
        }
 
        #endregion
    }
 
    class Dictionnaire
    {
        public Section racine = new Section();
 
        public void AjouterMot(string mot)
        {
            Section current = racine;
            char[] chrs = mot.ToCharArray();
            for (int i = 0; i < chrs.Length; i++)
            {
                current = current[chrs[i]];
            }
            current.terminal = true;
        }
 
        protected string[] BindAll(Section section)
        {
            List<string> result = new List<string>();
 
            if (section.terminal)
                result.Add(section.c.ToString());
 
            foreach (Section sub in section.next)
            {
                foreach (string item in BindAll(sub))
                {
                    result.Add(section.c.ToString() + item);
                }
            }
            return result.ToArray();
        }
 
        public string[] Possibilités(string début)
        {
            if (String.IsNullOrEmpty(début))
            {
                List<string> result = new List<string>();
                foreach (Section s in racine.next)
                {
                    result.AddRange(BindAll(s));
                }
                return result.ToArray();
            }
            else
            {
                char[] chrs = début.ToCharArray();
                Section current = racine;
                for (int i = 0; i < chrs.Length; i++)
                {
                    current = current.TryGet(chrs[i]);
                    if (current == null)
                        return new string[0];
                }
 
                string[] r_array = BindAll(current);
                for (int i = 0; i < r_array.Length; i++)
                {
                    r_array[i] = début.Substring(0, début.Length - 1) + r_array[i];
                }
 
                return r_array;
            }
        }
    } | 
Partager