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

  • SEARCH
  • Home
  • 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 7899915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:49:59+00:00 2026-06-03T08:49:59+00:00

Basically I want to be able to have each node of type tree have

  • 0

Basically I want to be able to have each node of type tree have a Data field and a list of branches. This list should contain a number of objects of type Tree.
I think I have the actual implementation of the list down, but I get strange behavior when I try using the getLeaves method. Basically it calls itself recursively and never returns, and the way that happens is somehow the second node of the tree gets it’s first branch set as itself (I think).

class Tree:
    """Basic tree graph datatype"""
    branches = []

    def __init__(self, root):
        self.root = root

    def addBranch (self, addition):
    """Adds another object of type Tree as a branch"""
        self.branches += [addition]

    def getLeaves (self):
        """returns the leaves of a given branch. For leaves of the tree, specify root"""
        print (len(self.branches))
        if (len(self.branches) == 0):
            return self.root
        else:
            branchSum = []
            for b in self.branches:
                branchSum += b.getLeaves()
            return (branchSum)
  • 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-03T08:50:01+00:00Added an answer on June 3, 2026 at 8:50 am

    Your ‘branches’ variable is a class member, not an instance member. You need to initialize the ‘branches’ instance variable in the constructor:

    class Tree:
        """Basic tree graph datatype"""
    
        def __init__(self, root):
           self.branches = []
           self.root = root
    

    The rest of your code looks good.

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

Sidebar

Related Questions

Given this structure.. I basically want to be able to take a list of
I have a scraping object basically. I want to be able to add POST
I have two action methods that are conflicting. Basically, I want to be able
I am in a situation where I basically want to be able to have
this may sound kind of convoluted but basically i want to have a form
I have a 2-dimensional array of objects and I basically want to databind each
I have three NSViews stacked on each other, and I want to be able
Basically I want to re-style UITableView(Controller). Each cell will have a picture as a
I basically want to be able to deploy multiple versions of the same EAR
Basically we want to be able to somehow synchronize our .NET application's contacts with

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.