lib/rambling/trie/compressor.rb in rambling-trie-0.5.2 vs lib/rambling/trie/compressor.rb in rambling-trie-0.6.0

- old
+ new

@@ -9,37 +9,41 @@ end # Compress the current node using redundant node elimination. # @return [Root, Node] the compressed node. def compress_tree! - if children.size == 1 && !terminal? && letter - merge_with! children.values.first + if compressable? + merge_with! children.first compress_tree! end - children.values.each &:compress_tree! + children.each &:compress_tree! self end private + def compressable? + !(root? || terminal?) && children_tree.size == 1 + end + def merge_with!(child) delete_old_key_on_parent! redefine_self! child - children.each { |_, node| node.parent = self } + children.each { |node| node.parent = self } end def delete_old_key_on_parent! return if parent.nil? parent.delete letter end def redefine_self!(merged_node) self.letter = letter.to_s << merged_node.letter.to_s - self.children = merged_node.children + self.children_tree = merged_node.children_tree self.terminal = merged_node.terminal? end end end end