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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:35:15+00:00 2026-06-15T17:35:15+00:00

I have a tree data-structure implemented in ruby. I’m using it to represent a

  • 0

I have a tree data-structure implemented in ruby. I’m using it to represent a parse-tree.

It works, as you might expect, by having many node objects, each containing useful values as well as an array of references to it’s child-nodes.

I’ve written a method to traverse the tree that’s pretty simple and works like:

def depth_first_traversal(node, &block) 
    if(node.has_children?)
        depth_first_traversal(node.children[0], &block) 
        yield node 
        depth_first_traversal(node.children[1], &block)
    else
        yield node  
    end
end 

The issue is that for each tree I only explicitly hold a reference to the root node. Thus far I’ve just been using my recursive traversal to access all the other nodes.

Now I need to change the values of the nodes in the tree and I’m not sure how to do it.
How could I modify this traversal so that I could modify each element in the tree, instead of just passing a reference to them in to &block?

— EDIT: —
Apologies for the lack of detail, I was trying to make my question broad and useful.

The ‘value’ of a node in the tree is several instance variables in each instance of the node-object. Lets call them @value and @type. There are getter and setter methods for them.

The tree is a binary tree – but that may change later. I also don’t think that’s the aspect of the problem I’m struggling with:

My tree explicitly creates the Node @root. All other nodes in the tree are created in a loop. So a typical node is accessible, for example as “the child of the child of the root” and in no other manner.

In other words, searching this structure of pointers is my only means of accessing the nodes.
If ruby passes exclusively by value, any value yielded (like in the above method) will be a copy of this object, not the object itself.

So I’m confused about how I should modify values in any tree, not just this one.

  • 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-15T17:35:15+00:00Added an answer on June 15, 2026 at 5:35 pm

    If I understand you correctly, you could probably do something like this:

    def df_tree_map(node, &block)
        if(node.has_children?)
            df_tree_map(node.children[0], &block)
            node = yield node
            df_tree_map(node.children[1], &block)
        else
            node = yield node
        end
    end
    

    Obviously this is going to have consequences to the tree structure, but that might be a benefit. The critical point here though is that you’re block will need to return a node instead of any old thing. Returning a string, for example, isn’t going to work the way that Array#map does, because a node inherently has children.

    Another solution is to allow the map function to modify the contents of the node but not the structure. I’m taking a little liberty here as you didn’t post the instance variables nodes have access too, but it should make enough sense:

    def df_tree_map(node, &block)
        if(node.has_children?)
            df_tree_map(node.children[0], &block)
            node.contents = yield node.contents
            df_tree_map(node.children[1], &block)
        else
            node.contents = yield node.contents
        end
    end
    

    Here, I’m not passing the node itself to the block, but rather the contents. This way, the tree structure cannot be altered by the map. It seems like it might be more consistent with the Array#map function, but it might not do what you’re looking for.

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

Sidebar

Related Questions

I've implemented a specialised tree data structure for a ray tracing application. I'm using
I have a MySQL table with a tree data structure. The fields are _id
I have a speed critical multithreaded program which involves data in a tree structure.
I have to build a tree using the following data which comes from a
I am using Delphi 2009. I have a very simple data structure, with 2
I have a large tree-like data structure of objects which behave mostly identical but
I am trying to have a tree display custom data that looks like this.
I have a hypothetical tree view that contains this data: RootNode Leaf vein SecondRoot
I have a binary tree function with 3 pieces of data in each node.
Ok, so I have a class for a Advanced Data Structure (in this case

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.