Optimize the GetDescendants and GetAncestors methods

Time complexity of the previous methods is O(N*N/2) slower than O(N) of
the new methods