public async Task <ActionResult <BTreeLowestCommonAncestorResponseDTO> > GetLowestCommonAncestor( [FromServices] BTreeLowestCommonAncestorRequestDTO modelDto, string uuid, int numberA, int numberB) { try { modelDto.UUID = uuid; modelDto.NumberA = numberA; modelDto.NumberB = numberB; BTreeLowestCommonAncestorResponseDTO response = await _binaryTreeService.GetBTreeLowestCommonAncestorAsync(modelDto); return(Ok(response)); } catch (NullReferenceException e) { System.Diagnostics.Debug.WriteLine(e.ToString()); return(NotFound(e.Message)); } catch (Exception e) { System.Diagnostics.Debug.WriteLine(e.ToString()); return(Problem(e.Message, null, 500)); } }
public async Task <BTreeLowestCommonAncestorResponseDTO> GetBTreeLowestCommonAncestorAsync(BTreeLowestCommonAncestorRequestDTO lowestAncestorDTO) { BTree binaryTree = await _binaryTreeRepository.GetBTreeAsync(lowestAncestorDTO.UUID); List <Node> nodePathNumberA = binaryTree.GetNodePath(lowestAncestorDTO.NumberA); List <Node> nodePathNumberB = binaryTree.GetNodePath(lowestAncestorDTO.NumberB); int minNodesBetweenTwoNodePaths = Math.Min(nodePathNumberA.Count, nodePathNumberB.Count); Node lowestCommonAncestor = null; for (int i = 0; i < minNodesBetweenTwoNodePaths - 1; i++) { if (nodePathNumberA[i].Equals(nodePathNumberB[i])) { lowestCommonAncestor = nodePathNumberA[i]; } } if (lowestCommonAncestor == null) { throw new NullReferenceException($"Lowest common ancestor between {lowestAncestorDTO.NumberA} and {lowestAncestorDTO.NumberB} was not found!"); } BTreeLowestCommonAncestorResponseDTO response = new BTreeLowestCommonAncestorResponseDTO(); response.LowestCommonAncestor = lowestCommonAncestor.Data; return(response); }