def clusters_to_tree(clusters):
  """Converts clusters to trees for string matching.

  Converts a list of events (ssh *HVRY% connect, ssh *HVRY% disconnect)
  to a tree (ssh -> *HVRY -> [connect, disconnect])

  Args:
    clusters: a dictionary of events to event data.

  Returns:
    A tree (nested dictionaries) of all the possible events
    Top level key is the length of the line.
  """

  tree = {}
  for event in clusters.keys():
    length = clusters[event]['line_len']
    branch = tree[length] = tree.get(length, {})
    for token in event.split():
      entry = branch[token] = branch.get(token, {})
      branch = entry
  return tree