From 0940118916b866192186b073dcbb3026072acc6f Mon Sep 17 00:00:00 2001 From: wuliaozhiji Date: Sat, 19 Feb 2022 15:08:05 -0500 Subject: [PATCH] [O] Better implementation for insert_move_sequence --- assignments/A2/a2_game_tree.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/assignments/A2/a2_game_tree.py b/assignments/A2/a2_game_tree.py index 0babac5..a99698c 100644 --- a/assignments/A2/a2_game_tree.py +++ b/assignments/A2/a2_game_tree.py @@ -173,15 +173,14 @@ class GameTree: if index == len(moves): return - # Next root already exists - for c in self._subtrees: - if c.move == moves[index]: - c.insert_move_sequence_helper(moves, index + 1) - return + sub = self.find_subtree_by_move(moves[index]) - # Next root doesn't exist - sub = GameTree(moves[index], not self.is_white_move) - self.add_subtree(sub) + # Create subtree if not exist + if not sub: + sub = GameTree(moves[index], not self.is_white_move) + self.add_subtree(sub) + + # Insert move sub.insert_move_sequence_helper(moves, index + 1) ############################################################################