-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.adb
83 lines (74 loc) · 2.55 KB
/
tree.adb
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
package body Tree is
procedure Search_And_Display(T : in Tree; Letters : in String) is
begin
Search_And_Display_arbre(T,Letters,'a');
end;
procedure Search_And_Display_arbre(T: in Tree; Letters: in String; Letter: in Character) is
cpt : Natural := 0;
begin
cpt := Count_Occurrence(Letters,Letter);
for i in 0..cpt loop
if T.childs(i) /= null then
--If we are not at the end we continue to go througt the tree
if Letter /= 'z' then
Search_And_Display_arbre(T.childs(i),Letters,character'succ(Letter));
else
--Use procedure display to display all words in the leaf
iterate(T.childs(i).words,display'access);
end if;
else
return;
end if;
end loop;
end;
--Take a cursor to display a list of string
procedure display(C : Cursor) is
word : Unbounded_String;
begin
word := Element(C);
Put_Line(To_String(word));
end;
procedure Insertion(T : in out Tree; Word : in String) is
begin
Insertion_arbre(T,Word,'a');
end;
procedure Insertion_arbre(T: in out Tree; Word: in String; Letter : in Character) is
cpt : Natural := 0;
begin
cpt := Count_Occurrence(Word,Letter);
--If the node doesn't exist yet we create it
if cpt <= M then
if T.childs(cpt) = null then
T.childs(cpt) := new Node;
T.childs(cpt).letter := Letter;
T.childs(cpt).occurrence := cpt;
end if;
if Letter /= 'z' then
Insertion_arbre(T.childs(cpt),Word,character'succ(Letter));
--When we are at the end of the tree we add the word to the list
else
--Add word to the list
T.childs(cpt).words.Append(To_Unbounded_String(Word));
end if;
end if;
end;
-- Count how many "Letter" there is within "Word"
function Count_Occurrence(Word : in String; Letter : in Character) return Natural is
Cpt : Natural := 0;
begin
for i in 1..(Word'Length) loop
if Word(i) = Letter then
Cpt := Cpt+1;
end if;
end loop;
return Cpt;
end;
-- Create a new tree with only one root node
function New_Tree return Tree is
T : Tree := new Node;
begin
return T;
end;
end;