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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T02:04:32+00:00 2026-05-13T02:04:32+00:00

I have a tree structure in a .csv file (nodes are of type text),

  • 0

I have a tree structure in a .csv file (nodes are of type text), and after reading the csv i want to store the data in a ruby object. I went through a few tree plugins and i think nested_set would serve the purpose for me.
However, i am facing problem with fixing a format of csv file, so that i can read it and convert into tree object. Is there any direct way of converting a csv file or 2 dimensional array, into a tree data structure??

  • 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-13T02:04:33+00:00Added an answer on May 13, 2026 at 2:04 am

    After you clarified that you don’t need to store this tree in database, I suggest to throw away NestedSets (they’re to store nested sets of objects on RDBMS, which you don’t need). What you need i simple tree

    class Node
      attr_accessor :parent, :children, :text
    
      def initialize(text)
        @text = text
        @children = []
      end
    end
    

    As I have right to choose format of CSV file, then I suggest sth like this:

    id,parent,text
    1,,"1"
    2,1,"1.1"
    3,1,"1.2"
    3,2,"1.1.1"
    

    Tree root is first row, without parent, and there is always order that parent is declared before its children. That way you can build tree

    def build_tree(rows)
      nodes = {}
      rows.each do |row|
        node = Node.new(row[:text])
        nodes[row[:id]] = node
    
        node.parent = nodes[row[:parent]]
        nodes[row[:parent]].children << node if row[:parent]
      end
    
      nodes.values.find {|node| node.parent.nil? }
    end
    
    root = build_tree(rows)
    root.text #=> "1"
    root.children.map(&:text) #=> ["1.1", "1.2"]
    root.children[0].children.map(&:text) #=> ["1.1.1"]
    

    If you need to get all texts from subnodes, then you need to use more tricks

    def get_nodes(tree_node)
      [ tree_node, tree_node.children.map{|node| get_nodes(node)} ].flatten
    end
    
    get_nodes(root).map(&:text) #=> ["1", "1.1", "1.1.1", "1.2"]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a tree structure. The nodes would have a leaf node type and
I have a tree structure and I want to find all nodes matching a
I have an arbitrary tree structure of nodes. I want to draw this tree
I have a tree data structure composed by parent nodes and child nodes, something
In Lua, I have a tree relationship structure between objects where an object can
I have a category/file tree structure. Both categories and files can have parents, so
I have constructed an XML tree structure of an XML file. I am able
I have a tree-like object structure that consists of two types of objects: object
I have a tree like structure of Categories, the leaf Nodes in the tree
I have a Dev-express Tree Structure with Drag and Drop capability. After Drag and

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.