public OrbitalBody GetCommonAncestor(OrbitalBody bodyOne, OrbitalBody bodyTwo) { // build complete list first. Dictionary <string, int> firstPathDistances = new Dictionary <string, int>(); OrbitalBody curBody = bodyOne.GetParent(); int curDist = 0; while (curBody != null) { firstPathDistances.Add(curBody.id, curDist); curDist++; curBody = curBody.GetParent(); } int totalDist = 0; // now do the other one (find the common one first curDist = 0; curBody = bodyTwo.GetParent(); while (curBody != null) { if (firstPathDistances.ContainsKey(curBody.id)) { // ancestor in common. totalDist = firstPathDistances[curBody.id] + curDist; return(curBody); } curDist++; curBody = curBody.GetParent(); } return(null); }
public int TraverseNodesUp(List <OrbitalBody> bottomNodes) { int bodyCount = 0; HashSet <string> bodyNames = new HashSet <string>(); // used names for (int intI = 0; intI < bottomNodes.Count; intI++) { OrbitalBody curBody = bottomNodes[intI]; while (curBody.GetParent() != null) { if (!bodyNames.Contains(curBody.id)) { bodyCount++; //bodyNames.Add(curBody.id); } curBody = curBody.GetParent(); } } return(bodyCount); }