Reputation: 127
I have some a string which needed to be evaluated in following pattern -
Input => String testTree = "(1(2(4)())(3))";
Output => {1,2,3,4,*,*,*}
Input => String testTree2 = "(1(2(4)(5))(3()(4()(3))))";
Output => {1,2,3,4,5,*,4,*,*,*,*,*,*,*,3}
Input => String testTree3 = "(1()())";
Output =>{1,*,*}
Above mention inputs have same pattern , where '()' or null is represented as '*'.Output is been saved to a int/string array , hence output form is like so.
Kindly suggest a method to evaluate such kind of pattern to the output form.
Upvotes: 1
Views: 72
Reputation: 7255
You could use the Interpreter pattern to read the input into a binary tree class.
Upvotes: 1