Professional Documents
Culture Documents
Hoje
!
rvores binrias:
!
!
especificao
implementao utilizando ns ligados entre si
Fac. Cincias
Univ. Lisboa
rvores
!
LINEARES, ou HIERRQUICOS
Fac. Cincias
Univ. Lisboa
Office
Templates
Netscape
Chrome
Cool
Plugins
Fac. Cincias
Univ. Lisboa
rvores binrias
!
Fac. Cincias
Univ. Lisboa
Terminologia
! Uma rvore uma coleco de ns, cada um ligado aos seus
sucessores.
Os outros so ns internos
Luis Antunes Algoritmos e Estruturas de Dados
Fac. Cincias
Univ. Lisboa
Mais terminologia
! Uma generalizao da relao pai-filho o ANTEPASSADO-
DESCENDENTE
!
desse n.
Fac. Cincias
Univ. Lisboa
Exemplo
dog
Filho de dog, pai de canine
cat
e irmo de wolf
Nvel 1
wolf
Nvel 2
canine
Sub-rvore de dog
Sub-rvore de cat
Fac. Cincias
Univ. Lisboa
rvores binrias
! Um conjunto de ns T uma rvore binria se um dos
Fac. Cincias
Univ. Lisboa
No exemplo
= rvore vazia
dog
cat
wolf
canine
Fac. Cincias
Univ. Lisboa
Expresso algbrica
! (x + y) * ((a + b) / c)
*
Fac. Cincias
Univ. Lisboa
Travessias
! Uma travessia consiste em visitar (e possvelmente
! So usuais:
!
Infixa
!
Prefixa
!
Sufixa
! E ainda
!
Em profundidade primeiro
!
Em largura primeiro
Fac. Cincias
Univ. Lisboa
Travessias
!
Travessia prefixa:
!
Travessia infixa:
!
Travessia sufixa:
!
Fac. Cincias
Univ. Lisboa
Algoritmo travessia
!
Prefixa
Visita a raiz
Travessia prefixa da subrvore esquerda
Fac. Cincias
Univ. Lisboa
Fac. Cincias
Univ. Lisboa
Fac. Cincias
Univ. Lisboa
rvores de expresses
*
Colocando os parntesis d
(x + y) * ((a + b) / c)
Travessia sufixa: x y + a b + c / *
Travessia prefixa: * + x y / + a b c
+
x
+
a
c
b
Fac. Cincias
Univ. Lisboa
40
30
32
30
55
32
55
35
50
Fac. Cincias
Univ. Lisboa
Fac. Cincias
Univ. Lisboa
especificao
specification
sorts
B i n a r yT r e e
constructors
empty : > B i n a r y T r e e ;
make : Element B i n a r yT r e e B i n a r yT r e e > B i n a r y T r e e ;
observers
isEmpty : B i n a r y T r e e ;
r o o t : Bi n a r y T r e e >? Element ;
l e f t S u b t r e e : B i n a r yT r e e >? B i n a r yT r e e ;
r i g h t S u b t r e e : B i n a r y T re e >? B i n a r y Tr e e ;
others
i s L e a f : B i n ar y T r e e ;
h e i g h t : B i n ar y T r e e > i n t ;
i s Ba l a n c ed : B i n a r y T re e ;
domains
T : BinaryTree ;
r o o t ( T ) i f not isEmpty ( T ) ;
l e f t S u b t r e e ( T ) i f not isEmpty ( T ) ;
r i g h t S u b t r e e ( T ) i f not isEmpty ( T ) ;
axioms
T1 , T2 : B i na r y T r e e ; X : Element
;
Luis Antunes
Algoritmos e Estruturas de Dados
isEmpty ( empty ( ) ) ;
not isEmpty ( make ( X , T1 , T2 ) ) ;
r o o t ( make ( X , T1 , T2 ) ) = X ;
Fac. Cincias
Univ. Lisboa
h e i g h t : B i n ar y T r e e > i n t ;
i s Ba l a n c ed : B i n a r y T re e ;
domains
T : BinaryTree ;
r o o t ( T ) i f not isEmpty ( T ) ;
l e f t S u b t r e e ( T ) i f not isEmpty ( T ) ;
r i g h t S u b t r e e ( T ) i f not isEmpty ( T ) ;
axioms
T1 , T2 : B i na r y T r e e ; X : Element ;
isEmpty ( empty ( ) ) ;
not isEmpty ( make ( X , T1 , T2 ) ) ;
r o o t ( make ( X , T1 , T2 ) ) = X ;
l e f t S u b t r e e ( make ( X , T1 , T2 ) ) = T1 ;
r i g h t S u b t r e e ( make ( X , T1 , T2 ) ) = T2 ;
i s L e a f ( T ) i f f not isEmpty ( T ) and
isEmpty ( l e f t S u b t r e e ( T ) ) and
isEmpty ( r i g h t S u b t r e e ( T ) ) ;
h e i g h t ( empty ( ) ) = 0 ;
h e i g h t ( make ( X , T1 , T2 ) ) =
1 + max ( h e i g h t ( T1 ) , h e i g h t ( T2 ) ) ;
i s Ba l a n c ed ( empty ( ) ) ;
i s Ba l a n c ed ( make ( X , T1 , T2 ) ) i f
i s Ba l a n c ed ( T1 ) and i s B al a n c ed ( T2 ) and
abs ( h e i g h t ( T1 ) h e i g h t ( T2 ) ) <= 1 ;
end s p e c i f i c a t i o n
especificao
Fac. Cincias
Univ. Lisboa
refinement
refinement
Element i s class O b j e c t
B i n a r y T r e e i s class L i n k e d B i n a r y T r e e {
empty : > B i n a r y T r e e i s L i n k e d B i n a r y T r e e ( ) ;
compound : e : Element l : B i n a r y T r e e r : B i n a r y T r e e >
BinaryTree is LinkedBinaryTree ( Object e ,
LinkedBinaryTree l , LinkedBinaryTree r ) ;
r o o t : B i n a r y T r e e >? Element i s O b j e c t data ( ) ;
l e f t S u b t r e e : B i n a r y T r e e >? r e t u r n : B i n a r y T r e e i s
LinkedBinaryTree l e f t S u b t r e e ( ) ;
r i g h t S u b t r e e : B i n a r y T r e e >? r e t u r n : B i n a r y T r e e i s
LinkedBinaryTree r i g h t S u b t r e e ( ) ;
isEmpty : B i n a r y T r e e i s boolean isEmpty ( ) ;
i s L e a f : B i n a r y T r e e i s boolean i s L e a f ( ) ;
h e i g h t : B i n a r y T r e e > i n t i s i n t h e i g h t ( ) ;
i s B a l a n c e d : B i n a r y T r e e i s boolean i s B a l a n c e d ( ) ;
}
end refinement
Fac. Cincias
Univ. Lisboa
Classe imutvel
! A operaes no-construtoras que devolvem um gnero
Fac. Cincias
Univ. Lisboa