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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:16:19+00:00 2026-05-25T20:16:19+00:00

I have a binary tree class as below: class BTree: def __init__(self, data, left=None,

  • 0

I have a binary tree class as below:

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

    def __unicode__(self):
        return "%s" % self.data

and I have another tree serializing method listed as below:

class JTree(object):
    def __init__(self, id, children=None): 
        self.id = id 
        if children is None:
            children=[]
        self.children = children

def encode_tree(obj):
    if not isinstance(obj, JTree):
        raise TypeError("%r is not JSON serializable" % (o,))

    return obj.__dict__

then I populate the Binary Tree data as below:

bt = BTree("1")
bt.left =BTree("2")
bt.right=BTree("3")

so if I serialize the data, I can get the following result:

tree = JTree(bt.data, [JTree(bt.left.data), JTree(bt.right.data)])

print json.dumps(tree, default=encode_tree)


{"id": "1", "children": [{"id": "2", "children": []}, {"id": "3", "children": []}]}

The problem is that I can’t figure out how to program a piece of code to generate the result. Which means I want to have a generator or recursive function to run the code:

JTree(bt.data, [JTree(bt.left.data), JTree(bt.right.data)])

Can somebody give me an idea? Thanks

  • 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-25T20:16:20+00:00Added an answer on May 25, 2026 at 8:16 pm

    It seems to me that you want a simple recursive function like:

    def convert_to_jtree(bt):
        return JTree(bt.data, [convert_to_jtree(bt.left) if bt.left else None,
                              convert_to_jtree(bt.right) if bt.right else None])
    

    or something very similar.

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

Sidebar

Related Questions

I have a binary tree class that is created with a root node and
Im having a problem with python.. I have a binary tree node type: class
I have a Binary Search Tree as a derived class from a Binary Tree,
I have a binary tree where each node can have a value. I want
I have a Binary tree for a mathematical expression(infix), i want to convert directly
I have a binary tree in unordered list that looks like this: <ul> <li>1
I have a binary tree of functions and terminal values. I'd like to print
I have problems building a binary tree from the bottom up. THe input of
I have a very simple binary tree structure, something like: struct nmbintree_s { unsigned
I have a PHP script that builds a binary search tree over a rather

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.