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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:58:22+00:00 2026-06-10T18:58:22+00:00

I have a possibly large rooted tree structure that I want to transform into

  • 0

I have a possibly large rooted tree structure that I want to transform into a X * Y matrix with X being the amount of leaves in the tree and Y being the amount of nodes in the tree with a degree larger than 1, i.e. the root node and internal nodes. The matrix should be filled as such:

Mi,j = { 0 if leaf i has ancestor j, 1 otherwise

For example, this tree:

      --A 
     /
    1   B
   / \ /
  /   3  
 /     \
0       C
 \ 
  \   --D
   \ /
    2
     \--E

would translate into this matrix:

  0 1 2 3
A T T F F
B T T F T
C T T F T
D T F T F
E T F T F

Since the trees might become pretty big (possibly ~100,000 leaves), I was wondering if there is a smarter/faster way of doing this than traversing up the tree for every one of the leaf nodes. It feels like some kind of algorithm is in this problem somewhere, but I haven’t figured it out yet. Maybe someone can help?

In my application, the tree represents large phylogenetic hierarchies, so it is not balanced and there might be nodes with more than two children.

  • 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-10T18:58:23+00:00Added an answer on June 10, 2026 at 6:58 pm

    I’d go with post-order traversal.

    Maintain lists of leaves while traversing the tree, and in each level – the list will contain all the leaves up to this level.

    decalrations for functions we will use:

    list merge(list1,list2) //merges two lists and creates a new list
    list create() // creates a new empty list
    void add(list,e) // appends e to the list
    void setParent(leaf,node) //sets (in your matrix) node as a parent of leaf
    

    Pseudo code:

    list Traverse(root):
      if (root == nil):
          l <- create()
          return l
      else if (root is leaf):
          l <- create()
          add(l,root)
          return l
      else: 
          l1 <- Traverse(root.left)
          l2 <- Traverse(root.right)
          l <- merge(l1,l2)
          for each leaf in l:
              setParent(leaf,root)
          return l
    

    Time is O(n*m) – for setting the matrix (though the algorithm itself is O(nlogn) time for a balanced tree).

    If you want to prevent the O(n*m) initialization you can initialize the matrix in O(1), and then run the algorithm above in O(nlogn). Though it will give better asymptotic complexity, I doubt it will actually be faster.

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

Sidebar

Related Questions

I have a fairly large text file that I would like to convert into
I have a large set of log files that I want to characterize or
I have a rather large(not too large but possibly 50+) set of conditions that
I have two large dictionaries with unique keys but possibly overlapping values. I want
I have a very large possible data set that I am trying to visualize
I have a possibly large block of text to search for instances of [[...]]
I have a project that will basically be a large brochure html website. Although
If i have a large number of timers (10 to possibly a couple hundred)
I have a javascript rich page that is passing a large JSON formatted to
I have a collection that is potentially going to be very large. Now I

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.