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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:25:26+00:00 2026-05-15T11:25:26+00:00

I have multiple binary trees stored as an array. In each slot is either

  • 0

I have multiple binary trees stored as an array. In each slot is either nil (or null; pick your language) or a fixed tuple storing two numbers: the indices of the two “children”. No node will have only one child — it’s either none or two.

Think of each slot as a binary node that only stores pointers to its children, and no inherent value.

Take this system of binary trees:

    0       1
   / \     / \
  2   3   4   5
 / \         / \
6   7       8   9
   / \
 10   11

The associated array would be:

   0       1       2      3     4      5      6      7        8     9     10    11
[ [2,3] , [4,5] , [6,7] , nil , nil , [8,9] , nil , [10,11] , nil , nil , nil , nil ]

I’ve already written simple functions to find direct parents of nodes (simply by searching from the front until there is a node that contains the child)

Furthermore, let us say that at relevant times, both all trees are anywhere between a few to a few thousand levels deep.

I’d like to find a function

P(m,n)

to find the lowest common ancestor of m and n — to put more formally, the LCA is defined as the “lowest”, or deepest node in which have m and n as descendants (children, or children of children, etc.). If there is none, a nil would be a valid return.

Some examples, given our given tree:

P( 6,11)   # => 2
P( 3,10)   # => 0
P( 8, 6)   # => nil
P( 2,11)   # => 2

The main method I’ve been able to find is one that uses an Euler trace, which turns the given tree (Adding node A as the invisible parent of 0 and 1, with a “value” of -1), into:

A-0-2-6-2-7-10-7-11-7-2-0-3-0-A-1-4-1-5-8-5-9-5-1-A

And from that, simply find the node between your given m and n that has the lowest number; For example, to find P(6,11), look for a 6 and an 11 on the trace. The number between them that is the lowest is 2, and that’s your answer. If A (-1) is in between them, return nil.

 -- Calculating P(6,11) --

A-0-2-6-2-7-10-7-11-7-2-0-3-0-A-1-4-1-5-8-5-9-5-1-A
      ^ ^        ^
      | |        |
      m lowest   n

Unfortunately, I do believe that finding the Euler trace of a tree that can be several thousands of levels deep is a bit machine-taxing…and because my tree is constantly being changed throughout the course of the programming, every time I wanted to find the LCA, I’d have to re-calculate the Euler trace and hold it in memory every time.

Is there a more memory efficient way, given the framework I’m using? One that maybe iterates upwards? One way I could think of would be the “count” the generation/depth of both nodes, and climb the lowest node until it matched the depth of the highest, and increment both until they find someone similar.

But that’d involve climbing up from level, say, 3025, back to 0, twice, to count the generation, and using a terribly inefficient climbing-up algorithm in the first place, and then re-climbing back up.

Are there any other better ways?


Clarifications

In the way this system is built, every child will have a number greater than their parents.

This does not guarantee that if n is in generation X, there are no nodes in generation (X-1) that are greater than n. For example:

        0
       / \
      /   \
     /     \
    1       2        6
   / \     / \      / \
  2   3   9  10    7   8
     / \              / \
    4   5            11 12

is a valid tree system.

Also, an artifact of the way the trees are built are that the two immediate children of the same parent will always be consecutively numbered.

  • 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-15T11:25:27+00:00Added an answer on May 15, 2026 at 11:25 am

    Are the nodes in order like in your example where the children have a larger id than the parent? If so, you might be able to do something similar to a merge sort to find them.. for your example, the parent tree of 6 and 11 are:

    6  -> 2 -> 0
    11 -> 7 -> 2 -> 0
    

    So perhaps the algorithm would be:

    left = left_start
    right = right_start
    
    while left > 0 and right > 0
        if left = right
            return left
        else if left > right
            left = parent(left)
        else
            right = parent(right)
    

    Which would run as:

    left    right
    ----    -----
     6        11    (right -> 7)
     6        7     (right -> 2)
     6        2     (left -> 2)
     2        2     (return 2)
    

    Is this correct?

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

Sidebar

Ask A Question

Stats

  • Questions 450k
  • Answers 450k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You could write virtually straightforward positive(1). positive(X) :- positive(Y), X… May 15, 2026 at 8:29 pm
  • Editorial Team
    Editorial Team added an answer You can define a brush as a LinearGradientBrush, look for… May 15, 2026 at 8:29 pm
  • Editorial Team
    Editorial Team added an answer There are other ways to verify ownership of website. Many… May 15, 2026 at 8:29 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.