Skip to content

Commit 098bcb7

Browse files
.
1 parent 2bd69fa commit 098bcb7

File tree

2 files changed

+113
-2
lines changed

2 files changed

+113
-2
lines changed

Algorithms/Medium_Algorithms.Tests/UnitTest1.cs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -777,10 +777,47 @@ public static bool compare(List<int> arr1, int[] arr2)
777777
}
778778
}
779779
return true;
780-
}
780+
}
781781
#endregion
782782

783+
#region AncestralTree
784+
public Dictionary<char, AncestralTreeClass.AncestralTree> getNewTrees()
785+
{
786+
var trees = new Dictionary<char, AncestralTreeClass.AncestralTree>();
787+
var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
788+
foreach (char a in alphabet)
789+
{
790+
trees.Add(a, new AncestralTreeClass.AncestralTree(a));
791+
}
792+
793+
trees['A'].AddAsAncestor(new AncestralTreeClass.AncestralTree[] {
794+
trees['B'], trees['C'], trees['D'], trees['E'], trees['F']
795+
});
796+
return trees;
797+
}
798+
799+
[Fact]
800+
public void AncestralTreeClassTestCase1()
801+
{
802+
var trees = getNewTrees();
803+
trees['A'].AddAsAncestor(
804+
new AncestralTreeClass.AncestralTree[] { trees['B'], trees['C'] }
805+
);
806+
trees['B'].AddAsAncestor(
807+
new AncestralTreeClass.AncestralTree[] { trees['D'], trees['E'] }
808+
);
809+
trees['D'].AddAsAncestor(
810+
new AncestralTreeClass.AncestralTree[] { trees['H'], trees['I'] }
811+
);
812+
trees['C'].AddAsAncestor(
813+
new AncestralTreeClass.AncestralTree[] { trees['F'], trees['G'] }
814+
);
783815

816+
AncestralTreeClass.AncestralTree yca =
817+
AncestralTreeClass.GetYoungestCommonAncestor(trees['A'], trees['E'], trees['I']);
818+
Assert.True(yca == trees['B']);
819+
}
820+
#endregion
784821

785822

786823

Algorithms/Medium_Algorithms/MediumAlgorithmsClass.cs

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1610,10 +1610,84 @@ public static List<int[]> getUnvisitedNeighbors(int i, int j, int[,] matrix, boo
16101610
}
16111611
return unvisitedNeighbors;
16121612
}
1613-
}
1613+
}
16141614
#endregion
16151615

1616+
#region AncestralTree
1617+
/// <summary>
1618+
///
1619+
/// </summary>
1620+
public class AncestralTreeClass
1621+
{
1622+
public static AncestralTree GetYoungestCommonAncestor(
1623+
AncestralTree topAncestor,
1624+
AncestralTree descendantOne,
1625+
AncestralTree descendantTwo
1626+
)
1627+
{
1628+
int depthOne = getDescendantDepth(descendantOne, topAncestor);
1629+
int depthTwo = getDescendantDepth(descendantTwo, topAncestor);
1630+
if (depthOne > depthTwo)
1631+
{
1632+
return backtrackAncestralTree(descendantOne, descendantTwo,
1633+
depthOne - depthTwo);
1634+
}
1635+
else
1636+
{
1637+
return backtrackAncestralTree(descendantTwo, descendantOne,
1638+
depthTwo - depthOne);
1639+
}
1640+
}
1641+
1642+
public static int getDescendantDepth(AncestralTree descendant, AncestralTree topAncestor)
1643+
{
1644+
int depth = 0;
1645+
while (descendant != topAncestor)
1646+
{
1647+
depth++;
1648+
descendant = descendant.ancestor;
1649+
}
1650+
return depth;
1651+
}
1652+
1653+
public static AncestralTree backtrackAncestralTree(
1654+
AncestralTree lowerDescendant,
1655+
AncestralTree higherDescendant,
1656+
int diff)
1657+
{
1658+
while (diff > 0)
1659+
{
1660+
lowerDescendant = lowerDescendant.ancestor;
1661+
diff--;
1662+
}
1663+
while (lowerDescendant != higherDescendant)
1664+
{
1665+
lowerDescendant = lowerDescendant.ancestor;
1666+
higherDescendant = higherDescendant.ancestor;
1667+
}
1668+
return lowerDescendant;
1669+
}
16161670

1671+
public class AncestralTree
1672+
{
1673+
public char name;
1674+
public AncestralTree ancestor;
1675+
public AncestralTree(char name)
1676+
{
1677+
this.name = name;
1678+
this.ancestor = null;
1679+
}
1680+
// This method is for testing only.
1681+
public void AddAsAncestor(AncestralTree[] descendants)
1682+
{
1683+
foreach (AncestralTree descendant in descendants)
1684+
{
1685+
descendant.ancestor = this;
1686+
}
1687+
}
1688+
}
1689+
}
1690+
#endregion
16171691

16181692

16191693

0 commit comments

Comments
 (0)