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 10 of 10

Thread: Tree java

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Tree java

    Hi i am supposed to write a code in java which will have as an input a tree.

    Here is my code ( needs to have 3 args):

    public class Tree
    {
    public static void main(String[] args)
    {

    int H = Integer.parseInt(args[0]);
    int n = Integer.parseInt(args[1]);
    int h = Integer.parseInt(args[2]);

    String a = "A";

    while (n > 0)
    {
    while (H > 0)
    {
    System.out.println(a);
    a = "A" + a + "A";
    H -= 1;
    }

    H = Integer.parseInt(args[0]);
    a = a.substring(2, a.length() - 2);
    n -= 1;
    }
    }
    }

    Here is the result:

    A
    AAA
    AAAAA
    AAAAAAA
    AAAAA
    AAAAAAA
    AAAAAAAAA
    AAAAAAAAAAA
    AAAAAAAAA
    AAAAAAAAAAA
    AAAAAAAAAAAAA
    AAAAAAAAAAAAAAA


    I want to know what should i do to make it straight.


  2. #2
    Member Staticity's Avatar
    Join Date
    Jul 2011
    Location
    Texas
    Posts
    105
    My Mood
    Inspired
    Thanks
    3
    Thanked 5 Times in 5 Posts

    Default Re: Tree java

    Hey Andrei,

    Could you please read This?

    After reading, could you also clarify your question? Tell us what you're trying to accomplish, and what problem you are encountering.
    Simplicity calls for Complexity. Think about it.

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Tree java

    public class Tree
    	{
    	    public static void main(String[] args)
    	    {
     
    	    int H = Integer.parseInt(args[0]);
    	    int n = Integer.parseInt(args[1]);
    	    int h = Integer.parseInt(args[2]);
     
    	    String a = "A";
     
    	    while (n > 0)
    	        {
    	        while (H > 0)
    	            {
    	            System.out.println(a);
    	            a = "A" + a + "A";
    	            H -= 1;
    	            }
     
    	        H = Integer.parseInt(args[0]);
    	        a = a.substring(1, a.length() - 1);
    	        n -= 1;
    	        }
               }
    	}

    In java i need to type : java Tree 4 3 2 (this are the arguments)

    So the result is this:

    A
    AAA
    AAAAA
    AAAAAAA
    AAAAA
    AAAAAAA
    AAAAAAAAA
    AAAAAAAAAAA
    AAAAAAAAA
    AAAAAAAAAAA
    AAAAAAAAAAAAA
    AAAAAAAAAAAAAAA


    But I want that my final result would give me a straight tree, not like the one above (too on the left size). Like this:

             	   A
    	         AAA
    	        AAAAA
    	      AAAAAAA
    	        AAAAA
    	      AAAAAAA
    	    AAAAAAAAA
    	   AAAAAAAAAAA
    	     AAAAAAAA
    	   AAAAAAAAAAA
    	  AAAAAAAAAAAAA
    	AAAAAAAAAAAAAAA
    Last edited by Andrei; February 12th, 2012 at 01:08 AM.

  4. #4
    Member Staticity's Avatar
    Join Date
    Jul 2011
    Location
    Texas
    Posts
    105
    My Mood
    Inspired
    Thanks
    3
    Thanked 5 Times in 5 Posts

    Default Re: Tree java

    I suppose by a "straight tree", you want your lines to be aligned in the center.. What have you tried?
    Simplicity calls for Complexity. Think about it.

  5. #5
    Junior Member
    Join Date
    Feb 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Tree java

    yes that is correct, i did not try anything yet, because im not sure where to start.

  6. #6
    Member Staticity's Avatar
    Join Date
    Jul 2011
    Location
    Texas
    Posts
    105
    My Mood
    Inspired
    Thanks
    3
    Thanked 5 Times in 5 Posts

    Default Re: Tree java

    Do you notice any pattern occurring in the program? What is the correlation between the # of A's and the # of spaces that should be produced?
    Simplicity calls for Complexity. Think about it.

  7. #7
    Junior Member
    Join Date
    Feb 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Tree java

    as the number of As is increasing the number of spaces are decreasing ? 2n-1?

  8. #8
    Member Staticity's Avatar
    Join Date
    Jul 2011
    Location
    Texas
    Posts
    105
    My Mood
    Inspired
    Thanks
    3
    Thanked 5 Times in 5 Posts

    Default Re: Tree java

    Depending on how far you want to space your tree, you are correct. There is an inverse relationship between the A's and spaces.

    So in my eyes, if your greatest length of A's (which would be the bottom row) is 13, you would have 0 spaces. If you have a length of 11, you should have 2 spaces, length of 1 would have 12.

    Now, how would you consider approaching this problem? There are an infinite number of ways to solve a problem.
    Simplicity calls for Complexity. Think about it.

  9. #9
    Junior Member
    Join Date
    Feb 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Tree java

    do i need the specify the total lenght in the loop and put H< spaces?

  10. #10
    Member Staticity's Avatar
    Join Date
    Jul 2011
    Location
    Texas
    Posts
    105
    My Mood
    Inspired
    Thanks
    3
    Thanked 5 Times in 5 Posts

    Default Re: Tree java

    I'm sorry, I don't quite understand your question. Could you clarify please?
    Simplicity calls for Complexity. Think about it.

Similar Threads

  1. B+ tree
    By cms in forum Algorithms & Recursion
    Replies: 2
    Last Post: December 15th, 2011, 07:58 AM
  2. Java Binary Tree (Beginners)?
    By IAmHere in forum Algorithms & Recursion
    Replies: 6
    Last Post: June 19th, 2011, 10:28 AM
  3. Java Binary Tree
    By comwizzz in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 28th, 2011, 03:15 PM
  4. Binary Search Tree in Java [HELP]
    By alan in forum Algorithms & Recursion
    Replies: 2
    Last Post: February 5th, 2011, 06:44 AM
  5. Data Structures(Binary Search Tree to AVL Tree)ASAP
    By jfAdik in forum Algorithms & Recursion
    Replies: 2
    Last Post: April 5th, 2010, 03:58 AM