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

Thread: print space

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default print space

    Hi, would you mind helping me in solving this problem?
    Because I stuck now. The problem is to print a space in a binary tree (not binary search tree)
    my current program can only output without appropriate spacing
    so if input is : 4 7 8 9 2 0 3 1 5
    the output will become
    4
    7 8
    9 2 0 3
    1 5

    What the expected output is
                 4
           7           8
        9    2      0    3
      1  5


    actually the "/" space is not really compulsory, the problem is to print spaces

    Here is my code contains 2 classes: TreeNode class and BinaryTree class


    import java.io.*;
    public class TreeNode 
    {
        public Object da;
        public TreeNode left;
        public TreeNode right;
        public TreeNode(Object newItem) 
        {
            da = newItem;
            left = null;
            right = null;
        }
        public TreeNode(Object newItem, TreeNode leftNode, TreeNode rightNode) 
        {
            da = newItem;
            left = leftNode;
            right = rightNode;
        }
     
        public void setItem(Object newItem) 
        { 
            da = newItem; 
        }
        public Object getItem() 
        { 
            return da; 
        }
        public void setLeft(TreeNode leftNode) 
        { 
            left = leftNode; 
        }
        public TreeNode getLeft() 
        { 
            return left; 
        }
        public void setRight(TreeNode rightNode) 
        { 
            right = rightNode; 
        }
        public TreeNode getRight() 
        { 
            return right; 
        }
        public int left (int i) 
        {  
            return 2*i;  
        } 
        public int right (int i) 
        {  
            return 2*i + 1;  
        } 
        public int parent (int i) 
        {  
            return (i-1)/2;
        }
    }

     
    import java.util.*;
     class BinaryTree
    {
     
        public static void main(String args[])
        {
            Scanner input = new Scanner(System.in);
            System.out.println("Enter the values to insert into the binary tree: ");
            String[] values = (input.nextLine()).split("\\s");
     
        TreeNode root = null;
        Queue queue = new LinkedList();
        TreeNode parent = null;
        for (int index = 0; index < values.length; index++)
        {
            if (root == null)
            {
                    root = new TreeNode(values[index]);
            queue.add(root);
            }
            else
            {
            TreeNode node = new TreeNode(values[index]);
            if (index % 2 == 1) {
                parent = (TreeNode) queue.remove();
                parent.setLeft(node);
            }
     
            else
            {
                parent.setRight(node);
            }
            queue.add(node);
            }
        }
        queue = new LinkedList();
        queue.add(root);
        int maxNodes = 1;
        int countNodes = 0;
        while (queue.size() > 0 )
        {
            TreeNode node = (TreeNode) queue.remove();
     
            System.out.print(node.getItem().toString() + " ");
            countNodes++;
            if (countNodes == maxNodes) 
            {
            System.out.println();
                    maxNodes = maxNodes * 2;
            countNodes = 0;
            }
                TreeNode leftChild = node.getLeft();
            if (leftChild != null)
                queue.add(leftChild);
            TreeNode rightChild = node.getRight();
            if (rightChild != null)
                queue.add(rightChild);
        }
        System.out.println();
        }
    }

    It can’t print like the real binary tree and I do not know how to make it accordingly.
    Any suggestions?
    Thanks a lot. =)
    Last edited by brainTuner; March 31st, 2010 at 06:22 AM.


  2. #2
    Member wolfgar's Avatar
    Join Date
    Oct 2009
    Location
    the middle of the woods
    Posts
    89
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: print space

    could u explain what it should look like?
    Programming: the art that fights back

  3. #3
    Junior Member
    Join Date
    Mar 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: print space

    I have already edited my question above, hope you have better understanding about my problem. Thanks -)

  4. #4
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: print space

    Here is something to look at,
                   1                 = (2^(n-c))-1
           2               2	 = (2^(n-c))-1, (2^(n-(c-1)))-1,
       3       3       3       3     = (2^(n-c))-1, (2^(n-(c-1)))-1,...
     4   4   4   4   4   4   4   4	 = (2^(n-c))-1, (2^(n-(c-1)))-1,...
    5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5  = (2^(n-c))-1, (2^(n-(c-1)))-1,...
    What this is showing you is that the first space on each line is determined by the equation
    (2^(n-c))-1

    Where n = number of layers, demo has 5 layers.
    c = current layer, i.e 1-5 from the demo.

    and any spaces following that, so the space after each character is of length
    (2^(n-(c-1)))-1

    Hope all that makes sense.

    The demo doesn't line up right, because of the text used on this forum lol!

    Regards,
    Chris

  5. #5
    Junior Member
    Join Date
    Mar 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: print space

    hmm, the algorithm you gave was quite helping
    But how about if the 'c' are random integers (i.e. they are not only from 1-5)?

    Thank you

  6. #6
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: print space

    the c is the current level, so it will work for any size tree that follow the format you gave

  7. #7
    Junior Member
    Join Date
    Mar 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: print space

    any idea of printing by using Queue?
    I am using levl ordr trversal to print..
    since this seems to print by using Array, doesn't it?

  8. #8
    Member wolfgar's Avatar
    Join Date
    Oct 2009
    Location
    the middle of the woods
    Posts
    89
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: print space

    start from the bottom and work your way up

    i dont remember what i made but once i needed to do that
    and did so useing a reverse loop ( one the de-increments )
    and for each layer add X number of spaces for each layer up it is

    String[] layers = the layers 0 being the top ;
    String[] newLayers = set to the number of layers in the layers array;
    for ( int i = layers.size(); i >= 0 ; i-- ){
        for ( int j = layers.size() - i; j > 0; j ++){
          newLayers[i] += padding;
       }
       newLayers[i] += layers[i];
    }

    that is just for the edges , but the internal padding could be done similar
    Programming: the art that fights back

Similar Threads

  1. Replies: 15
    Last Post: February 28th, 2010, 10:30 PM
  2. Java heap space error
    By Loki in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 27th, 2010, 10:47 PM
  3. Out of memory - Java heap space
    By fraxx in forum Java Theory & Questions
    Replies: 4
    Last Post: November 24th, 2009, 05:26 AM
  4. OutOfMemoryError (Java heap space)
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 21st, 2009, 11:56 AM
  5. how do you leave a line space in java?
    By ss7 in forum Java Theory & Questions
    Replies: 7
    Last Post: November 5th, 2009, 08:20 AM