 //
 
 import Foundation
-
-class HuffmanTreeNode: CustomStringConvertible {
-    let node: HuffmanLength?
-    var left: HuffmanTreeNode? = nil
-    var right: HuffmanTreeNode? = nil
-
-    var description: String {
-        return "value: \(self.node)\n\tleft: \(left)\n\tright: \(right)"
-    }
-
-    init(_ node: HuffmanLength? = nil) {
-        self.node = node
-    }
-}
 
 class HuffmanTable: CustomStringConvertible {
 

            self.lengths[index].reversedSymbol = reverse(bits: loopBits, in: symbol)
        }


        let leafCount = Int(pow(Double(2), Double(self.lengths.first!.bits)))
        var treeLengths: [HuffmanLength?] = Array(repeating: nil, count: leafCount)
        var reversedTreeLengths: [HuffmanLength?] = Array(repeating: nil, count: leafCount)
//        self.lengths.sorted(by: { $0.symbol < $1.symbol! })
//        treeLengths.append(contentsOf: )
        var sortedLengths = self.lengths.sorted(by: { $0.symbol! < $1.symbol! })
        for length in sortedLengths {
            treeLengths.append(length)
        }
        sortedLengths = self.lengths.sorted(by: { $0.reversedSymbol! < $1.reversedSymbol! })
        for length in sortedLengths {
            reversedTreeLengths.append(length)
        }

        self.tree = treeLengths
        self.reversedTree = reversedTreeLengths
        print()
    }

    convenience init(lengthsToOrder: [Int]) {

        }

        // Calculates symbol and reversedSymbol properties for each length in 'lengths' array
        var loopBits = -1
        var symbol = -1
        for index in 0..<self.lengths.count {
            symbol += 1
            let length = self.lengths[index]
            // We sometimes need to make symbol to have length.bits bit length
            if length.bits != loopBits {
                symbol <<= (length.bits - loopBits)
                loopBits = length.bits
