module I1M.Monticulo
(Monticulo,
vacio,
inserta,
menor,
resto,
esVacio,
valido
) where
import Data.List (sort)
data Monticulo a = Vacio
| M a Int (Monticulo a) (Monticulo a)
deriving Int -> Monticulo a -> ShowS
[Monticulo a] -> ShowS
Monticulo a -> String
(Int -> Monticulo a -> ShowS)
-> (Monticulo a -> String)
-> ([Monticulo a] -> ShowS)
-> Show (Monticulo a)
forall a. Show a => Int -> Monticulo a -> ShowS
forall a. Show a => [Monticulo a] -> ShowS
forall a. Show a => Monticulo a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Monticulo a] -> ShowS
$cshowList :: forall a. Show a => [Monticulo a] -> ShowS
show :: Monticulo a -> String
$cshow :: forall a. Show a => Monticulo a -> String
showsPrec :: Int -> Monticulo a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> Monticulo a -> ShowS
Show
vacio :: Ord a => Monticulo a
vacio :: Monticulo a
vacio = Monticulo a
forall a. Monticulo a
Vacio
rango :: Ord a => Monticulo a -> Int
rango :: Monticulo a -> Int
rango Monticulo a
Vacio = Int
0
rango (M a
_ Int
r Monticulo a
_ Monticulo a
_) = Int
r
creaM :: Ord a => a -> Monticulo a -> Monticulo a -> Monticulo a
creaM :: a -> Monticulo a -> Monticulo a -> Monticulo a
creaM a
x Monticulo a
a Monticulo a
b | Monticulo a -> Int
forall a. Ord a => Monticulo a -> Int
rango Monticulo a
a Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Monticulo a -> Int
forall a. Ord a => Monticulo a -> Int
rango Monticulo a
b = a -> Int -> Monticulo a -> Monticulo a -> Monticulo a
forall a. a -> Int -> Monticulo a -> Monticulo a -> Monticulo a
M a
x (Monticulo a -> Int
forall a. Ord a => Monticulo a -> Int
rango Monticulo a
b Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Monticulo a
a Monticulo a
b
| Bool
otherwise = a -> Int -> Monticulo a -> Monticulo a -> Monticulo a
forall a. a -> Int -> Monticulo a -> Monticulo a -> Monticulo a
M a
x (Monticulo a -> Int
forall a. Ord a => Monticulo a -> Int
rango Monticulo a
a Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Monticulo a
b Monticulo a
a
mezcla :: Ord a => Monticulo a -> Monticulo a -> Monticulo a
mezcla :: Monticulo a -> Monticulo a -> Monticulo a
mezcla Monticulo a
m Monticulo a
Vacio = Monticulo a
m
mezcla Monticulo a
Vacio Monticulo a
m = Monticulo a
m
mezcla m1 :: Monticulo a
m1@(M a
x Int
_ Monticulo a
a1 Monticulo a
b1) m2 :: Monticulo a
m2@(M a
y Int
_ Monticulo a
a2 Monticulo a
b2)
| a
x a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
y = a -> Monticulo a -> Monticulo a -> Monticulo a
forall a. Ord a => a -> Monticulo a -> Monticulo a -> Monticulo a
creaM a
x Monticulo a
a1 (Monticulo a -> Monticulo a -> Monticulo a
forall a. Ord a => Monticulo a -> Monticulo a -> Monticulo a
mezcla Monticulo a
b1 Monticulo a
m2)
| Bool
otherwise = a -> Monticulo a -> Monticulo a -> Monticulo a
forall a. Ord a => a -> Monticulo a -> Monticulo a -> Monticulo a
creaM a
y Monticulo a
a2 (Monticulo a -> Monticulo a -> Monticulo a
forall a. Ord a => Monticulo a -> Monticulo a -> Monticulo a
mezcla Monticulo a
m1 Monticulo a
b2)
inserta :: Ord a => a -> Monticulo a -> Monticulo a
inserta :: a -> Monticulo a -> Monticulo a
inserta a
x = Monticulo a -> Monticulo a -> Monticulo a
forall a. Ord a => Monticulo a -> Monticulo a -> Monticulo a
mezcla (a -> Int -> Monticulo a -> Monticulo a -> Monticulo a
forall a. a -> Int -> Monticulo a -> Monticulo a -> Monticulo a
M a
x Int
1 Monticulo a
forall a. Monticulo a
Vacio Monticulo a
forall a. Monticulo a
Vacio)
menor :: Ord a => Monticulo a -> a
menor :: Monticulo a -> a
menor (M a
x Int
_ Monticulo a
_ Monticulo a
_) = a
x
menor Monticulo a
Vacio = String -> a
forall a. HasCallStack => String -> a
error String
"menor: monticulo vacio"
resto :: Ord a => Monticulo a -> Monticulo a
resto :: Monticulo a -> Monticulo a
resto Monticulo a
Vacio = String -> Monticulo a
forall a. HasCallStack => String -> a
error String
"resto: monticulo vacio"
resto (M a
_ Int
_ Monticulo a
a Monticulo a
b) = Monticulo a -> Monticulo a -> Monticulo a
forall a. Ord a => Monticulo a -> Monticulo a -> Monticulo a
mezcla Monticulo a
a Monticulo a
b
esVacio :: Ord a => Monticulo a -> Bool
esVacio :: Monticulo a -> Bool
esVacio Monticulo a
Vacio = Bool
True
esVacio Monticulo a
_ = Bool
False
valido :: Ord a => Monticulo a -> Bool
valido :: Monticulo a -> Bool
valido Monticulo a
Vacio = Bool
True
valido (M a
_ Int
_ Monticulo a
Vacio Monticulo a
Vacio) = Bool
True
valido (M a
x Int
_ m1 :: Monticulo a
m1@(M a
x1 Int
_ Monticulo a
_ Monticulo a
_) Monticulo a
Vacio) =
a
x a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
x1 Bool -> Bool -> Bool
&& Monticulo a -> Bool
forall a. Ord a => Monticulo a -> Bool
valido Monticulo a
m1
valido (M a
x Int
_ Monticulo a
Vacio m2 :: Monticulo a
m2@(M a
x2 Int
_ Monticulo a
_ Monticulo a
_)) =
a
x a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
x2 Bool -> Bool -> Bool
&& Monticulo a -> Bool
forall a. Ord a => Monticulo a -> Bool
valido Monticulo a
m2
valido (M a
x Int
_ m1 :: Monticulo a
m1@(M a
x1 Int
_ Monticulo a
_ Monticulo a
_) m2 :: Monticulo a
m2@(M a
x2 Int
_ Monticulo a
_ Monticulo a
_)) =
a
x a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
x1 Bool -> Bool -> Bool
&& Monticulo a -> Bool
forall a. Ord a => Monticulo a -> Bool
valido Monticulo a
m1 Bool -> Bool -> Bool
&&
a
x a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
x2 Bool -> Bool -> Bool
&& Monticulo a -> Bool
forall a. Ord a => Monticulo a -> Bool
valido Monticulo a
m2
elementos :: Ord a => Monticulo a -> [a]
elementos :: Monticulo a -> [a]
elementos Monticulo a
Vacio = []
elementos (M a
x Int
_ Monticulo a
a Monticulo a
b) = a
x a -> [a] -> [a]
forall a. a -> [a] -> [a]
: Monticulo a -> [a]
forall a. Ord a => Monticulo a -> [a]
elementos Monticulo a
a [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ Monticulo a -> [a]
forall a. Ord a => Monticulo a -> [a]
elementos Monticulo a
b
equivMonticulos :: Ord a => Monticulo a -> Monticulo a -> Bool
equivMonticulos :: Monticulo a -> Monticulo a -> Bool
equivMonticulos Monticulo a
m1 Monticulo a
m2 =
[a] -> [a]
forall a. Ord a => [a] -> [a]
sort (Monticulo a -> [a]
forall a. Ord a => Monticulo a -> [a]
elementos Monticulo a
m1) [a] -> [a] -> Bool
forall a. Eq a => a -> a -> Bool
== [a] -> [a]
forall a. Ord a => [a] -> [a]
sort (Monticulo a -> [a]
forall a. Ord a => Monticulo a -> [a]
elementos Monticulo a
m2)
instance Ord a => Eq (Monticulo a) where
== :: Monticulo a -> Monticulo a -> Bool
(==) = Monticulo a -> Monticulo a -> Bool
forall a. Ord a => Monticulo a -> Monticulo a -> Bool
equivMonticulos