def _mk_encode_tree(self): """Constructs the encoding tree form the heap containing _Node objects. Returns the first node of the tree.""" freq_heap = MinHeap.from_iterable(self.freq_list) while len(freq_heap) > 1: # get the nodes with the smallest frequency a = freq_heap.remove() b = freq_heap.remove() # make the new node and add it in it's proper position new_node = TreeNode(a.freq + b.freq, content=None) new_node.lchild = a new_node.rchild = b freq_heap.insert(new_node) return freq_heap.remove()