Drawing fir tree

Found this task somewhere on the web and decided to make brain massage. This was asked on some software interview.

Input

Look at fir tree and write a program that print it, depending on only one input parameter - number of rows rowNum.
  /\  
 /**\ 
/****\

Thinking of

Let's look at above fir tree.

  1. We definitely see symmetry here. Symmetry makes me feel that we need to handle left and right parts in opposite way. 
  2. Four different symbols used for drawing - space, slash, start, backslash. Slash and backslash are symmetrical and applicable only on left and right sides accordingly. Space and star are independent of position.
  3. Right side ends at backslash - no need to print anything after backslash symbol, thus no need spaces on right side.

But optionally we can print it for symmetry like that, of course, if imagine, that underscores are spaces:
__/\__
_/**\_
/****\

Lets look at left side, slasg symbol. This slash will be kind of boundary, because if we did not reach condition yet to print slash - we generally print spaces. If we jumped over that condition - we print stars, generally.

so there are three logical boundaries

  1. left / right sides. common length of line is 2 * rowNum so rowNum first is for left, rowNum second is for right side of fir tree.
  2. before slash / slash / after slash for left side
  3. before backslash \ backslash \ after backslash

We will draw fir tree in rowNum lines // for rowPos ... rowNum
With 2 * rowNum symbols max in each line // for symPos ... 2 * rowNum
1st boundary is a simple condition // if symPos < rowNum
2nd more interesting one, which is depending on rowNum and rowPos, symPos == rowNum - rowPos - 1
3rd analogically the same as 2nd, but inverted from the end, i == n + j

Solution in code

    private void printFirTree(int rowNum) {
        for (int rowPos = 0; rowPos < rowNum; rowPos++) {
            for (int symPos = 0; symPos < 2 * rowNum; symPos++) {
                if (symPos < rowNum) { // left side
                    if (symPos < rowNum - rowPos - 1) {
                        System.out.print(" ");
                    }
                    if (symPos == rowNum - rowPos - 1) {
                        System.out.print("/");
                    }
                    if (symPos >= rowNum - rowPos) {
                        System.out.print("*");
                    }
                } else { // right side
                    if (symPos < rowNum + rowPos) {
                        System.out.print("*");
                    }
                    if (symPos == rowNum + rowPos) {
                        System.out.print("\\");
                    }
                    if (symPos > rowNum + rowPos) { // we can print spaces here of just omit this if
                        System.out.print(" ");
                    }
                }
            }
            System.out.println();
        }
    }

Result

with rowNum = 3
  /\  
 /**\ 
/****\

with rowNum = 10
         /\         
        /**\        
       /****\       
      /******\      
     /********\     
    /**********\    
   /************\   
  /**************\  
 /****************\ 
/******************\

Popular Posts