code: def replace_tags(tree, tags):
      if tree.is_leaf():
          raise ValueError("Must call replace_tags with non-leaf")
  
      tag_iterator = iter(tags)
  
      new_tree = copy.deepcopy(tree)
      queue = deque()
      queue.append(new_tree)
      while len(queue) > 0:
          next_node = queue.pop()
          if next_node.is_preterminal():
              try:
                  label = next(tag_iterator)
              except StopIteration:
                  raise ValueError(
                      "Not enough tags in sentence for given tree"
                  )
              next_node.label = label
          elif next_node.is_leaf():
              raise ValueError(
                  "Got a badly structured tree: {}".format(tree)
              )
          else:
              queue.extend(reversed(next_node.children))
  
      if any(True for _ in tag_iterator):
          raise ValueError("Too many tags for the given tree")
  
      return new_tree
  
file_path: stanza/stanza/models/constituency/utils.py
imports: from collections import Counter, deque
  import copy
  import logging
  import torch.nn
  from torch import optim
  from stanza.models.common.doc import TEXT, Document
  from stanza.utils.get_tqdm import get_tqdm
label_desc: Use image node_id 1 for calling a global function with example usage: replace_tags(tree, tags) and returns: new_tree