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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:15:48+00:00 2026-05-27T21:15:48+00:00

I have a tree structure that has a node with a parent ID (unlimited

  • 0

I have a tree structure that has a node with a parent ID (unlimited child nodes). For display purposes I need this tree structure as a binary tree. How I do this is at each level nodes are grouped into a single node based upon a condition. When a node is selected its children are then displayed. Example:

N-Ary tree

The green is when the condition is true and red is false

BTree

B, C have been grouped into the left node and D, E are on the right based on their conditions.

QUESTION: I’m using KnockoutJS to display my tree and I need to be able to perform normal tree operations like getting a node based on its ID, inserting node(s) removing node(s). This is the structure I have. Is there a better structure/way of doing this?

var tree = [
    { groupNodeId: "A", childNodes: [
        { nodeId: "A", childGroupNodes: [
            { groupNodeId: "B", condition: true, childNodes: [
                { nodeId: "B", childGroupNodes: []},
                { nodeId: "C", childGroupNodes: []}
            ]},
            { groupNodeId: "D", condition: false, childNodes: [
                { nodeId: "D", childGroupNodes: []},
                { nodeId: "E", childGroupNodes: []}
            ]}
        ]}
    ]}
];
  • 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-27T21:15:48+00:00Added an answer on May 27, 2026 at 9:15 pm

    I’m not sure exactly what you want, but assuming that:

    1. You want to insert “groupNodes”, not regular nodes, and that groupNodes can have unlimited children.
    2. The original tree contains only regular nodes, not groupNodes.
    3. All regular nodes have a nodeId, a condition set to true or false, and an array childNodes.
    4. You don’t want to create groupNodes that would have no children.

    then here’s your answer.

    function binize(tree) {
      var left,right,t,u,
        stack=[tree];
      while(t=stack.pop()) {
        left=[];
        right=[];
        while(u=t.childNodes.pop()) {
          (u.condition?left:right).push(u);
          stack.push(u);
        }
        left.length&&t.childNodes.push({
          groupNodeId:left[0].nodeId,
          condition:true,
          childNodes:left
        });
        right.length&&t.childNodes.push({
          groupNodeId:right[0].nodeId,
          condition:false,
          childNodes:right
        });
      }
    }
    

    I tested it using this data structure (note that tree is the top level object, not an array containing it).

    var tree={nodeId:'A',childNodes:[
      {nodeId:'B',condition:true,childNodes:[]},
      {nodeId:'C',condition:true,childNodes:[]},
      {nodeId:'D',condition:false,childNodes:[
        {nodeId:'F',condition:false,childNodes:[]},
        {nodeIf:'G',condition:false,childNodes:[]}
      ]},
      {nodeId:'E',condition:false,childNodes:[]}
    ]};
    

    If I knew a little more about your tree then I might have taken a different approach. For example, if the tree isn’t very deep then it might be more efficient (and of course cleaner) to dig through it recursively.

    Also, I have never used KnockoutJS before, and have no idea what structures it likes. I just generated the structures you indicated; hopefully that will work.

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

Sidebar

Related Questions

I have a tree data structure that is L levels deep each node has
I have a tree structure where each Node has a parent and a Set<Node>
I have a table in SQL server that has the normal tree structure of
I have a categories table that has a structure: categoryId parentId categoryName This table
I need create a tree structure recursively. In the tree each node has different
I have a tree like structure roughly like this: class Node < ActiveRecord::Base belongs_to
I have an arbitrary tree structure of nodes. I want to draw this tree
I have a tree structure that represents an org chart and I need to
I have a tree structure that can be n-levels deep, without restriction. That means
I have a tree structure in memory that I would like to render in

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.