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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:23:05+00:00 2026-06-07T16:23:05+00:00

I have numerical strings that represent trees (I don’t know if there’s an official

  • 0

I have numerical strings that represent trees (I don’t know if there’s an official name for this):

012323301212

The above example represents 2 trees. The root is signified with 0. Immediate children of the root are ‘1’s, immediate children of ‘1’s are ‘2’s, and so on. I need to group these into sub trees that consist of parents and their immediate children. So the above would be decomposed into…

01 122 23 233 011 12 12

I was thinking one possible way to do this would be to build a tree structure from the string then visit each node and generate a subtree of it and its immediate children (if it had any), but this seems relatively complicated. Is there some clever way I can do this without resorting to creating a tree structure and traversing it?

  • 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-07T16:23:07+00:00Added an answer on June 7, 2026 at 4:23 pm

    The output you’re requesting essentially is the tree structure. That said, you can treat your input as a pre-order depth-first traversal and read off the tree structure without holding all of it in memory at once:

    depths = [0, 1, 2, 3, 2, 3, 3, 0, 1, 2, 1, 2]   # Our input
    
    next_id = 0                                 # Id of the next node
    ids = []                                    # The ids of nodes as we traverse
    parents = {}                                # Maps children to parents
    children = {}                               # Maps parents to lists of children
    
    for depth in depths
      # First we give this node an id
      id = next_id
      next_id += 1
    
      if ids.length <= depth
        # If this is our first time to this depth, push a new level onto ids.
        ids.append(id)
      else
        # Otherwise just insert the id into the list at its depth
        ids[depth] = id
    
        # And truncate off all elements after.  This preserves the invariant
        # that each id is the descendent of ids that appears before it.
        ids[depth].resize(depth + 1)
    
      assert(ids.length == depth + 1)
    
      # Emit the link between this node and its parent
      if depth > 0
        parents[ids[depth]] = ids[depth - 1]
        children[ids[depth - 1]] ||= []       # add the empty list first if needed
        children[ids[depth - 1]].append(ids[depth])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this relatively large numerical application code that may run for a few
I have this question in a Compilers coursework but I don't really know how
I have a numerical indexed array of strings. Another array contains a set of
I have a couple of numerical datasets that I need to create a concept
I have an order form with about 30 text fields that contain numerical values.
I have a table with a name (varchar) field that only holds numeric string
I have a list of strings that can contain a letter or a string
I have a dataset that looks like this: 0 _ _ 23.0186E-03 10 _
I have C function that takes string pointer as a parameter. This functions returns
I have a variable that contains a 4 byte, network-order IPv4 address (this was

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.