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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:55:00+00:00 2026-05-30T01:55:00+00:00

How exactly does the below code block work? More specifically, how does the program

  • 0

How exactly does the below code block work? More specifically, how does the program know which option to return?

return    ancestor (node1->left(), node2)
           || ancestor (node1->right(), node2)
           || ancestor (node2->left(), node1)
           || ancestor (node2->left(), node1);

This code block is part of code to traverse a tree in order to determines if one node is an ancestor of the other one when given node1 and node2 in a tree.

Note that node1 and node2 are passed in to the function that is responsible for determining if there is a possible ancestor/descendent relationship:

bool ancestor (const Binary_node<Type> * node1, const Binary_node<Type> * node2)
{
         // .... code
}
  • 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-30T01:55:01+00:00Added an answer on May 30, 2026 at 1:55 am

    How does the program know which option to return?

    The program will keep trying the options, until it finds one that works.

    How exactly does the below code block work?

    In each call to ancestor(), the function will try four possibilities:

    • Move node1 to its left subtree, and try to work through the rest rest of the problem.
    • If that didn’t work, try to move node1 to its right subtree instead.
    • If that didn’t work, move node2 to its left subtree instead.
    • If that didn’t work, move node2 to its right subtree instead.

    If all four possibilities failed, then nodes node1 and node2 are surely not related via ancestor relationship.

    Warning: As implemented, the ancestor function is extremely slow, except for very small trees. Because we try four options in each ancestor() call, the number of states roughly quadruples if you increase the height of the tree by 1.

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

Sidebar

Related Questions

I have the code below, which works lovely and does EXACTLY what I want
How exactly does NSInvocation work? Is there a good introduction? I’m specifically having issues
How does async JMS work? I've below sample code: public class JmsAdapter implements MessageListener,
The below code is almost perfect and does exactly what I need it to
I was running the code below, which essentially does very little. It just adds
below code sample does not return the original text after encrypt/decrypt operation and I
What exactly does putting extern C into C++ code do? For example: extern C
What exactly does a finally block in exception handling perform?
Can anyone tell me what exactly does this Java code do? SecureRandom random =
Trying to get the code below to work but the success doesn't execute -

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.