<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def update(self, changed_state_model=None, with_expand=False):
 """Checks if all states are in tree and if tree has states which were deleted :param changed_state_model: Model that row has to be updated :param with_expand: The expand flag for the tree """

        if not self.view_is_registered:
            return

        # define initial state-model for update
        if changed_state_model is None:
            # reset all
            parent_row_iter = None
            self.state_row_iter_dict_by_state_path.clear()
            self.tree_store.clear()
            if self._selected_sm_model:
                changed_state_model = self._selected_sm_model.root_state
            else:
                return
        else:  # pick
            if changed_state_model.state.is_root_state:
                parent_row_iter = self.state_row_iter_dict_by_state_path[changed_state_model.state.get_path()]
            else:
                if changed_state_model.state.is_root_state_of_library:
                    # because either lib-state or lib-state-root is in tree the next higher hierarchy state is updated
                    changed_upper_state_m = changed_state_model.parent.parent
                else:
                    changed_upper_state_m = changed_state_model.parent
                # TODO check the work around of the next 2 lines while refactoring -> it is a check to be more robust
                while changed_upper_state_m.state.get_path() not in self.state_row_iter_dict_by_state_path:
                    # show Warning because because avoided method states_update
                    logger.warning("Take a parent state because this is not in.")
                    changed_upper_state_m = changed_upper_state_m.parent
                parent_row_iter = self.state_row_iter_dict_by_state_path[changed_upper_state_m.state.get_path()]

        # do recursive update
        self.insert_and_update_recursively(parent_row_iter, changed_state_model, with_expand)