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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:40:08+00:00 2026-06-05T16:40:08+00:00

Say we have a binary tree as follow: I’m looking for an algorithm to

  • 0

Say we have a binary tree as follow: enter image description here

I’m looking for an algorithm to find all the equivalence of A. I’m given an array that contains elements in this tree. The rule is, if all the children of a node exist in an array, it is equivalent to having the node in the array.
For example, if we have B and C in the array, it is equivalent to having an A. So in the array above, F+G=C, and C+B = A, so [B,F,G] is also equivalent to A. Likewise [D E F G] is also equivalent to A.

I can recursively call something like checkSubstitute(node):

if node in array
return true
else:
    for child in nodeChildren
        if ((child not in array) && (child == terminalNode))
            return false
        else
        return checkSubstitute(child)

Does this logic make sense? Also how can I store all the equivalent arrays using an algorithm like the one above?

Thanks in advance!!

  • 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-05T16:40:10+00:00Added an answer on June 5, 2026 at 4:40 pm

    The method you gave doesn’t work properly, since you always return a value during the first iteration of the for loop.
    Suppose you have an array [D]. Then checkSubstitute(B) returns True, when it should return False.

    Instead of using a for loop, it’s easier just to make two explicit calls on both of the children. This assumes every node has either zero or two children. If a node can have one child, some additional null checking is necessary.

    #returns true if Node n exists in NodeList seq, or if its equivalent exists in seq.
    function exists(n, seq):
        if n in seq:
            return True
        if not n.hasChildren:
            return False
        return exists(n.leftChild, seq) and exists(n.rightChild, seq)
    

    getting all equivalent arrays just requires a bit of combinatorics.

    #gets all possible equivalents for the given node. This includes itself.
    #An equivalent is a list of nodes, so this method returns a list of lists of nodes.
    function getPossibleEquivalents(node):
        ret = new List()
        baseCase = new List()
        baseCase.append(node)
        ret.append(baseCase)
        if not node.hasChildren:
            return ret  
        for each leftEquivalent in getPossibleEquivalents(node.leftChild):
            for each rightEquivalent in getPossibleEquivalents(node.rightChild):
                ret.append(leftEquivalent + rightEquivalent)
        return ret
    

    Edit:
    You can extend getPossibleEquivalents for trees with exactly 0 or N children, by nesting N for loops:

    for each child0Equivalent in getPossibleEquivalents(node.child[0]):
        for each child1Equivalent in getPossibleEquivalents(node.child[1]):
            for each child2Equivalent in getPossibleEquivalents(node.child[2]):
                for each child3Equivalent in getPossibleEquivalents(node.child[3]):
                    for each child4Equivalent in getPossibleEquivalents(node.child[4]):
                        ret.append(child0Equivalent + child1Equivalent + child2Equivalent + child3Equivalent + child4Equivalent + child5Equivalent)
    

    If you want to write a single function that can handle trees with any number of children, you need to take the Cartesian Product of each possible equivalent of each child. Some languages implement cartesian product for you already. For example, in python:

    from itertools import product
    
    def getPossibleEquivalents(node):
        ret = [node]
        if len(node.children) == 0: return ret
        for equivalentTuple in product(map(getPossibleEquivalents, node.children)):
            possibleEquivalent = reduce(lambda x,y: x+y, equivalentTuple)
            ret.append(possibleEquivalent)
        return ret
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a binary tree which contains pointers at each node going to
Let's say we have a binary file that contains 2 bytes that form an
Let's say I have a binary tree data structure defined as follows type 'a
Let's say I have a binary file that is formatted like [unsigned int(length of
Let's say we have an SDK in C++ that accepts some binary data (like
Suppose I have a binary search tree which, initially, satisfies all of the red-black
Let's say that I have a binary that I am building, and I include
Let's say you have a List<List<Boolean>> and you want to encode that into binary
Let's say I have a binary vector of length N, and I'm looking for
Say I have a binary file of 12GB and I want to slice 8GB

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.