'Refactors a parse tree (modifying the tree in place).
For compatible patterns the bottom matcher module is
used. Otherwise the tree is traversed node-to-node for
matches.
Args:
tree: a pytree.Node instance representing the root of the tree
to be refactored.
name: a human-readable name for this tree.
Returns:
True if the tree was modified, False otherwise.'

def refactor_tree(self, tree, name):

    for fixer in chain(self.pre_order, self.post_order):
        fixer.start_tree(tree, name)
    self.traverse_by(self.bmi_pre_order_heads, tree.pre_order())
    self.traverse_by(self.bmi_post_order_heads, tree.post_order())
    match_set = self.BM.run(tree.leaves())
    while any(match_set.values()):
        for fixer in self.BM.fixers:
            if ((fixer in match_set) and match_set[fixer]):
                match_set[fixer].sort(key=pytree.Base.depth, reverse=True)
                if fixer.keep_line_order:
                    match_set[fixer].sort(key=pytree.Base.get_lineno)
                for node in list(match_set[fixer]):
                    if (node in match_set[fixer]):
                        match_set[fixer].remove(node)
                    try:
                        find_root(node)
                    except AssertionError:
                        continue
                    if (node.fixers_applied and (fixer in node.fixers_applied)):
                        continue
                    results = fixer.match(node)
                    if results:
                        new = fixer.transform(node, results)
                        if (new is not None):
                            node.replace(new)
                            for node in new.post_order():
                                if (not node.fixers_applied):
                                    node.fixers_applied = []
                                node.fixers_applied.append(fixer)
                            new_matches = self.BM.run(new.leaves())
                            for fxr in new_matches:
                                if (fxr not in match_set):
                                    match_set[fxr] = []
                                match_set[fxr].extend(new_matches[fxr])
    for fixer in chain(self.pre_order, self.post_order):
        fixer.finish_tree(tree, name)
    return tree.was_changed
