     }
 
     func findNextSymbol(in bitArray: [UInt8], reversed: Bool = true, bitOrder: BitOrder = .reversed) -> HuffmanLength? {
-        var cachedLength = -1
-        var cached: Int = -1
+//        var cachedLength = -1
+//        var cached: Int = -1
+//
+//        for length in self.lengths {
+//            let lbits = length.bits
+//
+//            if cachedLength != lbits {
+//                cached = convertToInt(uint8Array: Array(bitArray[0..<lbits]), bitOrder: bitOrder)
+//                cachedLength = lbits
+//            }
+//            if (reversed && length.reversedSymbol == cached) ||
+//                (!reversed && length.symbol == cached) {
+//                return length
+//            }
+//        }
+//        return nil
+        var node = reversed ? self.reversedTreeRoot : self.treeRoot
+        for (bitIndex, bit) in bitArray.enumerated() {
+            let newNode: HuffmanTreeNode?
+            if bit == 0 {
+                print("right")
+                newNode = node.right
+            } else {
+                print("left")
+                newNode = node.left
+            }
 
-        for length in self.lengths {
-            let lbits = length.bits
-
-            if cachedLength != lbits {
-                cached = convertToInt(uint8Array: Array(bitArray[0..<lbits]), bitOrder: bitOrder)
-                cachedLength = lbits
+            guard newNode != nil else {
+                return node.node
             }
-            if (reversed && length.reversedSymbol == cached) ||
-                (!reversed && length.symbol == cached) {
-                return length
+            if newNode?.node == nil {
+                return node.node
             }
+            node = newNode!
         }
         return nil
     }

            parents.append(treeParent.right!)
            parents.append(reversedTreeParent.right!)
        }
    }

    convenience init(lengthsToOrder: [Int]) {


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
