'Remove the subtree rooted at root_block_key
We do this breadth-first to make sure that we don\'t remove
any children that may have parents that we don\'t want to delete.'

@contract(root_block_key=BlockKey, blocks='dict(BlockKey:    BlockData)')
def _remove_subtree(self, root_block_key, blocks):

    child_parent_map = defaultdict(set)
    for (block_key, block_data) in blocks.iteritems():
        for child in block_data.fields.get('children', []):
            child_parent_map[BlockKey(*child)].add(block_key)
    to_delete = {root_block_key}
    tier = {root_block_key}
    while tier:
        next_tier = set()
        for block_key in tier:
            for child in blocks[block_key].fields.get('children', []):
                child_block_key = BlockKey(*child)
                parents = child_parent_map[child_block_key]
                if parents.issubset(to_delete):
                    next_tier.add(child_block_key)
        tier = next_tier
        to_delete.update(tier)
    for block_key in to_delete:
        del blocks[block_key]
