module Huffman where

--import qualified Data.List as List

import qualified Data.Map.Strict as Map
import Auxiliaries -- defines Bit
type Map = Map.Map
type CodingTable = Map Char [Bit]

-----------------------------------------------------------------------
-- Aufgabe 2
-----------------------------------------------------------------------

data Node = Leaf Char Integer | Inner Node Node Integer
data HTree = Root Node

exercise1TreeValid :: HTree
exercise1TreeValid = Root (Inner (Inner (Leaf 'E' 158) (Inner (Leaf 'N' 97) (Leaf 'I' 82) 179) 337) (Inner (Inner (Leaf 'R' 77) (Leaf 'S' 67) 144) (Inner (Leaf 'T' 64) (Leaf 'A' 61) 125) 269) 606)

exercise1TreeInvalid :: HTree
exercise1TreeInvalid = 
    Root (Inner (Leaf 'A' 21) (Leaf 'B' 20) 42)

-----------------------------------------------------------------------
-- Aufgabe 3
-----------------------------------------------------------------------

isConsistent :: HTree -> Bool
isConsistent (Root node) =
    case node of
        Leaf _ _ -> True
        Inner left right frequency ->
            case left of
                Leaf _ leftFrequency ->
                    case right of
                        Leaf _ rightFrequency -> frequency == leftFrequency + rightFrequency
                        Inner _ _ rightFrequency -> frequency == leftFrequency + rightFrequency && isConsistent (Root right)
                Inner _ _ leftFrequency ->
                    case right of
                        Leaf _ rightFrequency -> frequency == leftFrequency + rightFrequency && isConsistent (Root left)
                        Inner _ _ rightFrequency -> frequency == leftFrequency + rightFrequency && isConsistent (Root left) && isConsistent (Root right)

-----------------------------------------------------------------------
-- Aufgabe 4
-----------------------------------------------------------------------
toCodingTable :: HTree -> CodingTable
toCodingTable (Root node) =
    case node of
        Leaf char _ -> singleton char [Zero]
        Inner _ _ _ -> fromList toCodingTableList (Root node)

toCodingTableList :: HTree -> [(Char,[Bit])]
toCodingTableList (Root node) =
    case node of
        Leaf char _ -> [(char,[Zero])]
        Inner left right frequency -> 

--type Map = Map.Map
--type CodingTable = Map Char [Bit]