Beispiel #1
0
        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, ""));
        }
 // O(n) time | O(n) space
 public static List <int> PostOrderTraverse(BSTTraversal tree, List <int> array)
 {
     if (tree.left != null)
     {
         PostOrderTraverse(tree.left, array);
     }
     if (tree.right != null)
     {
         PostOrderTraverse(tree.right, array);
     }
     array.Add(tree.value);
     return(array);
 }