public void DoTest4()
        {
            var top = new AncestralTree('A');
            var B   = new AncestralTree('B');
            var C   = new AncestralTree('C');

            top.AddAsAncestor(new AncestralTree[] { B, C });

            var D = new AncestralTree('D');
            var E = new AncestralTree('E');

            B.AddAsAncestor(new AncestralTree[] { D, E });

            var F = new AncestralTree('F');
            var G = new AncestralTree('G');

            C.AddAsAncestor(new AncestralTree[] { F, G });

            var H = new AncestralTree('H');
            var I = new AncestralTree('I');

            D.AddAsAncestor(new AncestralTree[] { H, I });



            Assert.That(GetYoungestCommonAncestor(top, H, G).name, Is.EqualTo('A'));
        }
        public static AncestralTree GetNthAncestor(AncestralTree person, int distance)
        {
            while (distance != 0)
            {
                person = person.ancestor;
                --distance;
            }

            return(person);
        }
Exemple #3
0
        public static int getDescendantDepth(AncestralTree descendant, AncestralTree topAncestor)
        {
            int depth = 0;

            while (descendant != topAncestor)
            {
                depth++;
                descendant = descendant.ancestor;
            }
            return(depth);
        }
        public static int GetDept(AncestralTree person)
        {
            int dept = 0;

            while (person.ancestor != null)
            {
                person = person.ancestor;
                ++dept;
            }

            return(dept);
        }
        public static List <AncestralTree> GetLineage(AncestralTree person, List <AncestralTree> lineage)
        {
            if (person.ancestor == null)
            {
                lineage.Add(person);
                return(lineage);
            }

            var theLineage = GetLineage(person.ancestor, lineage);

            lineage.Add(person);
            return(theLineage);
        }
 public static AncestralTree backtrackAncestralTree(AncestralTree lowerDescendant, AncestralTree higherDescendant, int diff)
 {
     while (diff > 0)
     {
         lowerDescendant = lowerDescendant.ancestor;
         diff--;
     }
     while (lowerDescendant != higherDescendant)
     {
         lowerDescendant  = lowerDescendant.ancestor;
         higherDescendant = higherDescendant.ancestor;
     }
     return(lowerDescendant);
 }
        public static AncestralTree GetYoungestCommonAncestor(
            AncestralTree topAncestor,
            AncestralTree descendantOne,
            AncestralTree descendantTwo
            )
        {
            int depthOne = getDescendantDepth(descendantOne, topAncestor);
            int depthTwo = getDescendantDepth(descendantTwo, topAncestor);

            if (depthOne > depthTwo)
            {
                return(backtrackAncestralTree(descendantOne, descendantTwo, depthOne - depthTwo));
            }
            else
            {
                return(backtrackAncestralTree(descendantTwo, descendantOne, depthTwo - depthOne));
            }
        }
        public static AncestralTree GetYoungestCommonAncestor(
            AncestralTree topAncestor,
            AncestralTree descendantOne,
            AncestralTree descendantTwo
            )
        {
            var lineageOne = GetLineage(descendantOne, new List <AncestralTree>());
            var lineageTwo = GetLineage(descendantTwo, new List <AncestralTree>());
            var lastCommon = topAncestor;
            int i          = 0;

            while (i < Math.Min(lineageOne.Count, lineageTwo.Count) && lineageOne[i] == lineageTwo[i])
            {
                lastCommon = lineageOne[i];
                ++i;
            }

            return(lastCommon);
        }
        public static AncestralTree GetYoungestCommonAncestor(
            AncestralTree topAncestor,
            AncestralTree descendantOne,
            AncestralTree descendantTwo
            )
        {
            int deptOne = GetDept(descendantOne);
            int deptTwo = GetDept(descendantTwo);
            var yougest = deptOne > deptTwo ? descendantOne : descendantTwo;
            var oldest  = deptOne > deptTwo ? descendantTwo : descendantOne;
            int distanceFromYoungest = Math.Max(deptOne, deptTwo) - Math.Min(deptOne, deptTwo);
            var sameLevelAncestor    = GetNthAncestor(yougest, distanceFromYoungest);

            while (oldest.name != sameLevelAncestor.name)
            {
                oldest            = oldest.ancestor;
                sameLevelAncestor = sameLevelAncestor.ancestor;
            }

            return(oldest);
        }
 public AncestralTree(char name)
 {
     this.name     = name;
     this.ancestor = null;
 }