-
Notifications
You must be signed in to change notification settings - Fork 0
/
bintree-datatype.rkt
207 lines (148 loc) · 6.23 KB
/
bintree-datatype.rkt
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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#lang eopl
;Datatypes
(define-datatype bintree bintree?
(empty-tree (nv null?) )
(node (key number?)
(left bintree?)
(right bintree?)))
;Empty-bintree
(define empty-bintree '())
;;AUXXXXXXXXXX FUNCCCCCCCCCCCTSSSSSSSSSSS
;permite visualizar un árbol en formato de listas (unparse)
(define view-tree
(lambda (bt)
(cases bintree bt
(empty-tree (lv) '())
(node (key left right) (list key (view-tree left) (view-tree right) ) )
)
)
)
;permite convertir un arbol en formato de listas a tipo de dato(?
(define parse (lambda (bt) (cond [(equal? bt '()) (empty-tree '()) ]
[else (node (car bt) (parse (car (cdr bt))) (parse (car (cdr (cdr bt))) ) )]
)))
;current-element
(define current-element (lambda (bt)
(cases bintree bt
(empty-tree (lv) '())
(node (key left right) key )
)
))
;move-to-left-son
(define move-to-left-son (lambda (bt)
(cases bintree bt
(empty-tree (lv) '())
(node (key left right) (view-tree left))
)
))
;move-to-right-son
(define move-to-right-son (lambda (bt)
(cases bintree bt
(empty-tree (lv) '())
(node (key left right) (view-tree left))
)
))
;number->bintree
(define number->bintree
(lambda (n)
(node n (empty-tree empty-bintree) (empty-tree empty-bintree)
))
)
;empty-bintree?
(define empty-bintree?
(lambda (bt)
(cases bintree bt
(empty-tree (lv) #t)
(node (key left right) #f))))
;at-leaf?
(define at-leaf?
(lambda (bt)
(cases bintree bt
(empty-tree (lv) #f )
(node (key left right) (cond [(and (empty-bintree? left) (empty-bintree? right)) #t ]
[else #f]) )
)
)
)
;bintree-with-at-least-one-child?
(define bintree-with-at-least-one-child?
(lambda (bt)
(cases bintree bt
(empty-tree (lv) #f)
(node (key left right) (cond [(and (empty-bintree? left) (empty-bintree? right)) #f]
[(or (bintree? left) (bintree? right) ) #t]
) )
)
)
)
;insert-to-left (insert-to-left )
(define insert-to-left
(lambda (n bt)
(cases bintree bt
(empty-tree (lv) (node n empty-bintree empty-bintree ) )
(node (key left right)
(cond [(empty-bintree? left ) (view-tree (node key (number->bintree n) right )) ]
[else (node key (insert-to-left left) right) ]
)
)
)
)
)
;insert-to-right (insert-to-right )
(define insert-to-right
(lambda (n bt)
(cases bintree bt
(empty-tree (lv) (node n empty-bintree empty-bintree ) )
(node (key left right)
(cond [(empty-bintree? right ) (view-tree (node key left (number->bintree n) )) ]
[else (node key left (insert-to-right right)) ]
)
)
)
)
)
;bintree-order-validation
(define bintree-order-validation
(lambda (bt)
(cases bintree bt
(empty-tree (lv) #t)
(node (key left right) (cond
[(and (cases bintree left
(empty-tree (lv) #t)
(node (key1 left1 right1) #f)) (cases bintree right
(empty-tree (lv) #t)
(node (key1 left1 right1) #f)) ) #t ]
[(and (< (cases bintree left
(empty-tree (lv) -10000)
(node (key1 left1 right1) key1 )
) key) (> (cases bintree right
(empty-tree (lv) 10000)
(node (key1 left1 right1) key1 )
) key)) (and (bintree-order-validation left ) (bintree-order-validation right ) )]
[else #f]
)
)
)
))
;insert-element-into-bintree ;lamasputaaa
(define insert-element-into-bintree
(lambda (n bt)
(cases bintree bt
(empty-tree (lv) (number->bintree n))
(node (key left right) (cond
[(< n key) (node key (insert-element-into-bintree n left ) right) ]
[(> n key) (node key left (insert-element-into-bintree n right)) ]
)
))))
;
(define tree1 (node 3
(node 2 (empty-tree '() ) (empty-tree '() ))
(node 4 (empty-tree '() ) (empty-tree '() ) ) ) )
(define emptree (empty-tree empty-bintree ))
(define leaftree (node 1 (empty-tree '() ) (empty-tree '() ) ))
(define wftree (node 6 (node 5 (node 4 (empty-tree '() ) (empty-tree '() ) ) (empty-tree '() ) ) (node 8 (empty-tree '() ) (empty-tree '() )) ))
(define bftree (node 6 (node 9 (empty-tree '() ) (empty-tree '() ) ) (node 8 (empty-tree '() ) (empty-tree '() )) ))
(define simptree (node 1 (empty-tree '() ) (empty-tree '() )))
(define Arbol_Ejemplo (node 8 (node 3 (node 1 (empty-tree '() ) (empty-tree '() )) (node 6 (node 4 (empty-tree '() ) (empty-tree '() )) (node 7 (empty-tree '() ) (empty-tree '() )))) (node 10 (empty-tree '() ) (node 14 (node 13 (empty-tree '() ) (empty-tree '() )) (empty-tree '() )))))
;(8 (3 (1 () ()) (6 (4 () ()) (7 () ()))) (10 () (14 (13 () ()) ())))
;pregunta para Juamarcos: ¿La inserción tiene algún tipo de orden o restricción?