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 9015517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:46:16+00:00 2026-06-16T03:46:16+00:00

I have an algorithm to populate a tree like structure (class: Scan_instance_tree), but unfortunately,

  • 0

I have an algorithm to populate a tree like structure (class: Scan_instance_tree), but unfortunately, during each call, it is incorrectly adding to the root node’s children, as well as to the new child nodes created further down in the tree.

As a clue, I saw another thread…

Persistent objects in recursive python functions

…where this problem was mentioned briefly, and it was suggested that the parameters passed had to be mutable. Is that the answer, and how would I do this, in this example???

Here is my current code:

class Field_node(object):
    field_phenotype_id = -1
    field_name = ''
    field_parent_id = -1
    child_nodes = []          


class Scan_instance_tree(object):
    root_node = None  

    def __init__(self, a_db):
        self.root_node = Field_node()
        scan_field_values = self.create_scan_field_values(a_db)   # This just creates a temporary user-friendly version of a database table    
        self.build_tree(scan_field_values)  

    def build_tree(self, a_scan_field_values):
        self.root_node.field_name = 'ROOT'      
        self.add_child_nodes(a_scan_field_values, self.root_node)


    def add_child_nodes(self, a_scan_field_values, a_parent_node):
        i = 0
        while i < len(a_scan_field_values):
            if a_scan_field_values[i]['field_parent_dependancy'] == a_parent_node.field_phenotype_id:
                #highest_level_children.append(a_scan_field_values.pop(a_scan_field_values.index(scan_field))) 
                child_node = Field_node()
                child_node.field_phenotype_id = a_scan_field_values[i]['field_phenotype_id']
                child_node.field_name = a_scan_field_values[i]['field_name']
                child_node.field_parent_dependancy = a_scan_field_values[i]['field_parent_dependancy']

                a_parent_node.child_nodes.append(child_node)
                a_scan_field_values.remove(a_scan_field_values[i])

                # RECURSION: get the child nodes
                self.add_child_nodes(a_scan_field_values, child_node)
            else: 
                i = i+1

If I remove the recursive call to self.add_child_nodes(…), the root’s children are added correctly, ie they only consist of those nodes where the field_parent_dependancy = -1
If I allow the recursive call, the root’s children contain all the nodes, regardless of the field_parent_dependancy value.

Best regards

Ann

  • 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-06-16T03:46:17+00:00Added an answer on June 16, 2026 at 3:46 am

    When you define your Field_node class, the line

        child_nodes = []
    

    is actually instantiating a single list as a class attribute, rather than an instance attribute, that will be shared by all instances of the class.

    What you should do instead is create instance attributes in __init__, e.g.:

    class Field_node(object):
        def __init__(self):
            self.field_phenotype_id = -1
            self.field_name = ''
            self.field_parent_id = -1
            self.child_nodes = []
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this algorithm in Java to store passwords in database. I'd like to
I have an algorithm written in Java that I would like to make more
I have an algorithm where I create two bi-dimensional arrays like this: TYPE TPtrMatrixLine
I have an algorithm that receives input and delivers output which I would like
I have an algorithm that works perfectly, but it uses recursion. I know there
I made an algorithm to generate sudokus, but it was terribly inefficient. Each puzzle
I have algorithm of calculating average speed in pure python: speed = [...] avg_speed
I have algorithm of calculation of the difference between neighboring elements in pure python:
I have algorithm/computation in Java and unit test for it. The unit test expects
I have an algorithm that finds the minimum of function/n on Rn. And I

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.