Program to fill Min-max game tree in Python

The Min-max game tree algorithm is used in two-player games where one player wants to maximize the score while the other wants to minimize it. In this implementation, Player 1 (maximizer) plays at even levels and Player 2 (minimizer) plays at odd levels.

How the Algorithm Works

The algorithm uses a bottom-up approach:

  • Leaf nodes contain the actual game scores
  • Internal nodes at even levels (Player 1) choose the maximum value from their children
  • Internal nodes at odd levels (Player 2) choose the minimum value from their children

Example Tree Structure

0 3 0 0 0 -3 4 Level 0 (Max) Level 1 (Min) Level 2 (Max) Level 3 (Leaves)

Implementation

class TreeNode:
    def __init__(self, data, left=None, right=None):
        self.val = data
        self.left = left
        self.right = right

class Solution:
    def helper(self, root, h, currentHeight):
        if not root:
            return
            
        # Recursively process children first
        self.helper(root.left, h, currentHeight + 1)
        self.helper(root.right, h, currentHeight + 1)
        
        # Only process internal nodes (not leaves)
        if currentHeight < h:
            if currentHeight % 2 == 0:  # Even level - maximize
                if root.left and root.right:
                    root.val = max(root.left.val, root.right.val)
                elif root.left:
                    root.val = root.left.val
                elif root.right:
                    root.val = root.right.val
            else:  # Odd level - minimize
                if root.left and root.right:
                    root.val = min(root.left.val, root.right.val)
                elif root.left:
                    root.val = root.left.val
                elif root.right:
                    root.val = root.right.val
    
    def height(self, root):
        if not root:
            return 0
        return 1 + max(self.height(root.left), self.height(root.right))
    
    def solve(self, root):
        h = self.height(root)
        self.helper(root, h, 0)
        return root
    
    def print_tree(self, root):
        if root is not None:
            self.print_tree(root.left)
            print(root.val, end=', ')
            self.print_tree(root.right)

# Create the game tree
ob = Solution()
root = TreeNode(0)
root.left = TreeNode(3)
root.right = TreeNode(0)
root.right.left = TreeNode(0)
root.right.right = TreeNode(0)
root.right.left.left = TreeNode(-3)
root.right.right.right = TreeNode(4)

print("Tree values after min-max algorithm:")
ob.print_tree(ob.solve(root))
Tree values after min-max algorithm:
3, 3, -3, -3, -3, 4, 4,

Step-by-Step Execution

The algorithm works from bottom to top:

  1. Level 2 (Max): Left child gets max(-3) = -3, Right child gets max(4) = 4
  2. Level 1 (Min): Right child gets min(-3, 4) = -3
  3. Level 0 (Max): Root gets max(3, -3) = 3

Key Points

  • The algorithm assumes both players play optimally
  • Even levels correspond to the maximizing player
  • Odd levels correspond to the minimizing player
  • Leaf nodes contain the actual game outcomes

Conclusion

The min-max algorithm fills game trees by propagating optimal values upward, with players alternating between maximizing and minimizing strategies. This approach ensures both players make the best possible moves given the current game state.

Updated on: 2026-03-25T12:41:04+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements