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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:19:10+00:00 2026-06-11T15:19:10+00:00

The following code builds a hierarchy of nodes with 3 fixed levels (3 nodes):

  • 0

The following code builds a hierarchy of nodes with 3 fixed levels (3 nodes):

     function createNode(name) {
     return({name: name, children: []});
     }
     function addChild(node, child) {
     node.children.push(child);

      return node;
      }
      var tree = createNode("");
      var subChild = createNode("");
      addChild(subChild, createNode("A31"));

      addChild(tree, subChild);

How do I alter this code so that the number of levels created in the hierarchy is not fixed. How do I make the creation of nodes dynamic, in that the number of nodes are not known and will vary depending on the some data passed. How is done so that the hierarchy size can be built and defined dynamically. Can loops be applied in the above code to achieve this?

  • 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-11T15:19:11+00:00Added an answer on June 11, 2026 at 3:19 pm
    /**
     * Just parses a string in a specific format
     * @return object
     * {
     *    name, // Name of the node
     *    childens // Array of child nodes IN STRING REPRESENTATION
     * }
     */
    function parseNodeStr(str) {
      // @to-do
    }
    
    function createNode(defStr) {
      var node = false;,
          nodeParsed = parseNodeStr(defStr);
    
      if (nodeParsed) {
        node = {};
        node.name = nodeParsed.name;
        node.childrens = [];
        for (child in nodeParsed.childs) {
           addChild(node, createNode(child);
        }
      }
    
      return node;
    }
    
    function addChild(node, child) {
      if (child) {
        node.childrens.push(child);
      }
    
      return node;
    }
    
    var node = createNode("(node1,[(node2,[(n3),(n4)])])");
    

    I hope it helps you.

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

Sidebar

Related Questions

I have the following code: function build_all_combinations(input_array){ array = [1,2,3] limit = array.length -
I am building an XML document successfully with the following code: public function build($result)
I have the following code: Tag.find_all_by_company_id(4).each.collect{|tag| tag.name }.join(,) (Essentially I'm trying to build a
I have the following code which builds a Generic List of Abc from a
I am on Windows 7 - 64bit, using VS2010. The following code builds in
I'm running the following scheme: Ant makes java code builds and using soscmd API/utility
I have the following code snippet that builds chained select boxes based on data
The following code works fine (it builds a generic OrderBy): public class Item {
I think the following code snippet is perfectly legal (it builds anyway on MS
The following code builds error free on msvc, but does not compile on g++.

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.