I was wondering if someone could show me how to turn my code into a recursive method to build my binary tree. I am new to recursion and it doesn't make a whole lot of sense to me.
//=============================================================================================== // BinaryTree constructor - Takes the string and turns it into an array based on parenthesis. // Loops through using a stack and adds children that fall within parent parenthesis //=============================================================================================== public BinaryTree(String inputString) throws InvalidTreeSyntax { Stack<Node> nodeStack = new Stack<>(); String[] inputArray = inputString.substring(1, inputString.length()-1) // remove first & last parenthesis //and split the String into a arr of strings, retain the parenthesis .split("(?<=\\()|(?=\\()|(?<=\\))|(?=\\))"); parentNode = new Node(inputArray[0]); //setting the new first character to the root //starts essentially on the third character of the string for (int i = 1; i < inputArray.length - 1; i++) { //means there is another child. Child becomes parent if one exists if (inputArray[i].equals("(")) { nodeStack.push(parentNode); if (childNode != null) { parentNode = childNode; } //assign the current parent as the child of one on stack } else if(inputArray[i].equals(")")) { try { childNode = parentNode; parentNode = nodeStack.pop(); } catch (EmptyStackException e) { throw new InvalidTreeSyntax("Incorrect parenthesis!"); } //if it gets here, it is a value to be assigned as child to parent. } else { childNode = new Node(inputArray[i]); if (parentNode != null) { parentNode.addChild(childNode); }//addChild with throw InvalidTreeSyntax } }//for every node, will have 2 parenthesis if (this.nodesCheck(parentNode) * 3 != inputString.length()) throw new InvalidTreeSyntax("Incorrect Syntax"); }