Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Given two objects, can you find the nearest common ancestor?

Tags:

raku

Given two objects is there a simple way in Raku to find the nearest common ancestor in their tree of inheritence?

There are already some general answers for this:

How to find nearest common ancestor class of two objects?

Algorithm to find common ancestor of two nodes given

I was wondering if there's an idiomatic solution built-in to Raku already.

like image 480
Joseph Brenner Avatar asked Aug 21 '21 00:08

Joseph Brenner


People also ask

How do you find the least common ancestor of a binary tree?

Given a binary tree (not a binary search tree) and two values say n1 and n2, write a program to find the least common ancestor. Let T be a rooted tree. The lowest common ancestor between two nodes n1 and n2 is defined as the lowest node in T that has both n1 and n2 as descendants (where we allow a node to be a descendant of itself).

How to find the common ancestor of two pointers?

The main idea here is to try a huge jump at first. If doing a huge jump on both pointers leads them to the same node, then we have arrived at a common ancestor. However, we may have jumped too far. There could be some deeper common ancestor. So we can’t do this jump, let’s try something smaller.

What is the lowest common ancestor of two nodes?

2. Definition The Lowest Common Ancestor (LCA) of two nodes and in a rooted tree is the lowest (deepest) node that is an ancestor of both and . Remember that an ancestor of a node in a rooted tree is any node that lies on the path from the root to (including ).

Which shared ancestor of N1 and N2 is farthest from the root?

The LCA of n1 and n2 in T is the shared ancestor of n1 and n2 that is located farthest from the root.


Video Answer


1 Answers

  class A {}
  class B is A {}
  class C is B {}
  class D is B {}
  class E is D {}

  say E.^parents.first: * === D.^parents.any
like image 65
wamba Avatar answered Oct 31 '22 00:10

wamba