{-# OPTIONS_GHC -fno-warn-incomplete-uni-patterns #-}
module I1M.ArbolBin
(ABB,
vacio,
inserta,
elimina,
crea,
crea',
menor,
elementos,
pertenece,
valido
) where
data ABB a = Vacio
| Nodo a (ABB a) (ABB a)
deriving ABB a -> ABB a -> Bool
(ABB a -> ABB a -> Bool) -> (ABB a -> ABB a -> Bool) -> Eq (ABB a)
forall a. Eq a => ABB a -> ABB a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a. Eq a => ABB a -> ABB a -> Bool
== :: ABB a -> ABB a -> Bool
$c/= :: forall a. Eq a => ABB a -> ABB a -> Bool
/= :: ABB a -> ABB a -> Bool
Eq
instance (Show a, Ord a) => Show (ABB a) where
show :: ABB a -> String
show ABB a
Vacio = String
" -"
show (Nodo a
x ABB a
i ABB a
d) = String
" (" String -> ShowS
forall a. [a] -> [a] -> [a]
++ a -> String
forall a. Show a => a -> String
show a
x String -> ShowS
forall a. [a] -> [a] -> [a]
++ ABB a -> String
forall a. Show a => a -> String
show ABB a
i String -> ShowS
forall a. [a] -> [a] -> [a]
++ ABB a -> String
forall a. Show a => a -> String
show ABB a
d String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
")"
vacio :: ABB a
vacio :: forall a. ABB a
vacio = ABB a
forall a. ABB a
Vacio
pertenece :: (Ord a,Show a) => a -> ABB a -> Bool
pertenece :: forall a. (Ord a, Show a) => a -> ABB a -> Bool
pertenece a
_ ABB a
Vacio = Bool
False
pertenece a
v' (Nodo a
v ABB a
i ABB a
d)
| a
v a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
v' = Bool
True
| a
v' a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
v = a -> ABB a -> Bool
forall a. (Ord a, Show a) => a -> ABB a -> Bool
pertenece a
v' ABB a
i
| Bool
otherwise = a -> ABB a -> Bool
forall a. (Ord a, Show a) => a -> ABB a -> Bool
pertenece a
v' ABB a
d
inserta :: (Ord a,Show a) => a -> ABB a -> ABB a
inserta :: forall a. (Ord a, Show a) => a -> ABB a -> ABB a
inserta a
v' ABB a
Vacio = a -> ABB a -> ABB a -> ABB a
forall a. a -> ABB a -> ABB a -> ABB a
Nodo a
v' ABB a
forall a. ABB a
Vacio ABB a
forall a. ABB a
Vacio
inserta a
v' (Nodo a
v ABB a
i ABB a
d)
| a
v' a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
v = a -> ABB a -> ABB a -> ABB a
forall a. a -> ABB a -> ABB a -> ABB a
Nodo a
v ABB a
i ABB a
d
| a
v' a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
v = a -> ABB a -> ABB a -> ABB a
forall a. a -> ABB a -> ABB a -> ABB a
Nodo a
v (a -> ABB a -> ABB a
forall a. (Ord a, Show a) => a -> ABB a -> ABB a
inserta a
v' ABB a
i) ABB a
d
| Bool
otherwise = a -> ABB a -> ABB a -> ABB a
forall a. a -> ABB a -> ABB a -> ABB a
Nodo a
v ABB a
i (a -> ABB a -> ABB a
forall a. (Ord a, Show a) => a -> ABB a -> ABB a
inserta a
v' ABB a
d)
crea :: (Ord a,Show a) => [a] -> ABB a
crea :: forall a. (Ord a, Show a) => [a] -> ABB a
crea = (a -> ABB a -> ABB a) -> ABB a -> [a] -> ABB a
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr a -> ABB a -> ABB a
forall a. (Ord a, Show a) => a -> ABB a -> ABB a
inserta ABB a
forall a. ABB a
Vacio
crea' :: (Ord a,Show a) => [a] -> ABB a
crea' :: forall a. (Ord a, Show a) => [a] -> ABB a
crea' [] = ABB a
forall a. ABB a
Vacio
crea' [a]
vs = a -> ABB a -> ABB a -> ABB a
forall a. a -> ABB a -> ABB a -> ABB a
Nodo a
x ([a] -> ABB a
forall a. (Ord a, Show a) => [a] -> ABB a
crea' [a]
l1) ([a] -> ABB a
forall a. (Ord a, Show a) => [a] -> ABB a
crea' [a]
l2)
where n :: Int
n = [a] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
vs Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
2
l1 :: [a]
l1 = Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
take Int
n [a]
vs
(a
x:[a]
l2) = Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
drop Int
n [a]
vs
elementos :: (Ord a,Show a) => ABB a -> [a]
elementos :: forall a. (Ord a, Show a) => ABB a -> [a]
elementos ABB a
Vacio = []
elementos (Nodo a
v ABB a
i ABB a
d) = ABB a -> [a]
forall a. (Ord a, Show a) => ABB a -> [a]
elementos ABB a
i [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ [a
v] [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ ABB a -> [a]
forall a. (Ord a, Show a) => ABB a -> [a]
elementos ABB a
d
elimina :: (Ord a,Show a) => a -> ABB a -> ABB a
elimina :: forall a. (Ord a, Show a) => a -> ABB a -> ABB a
elimina a
_ ABB a
Vacio = ABB a
forall a. ABB a
Vacio
elimina a
v' (Nodo a
v ABB a
i ABB a
Vacio) | a
v'a -> a -> Bool
forall a. Eq a => a -> a -> Bool
==a
v = ABB a
i
elimina a
v' (Nodo a
v ABB a
Vacio ABB a
d) | a
v'a -> a -> Bool
forall a. Eq a => a -> a -> Bool
==a
v = ABB a
d
elimina a
v' (Nodo a
v ABB a
i ABB a
d)
| a
v' a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
v = a -> ABB a -> ABB a -> ABB a
forall a. a -> ABB a -> ABB a -> ABB a
Nodo a
v (a -> ABB a -> ABB a
forall a. (Ord a, Show a) => a -> ABB a -> ABB a
elimina a
v' ABB a
i) ABB a
d
| a
v' a -> a -> Bool
forall a. Ord a => a -> a -> Bool
> a
v = a -> ABB a -> ABB a -> ABB a
forall a. a -> ABB a -> ABB a -> ABB a
Nodo a
v ABB a
i (a -> ABB a -> ABB a
forall a. (Ord a, Show a) => a -> ABB a -> ABB a
elimina a
v' ABB a
d)
| Bool
otherwise = a -> ABB a -> ABB a -> ABB a
forall a. a -> ABB a -> ABB a -> ABB a
Nodo a
k ABB a
i (a -> ABB a -> ABB a
forall a. (Ord a, Show a) => a -> ABB a -> ABB a
elimina a
k ABB a
d)
where k :: a
k = ABB a -> a
forall a. Ord a => ABB a -> a
menor ABB a
d
menor :: Ord a => ABB a -> a
menor :: forall a. Ord a => ABB a -> a
menor (Nodo a
v ABB a
Vacio ABB a
_) = a
v
menor (Nodo a
_ ABB a
i ABB a
_) = ABB a -> a
forall a. Ord a => ABB a -> a
menor ABB a
i
menor ABB a
Vacio = String -> a
forall a. HasCallStack => String -> a
error String
"No tiene"
menorTodos :: (Ord a, Show a) => a -> ABB a -> Bool
menorTodos :: forall a. (Ord a, Show a) => a -> ABB a -> Bool
menorTodos a
_ ABB a
Vacio = Bool
True
menorTodos a
v ABB a
a = a
v a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< [a] -> a
forall a. Ord a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
minimum (ABB a -> [a]
forall a. (Ord a, Show a) => ABB a -> [a]
elementos ABB a
a)
mayorTodos :: (Ord a, Show a) => a -> ABB a -> Bool
mayorTodos :: forall a. (Ord a, Show a) => a -> ABB a -> Bool
mayorTodos a
_ ABB a
Vacio = Bool
True
mayorTodos a
v ABB a
a = a
v a -> a -> Bool
forall a. Ord a => a -> a -> Bool
> [a] -> a
forall a. Ord a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
maximum (ABB a -> [a]
forall a. (Ord a, Show a) => ABB a -> [a]
elementos ABB a
a)
valido :: (Ord a, Show a) => ABB a -> Bool
valido :: forall a. (Ord a, Show a) => ABB a -> Bool
valido ABB a
Vacio = Bool
True
valido (Nodo a
v ABB a
a ABB a
b) =
a -> ABB a -> Bool
forall a. (Ord a, Show a) => a -> ABB a -> Bool
mayorTodos a
v ABB a
a Bool -> Bool -> Bool
&&
a -> ABB a -> Bool
forall a. (Ord a, Show a) => a -> ABB a -> Bool
menorTodos a
v ABB a
b Bool -> Bool -> Bool
&&
ABB a -> Bool
forall a. (Ord a, Show a) => ABB a -> Bool
valido ABB a
a Bool -> Bool -> Bool
&&
ABB a -> Bool
forall a. (Ord a, Show a) => ABB a -> Bool
valido ABB a
b