public RestTemplate GetTreeInfo([FromBody] NodeDto root) { BSTree tree = new BSTree(toEntity(root, NodeFactoryImpl.getInstance())); TreeInfo treeInfo = new TreeInfo(); treeInfo.NumberOfLeaves = tree.numberOfLeaves(); NumberOfNodesWrapper wrapper = new NumberOfNodesWrapper(); wrapper.All = tree.numberOfNodes(NodeState.ALL); wrapper.HavingOneChild = tree.numberOfNodes(NodeState.HAVING_ONLY_ONE_CHILD); wrapper.HavingOnlyOneLeftChild = tree.numberOfNodes(NodeState.HAVING_ONLY_LEFT_CHILD); wrapper.HavingOnlyOneRightChild = tree.numberOfNodes(NodeState.HAVING_ONLY_RIGHT_CHILD); wrapper.HavingBothChildren = tree.numberOfNodes(NodeState.HAVING_BOTH_CHILDREN); treeInfo.NumberOfNodes = wrapper; int[] LevelAndNodeCount = new int[tree.findHeight()]; for (int i = 0; i < tree.findHeight(); i++) { LevelAndNodeCount[i] = tree.numberOfNodesAtLevel(i); } treeInfo.AtLevel = LevelAndNodeCount; treeInfo.Height = tree.findHeight(); treeInfo.Min = tree.minimum(); treeInfo.Max = tree.maximum(); treeInfo.MinOfRightChild = tree.minimumOfRightChild(); treeInfo.MaxOfLeftChild = tree.maximumOfLeftChild(); return(new RestTemplate((int)HttpStatusCode.OK, treeInfo, "")); }
public RestTemplate Traverse([FromBody] NodeDto root, string type) { BSTree tree = new BSTree(toEntity(root, NodeFactoryImpl.getInstance())); List <int> turnTo = new List <int>(); BSTTraversal traversal = null; TraversalAction action = new TraversalActionImpl(turnTo); if (type.Equals("preOrder")) { traversal = new PreOrderTraversal(action); } else if (type.Equals("inOrder")) { traversal = new InOrderTraversal(action); } else if (type.Equals("postOrder")) { traversal = new PostOrderTraversal(action); } else { return(new RestTemplate((int)HttpStatusCode.BadRequest, null, "preOrder or inOrder or postOrder is required for type")); } tree.traverse(traversal); return(new RestTemplate((int)HttpStatusCode.OK, turnTo, "")); }
public RestTemplate DeleteX([FromBody] NodeDto root, int x) { BSTree tree = new BSTree(toEntity(root, NodeFactoryImpl.getInstance())); if (tree.findX(x) == null) { return(new RestTemplate((int)HttpStatusCode.Conflict, null, x + " is not found")); } tree.delete(x); return(new RestTemplate((int)HttpStatusCode.OK, toDto(tree.root, 1, 0), "")); }
public RestTemplate GetPathLengthToX([FromBody] NodeDto root, int x) { BSTree tree = new BSTree(toEntity(root, NodeFactoryImpl.getInstance())); int length = tree.findPathLengthToX(x); string message = ""; if (length == -1) { message = x + " doesn't exist"; } return(new RestTemplate((int)HttpStatusCode.OK, length, message)); }