Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Help with recursion

  1. #1
    Junior Member
    Join Date
    Sep 2021
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with recursion

    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");
    	}

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,792
    Thanks
    57
    Thanked 2,687 Times in 2,637 Posts

    Default Re: Help with recursion

    How can the code be compiled and executed for testing?
    It is missing many parts including a main method and an input String.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jun 2022
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with recursion

    great video on how to do recursive programming (dynamic programming) the right way

    https://youtu.be/oBt53YbR9Kk

Similar Threads

  1. RECURSION
    By kevthanewversi in forum Algorithms & Recursion
    Replies: 5
    Last Post: April 21st, 2013, 06:56 AM
  2. Recursion
    By maple1100 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 3rd, 2013, 01:49 AM
  3. Recursion Help
    By WeaKeN in forum Algorithms & Recursion
    Replies: 10
    Last Post: May 27th, 2011, 08:17 AM
  4. [SOLVED] Recursion help
    By Actinistia in forum Java Theory & Questions
    Replies: 3
    Last Post: March 21st, 2011, 12:26 PM
  5. Recursion
    By javapenguin in forum Algorithms & Recursion
    Replies: 12
    Last Post: October 18th, 2010, 03:42 PM