def CompactArrayIndices(tree):
  """Makes a sparse tree dense.

  Returns a tree that may have been sparse, with its indices now compacted.
  Eg 'a.b.0.c', 'a.b.5.c' -> 'a.b.0.c', 'a.b.1.c'.

  Args:
      tree: or store. multi-level dict representing proto dbroot.
  Returns:
      The tree, compacted.
  """
  new_tree = {}
  index = 0
  for key, rest in sorted(tree.iteritems()):
    if key.isdigit():
      key = "%06d" % index
      index += 1
    if isinstance(rest, dict):
      new_tree[key] = CompactArrayIndices(rest)
    else:
      # primitive
      new_tree[key] = rest

  return new_tree