-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtd-llg-arbre_radix.ml
95 lines (59 loc) · 2.18 KB
/
td-llg-arbre_radix.ml
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
84
85
86
87
88
89
90
91
92
93
94
95
type arbre =
|Nil
|Noeud of bool * arbre * arbre ;;
let rec cherche n a = match a with
|Nil -> false
|Noeud (b,x,y) when n = 0 -> b
|Noeud (b,x,y) when n mod 2 = 0 -> cherche (n/2) x
|Noeud (b,x,y) -> cherche (n/2) y ;;
let rec ajoute n a = match a with
|Nil when n = 0 -> Noeud (true,Nil,Nil)
|Nil when n mod 2 = 0 -> Noeud (false,ajoute (n/2) Nil, Nil)
|Nil -> Noeud (false,Nil,ajoute (n/2) Nil)
|Noeud (b,x,y) when n = 0 -> Noeud (true,x,y)
|Noeud (b,x,y) when n mod 2 = 0 -> Noeud (b,ajoute (n/2) x,y)
|Noeud (b,x,y) -> Noeud (b,x, ajoute (n/2) y) ;;
let rec construit l = match l with
|[]-> Nil
|x::r -> ajoute x (construit r ) ;;
let construit_term l =
let rec aux u a = match u with
|[]->a
|x::r -> aux r ( ajoute x a )
in aux l Nil ;;
let rec supprime n a = match a with
|Nil -> a
|Noeud (b,x,y) when n = 0 -> Noeud (false,x,y)
|Noeud (b,x,y) when n mod 2 = 0 -> Noeud (b,supprime (n/2) x ,y)
|Noeud (b,x,y) -> Noeud (b,x, supprime (n/2) y) ;;
let rec union a b = match a,b with
|Nil,ens | ens, Nil -> ens
|Noeud (b,x,y), Noeud (bb,xx,yy) -> Noeud (b || bb , union x xx , union y yy);;
let rec intersection a b = match a,b with
|Nil,ens | ens, Nil -> Nil
|Noeud (b,x,y), Noeud (bb,xx,yy) -> Noeud (b && bb , intersection x xx , intersection y yy);;
let rec ajouter_1 u = match u with
|[] -> []
|x::r -> (1::x)::( ajouter_1 r) ;;
let rec ajouter_0 u = match u with
|[] -> []
|x::r -> (0::x)::( ajouter_0 r) ;;
let recup u = match u with
|[]->[]
|x::r -> x ;;
let rec binar a = match a with
|Nil -> []
|Noeud (b,x,y) when b -> let p,q = binar x, binar y in ajouter_0 p @ [(recup p)] @ ajouter_1 q
|Noeud (b,x,y)-> let p,q = binar x, binar y in ajouter_0 p @ ajouter_1 q ;;
let to_int u =
let rec aux u q s = match u with
|[]-> s
|x::r -> aux r (2*q) (s+x*q)
in aux u 1 0 ;;
let elements a = List.map to_int (binar a) ;;
let rec elague a =
let aux a = match a with
|Nil -> Nil
|Noeud (false,Nil,Nil) -> Nil
|Noeud (b,x,y) -> Noeud (b,elague x ,elague y)
in if a<>aux a then elague (aux a) else a ;;