Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 617985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:29:42+00:00 2026-05-13T18:29:42+00:00

Full disclosure, this is part of a homework assignment (though a small snippet, the

  • 0

Full disclosure, this is part of a homework assignment (though a small snippet, the project itself is a game playing AI).

I have this function built into a tree node class:

    def recursive_score_calc(self):
        current_score = self.board
        for c in self.children:
            child_score = c.recursive_score_calc()
            if(turn_color == 1):
                if(child_score > current_score):
                    current_score = child_score
            else:
                if(child_score < current_score):
                    current_score = child_score
        self.recursive_score = current_score
        return current_score

On a tree of depth 1 (a root and some children), it hits the Python recursion limit already. The function is designed to use dynamic programming to build a min-max tree from the bottom up. To be honest, I have no idea why this isn’t working as intended, but I am fairly new to Python as well.

Good people of Stack Overflow: Why does this code give me a stack overflow?

The Entire Class in question:

from Numeric import *

class TreeNode:
    children = []
    numChildren = 0
    board = zeros([8,8], Int)
    turn_color = 0 # signifies NEXT to act
    board_score = 0 # tally together board items
    recursive_score = 0 # set when the recursive score function is called

def __init__(self, board, turn_color):
    self.board = copy.deepcopy(board)
    self.turn_color = turn_color
    for x in range (0,7):
        for y in range (0,7):
            self.board_score = self.board_score + self.board[x][y]

def add_child(self, child):
    self.children.append(child)
    self.numChildren = self.numChildren + 1

def recursive_score_calc(self):
    current_score = self.board # if no valid moves, we are the board. no move will make our score worse
    for c in self.children:
        child_score = c.recursive_score_calc()
        if(turn_color == 1):
            if(child_score > current_score):
                current_score = child_score
        else:
            if(child_score < current_score):
                current_score = child_score
    self.recursive_score = current_score
    return current_score

The function that interacts with this (Please Note, this is bordering on the edge of what is appropriate to post here, I’ll remove this part after I accept an answer): [It didn’t turn out to be the critical part anyways]

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T18:29:42+00:00Added an answer on May 13, 2026 at 6:29 pm

    This bit of your code:

    class TreeNode:
        children = []
    

    means that every instance of the class shares the same children list. So, in this bit:

    def add_child(self, child):
        self.children.append(child)
    

    you’re appending to the “class-global” list. So, of course, every node is a child of every other node, and disaster is guaranteed.

    Fix: change your class to

    class TreeNode(object):
        numChildren = 0
        board = zeros([8,8], Int)
        turn_color = 0 # signifies NEXT to act
        board_score = 0 # tally together board items
        recursive_score = 0 # set when the recursive score function is called
    
    def __init__(self, board, turn_color):
        self.children = []
        self.board = copy.deepcopy(board)
        self.turn_color = turn_color
    ... etc, etc ...
    

    the rest doesn’t need to change to fix this bug (though there may be opportunities to improve it or fix other bugs, I have not inspected it deeply), but failing to assign self.children in __init__ is causing your current bug, and failing to inherit from object (unless you’re using Python 3, but I’d hope you would mention this little detail if so;-) is just a bug waiting to happen.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Full disclosure : This is for a homework assignment. This is driving me nuts.
full disclosure - this is for a homework assignment. And I normally would not
Full disclosure: this is for an assignment, so please don't post actual code solutions!
First off, full disclosure: This is going towards a uni assignment, so I don't
Full disclosure: this is for a school project. The project is in C++. They
First, in order to provide full disclosure, I want to point out that this
Full disclaimer: this is not really a homework, but I tagged it as such
Full disclosure: I don't really know Ruby. I'm mostly faking it. I have a
How would you define testing? In the interest of full disclosure, I'm posting this
Full disclosure: this issue is duplicated on the ggplot2 google group I'm developing a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.