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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:33:13+00:00 2026-05-28T05:33:13+00:00

I have a tree of nodes like this: class Node: next # the next

  • 0

I have a tree of nodes like this:

class Node:
    next        # the next node or None
    prev        # the previous node or None
    parent      # the parent or None
    children[]  # ordered list of child nodes
    columns[]   # a list of data. Currently only holdt the 
                # string representation of the node in the model.

Since I can’t know in advance how large the model is, I’ve reached the conclusion that recursion is not an option. I would like to keep as few nodes in memory as possible. This is what my method should print:

- 0
-- 0:0
--- 0:0:0
--- 0:0:1
---- 0:0:1:0
---- 0:0:1:1
--- 0:0:2
-- 0:1
- 1

But this is what it does print:

- 0
-- 0:0
-- 0:1
-- 0
- 1
--- 0:0:0
--- 0:0:1
--- 0:0:2
-- 0:1
-- 0
- 1
--- 0:0:1
---- 0:0:1:0
---- 0:0:1:1
--- 0:0:2
-- 0
---- 0:0:1:0
---- 0:0:1:1
--- 0:0:2
---- 0:0:1:1
---- 0:0:1:1

Here’s the code I’ve written:

def print_tree(from_path):
    nodelist = []
    root_node = model.get_iter(from_path)
    nodelist.append((root_node, 0)) # the second item in the tuple is the indentation

    while nodelist:
        node = nodelist[0][0]
        indent = nodelist[0][1]
        del(nodelist[0])
        print("{0} {1}".format("-" * indent, node.columns[0])) 

        if node.children:
            child = node.children[0]
            nodelist.append((child, indent +1))

            while child.next:
                nodelist.append((child.next, indent +1))
                child = child.next
        if node.next:
            next = node.next
            nodelist.append((next, indent))

Any help is greatly appreciated.

  • 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-28T05:33:14+00:00Added an answer on May 28, 2026 at 5:33 am

    Since each node has a reference to its parent, I think you can traverse the whole tree keeping only one node in memory at a time. I had a bit of trouble understanding your code (in particular how each node is loaded to memory), so I’ll post my suggestion in pseudo-code:

    def visit(node,indent):
        # Load your node data
        print("{0} {1}".format("-" * indent, node.columns[0])) # Do something with your data
        # Unload your node data
        if len(node.children) > 0 :
            return (node.children[0], indent+1) # Visit the first child, if there is one
        while node.next is None: # If no sibling, your parent is done
            node = node.parent
            indent -= 1
            if node is None: # Root node reached, end the traversal
                return None
        return (node.next, indent) # Visit your next sibling, if there is one
    
    cursor = (root_node, 0)
    while cursor is not None:
        cursor = visit(*cursor)
    

    If the node itself must be dynamically loaded (i.e. next, prev, parent and children only contain the path to another node’s data, not a reference to a Node object), tell me and I’ll update the answer (just need to change a little the places for loading/unloading). Of course, if unloading is just a matter of leaving the object to the garbage collector, it’s even easier…

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

Sidebar

Related Questions

I have defined a generic tree-node class like this: template<class DataType> class GenericNode {
I have a tree like structure roughly like this: class Node < ActiveRecord::Base belongs_to
I have this very simple C++ class: class Tree { public: Node *head; };
I'm writing a Java Tree in which tree nodes could have children that take
Im having a problem with python.. I have a binary tree node type: class
I have a tree structure where each Node has a parent and a Set<Node>
I have a binary tree class that is created with a root node and
I have trees like this one: $tree = array(A, array( array(B, 1), array(C, 2),
I require a tree / directed acyclic graph implementation something like this: public class
Ok so I have a Node in a tree which potentially has a parent

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.