Day 58: Got the Spacing Calculation.. I Think

Now that I had the hard coded version as an example I started to play around with dynamically setting the formatting for the tree. I got the initial spacing down thanks to some good old math and some help from Geometric Sequences I KNEW THAT BAD BOY would come back up. So happy I took the time to get familiar with it way back when I was working on learning Big O Notation properly. Felt good, I can’t lie. Calculating the space between tree values now simply requires the subtraction of 1 from the sequential value at each level I think. I still need to tests this but slowly I’m breaking down this stuff. Feels great!

TLDR;

Okay, so here are the highlights of what I did:

  • Continued to work on the “Technical Questions” section of the book “Cracking the Coding Interview” by Gayle Laakmann McDowell. Within that, I continued my work on Tree data structures. Started trying to replicate the example pretty print functions provided in the article I am reading through. I am still trying to replicate the horizontal pretty print that was introduced in the article. I was able to write out the code to produce dynamic spacing values for each level of the tree. The spacing needed is represented by a geometric sequence. The base space unit being a = 3 for now and the common ratio being 2. Lastly it must be reversed to be produce the most spacing at the top based on the overall height of the tree. So it’s all starting to come together which feels great. In my hardcoded tree the progress goes lvl 1 = 12 spaceslvl 2 = 6 spaces, and level 3 = 3 spaces. So far so good. Now to get those in-between spaces set up.
  • I read through chapter 2 of “Eloquent JavaScript” by Marjin Haverbeke. I am starting to read the book to help cover some insecurities I have about my knowledge of the language. I learned JavaScript on the fly through projects and YouTube videos. There are some core questions about the language that I don’t think I can answer, so hopefully this will help. I got to the exercise where the apparently famous **”FizzBuzz”**coding problem was introduced. I will tackle that tomorrow and see how well I fair.

Reference Article

I am reading through this article’s example “Pretty Print” for a tree. Thankfully they have multiple forms that I can learn from.

Horizontal Output Example

                                 1

                2                                3

        4                5                6                7

    8        9        10        11        12        13        14        15

So far this is my code (relevant functions only to keep it cleaner looking):

/* Basic Tree Implementation in C
 *
 * Improvement 1.2: Horizontal Pretty Print
 * Step 1: Get comfortable with the C language features for task
 * Step 2: Build a hard coded version of the desired output
 * Step 3: Start making aspects of the code dynamic
 *
 * */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>    // Include string manipulation library??
#include <math.h>      // Include math manipulation library??

typedef struct treenode {
  int value;
  struct treenode* left;
  struct treenode* right;
} treenode;

treenode* createNode(int value){
  treenode* result = malloc(sizeof(treenode));
  if(result != NULL){
    result->value = value;
    result->left = NULL;
    result->right = NULL;
  }
  return result;
}

int calcHeight(treenode* node){
  int leftHeight = 0, rightHeight = 0;

  if(node == NULL)
    return 0;

  if(node->left != NULL)
    leftHeight = 1 + calcHeight(node->left);

  if(node->right != NULL)
    rightHeight = 1 + calcHeight(node->right);

  return leftHeight >= rightHeight ? leftHeight : rightHeight;
}

void printSpace(int multiple, int spaceValue){
  int power = (int) pow(2, multiple);
  for(int i=0; i < power * spaceValue; ++i)
    printf(" ");
}

void prettyPrintHoriz(treenode* root){
  if(root == NULL){
    printf("Empty Tree\n");
    return;
  }
  int width = 24;
  int height = calcHeight(root);
  const int SPACE = 3;

  char treeDiagram[height+1][width];

  printf("\n");
  for(int i = 0; i < (height+1); ++i){
   printSpace(height - i, SPACE);
   printf("0\n\n");
  }

  strcpy(treeDiagram[0], "            1\n");
  strcpy(treeDiagram[1], "      2           3\n");
  strcpy(treeDiagram[2], "   4     5\n");

  for(int i = 0; i < height+1; ++i)
    printf("\n%s", treeDiagram[i]);
}

int main(){
  treenode* root = createNode(1);
  treenode* n2 = createNode(2);
  treenode* n3 = createNode(3);
  treenode* n4 = createNode(4);
  treenode* n5 = createNode(5);

  root->left = n2;
  root->right = n3;
  n2->left = n4;
  n2->right = n5;
  // Structure of current tree:
  //
  //       root
  //      /    \
  //    n2      n3
  //           /  \
  //         n4    n5

  //      1
  //     / \
  //    2   3
  //   / \
  //  4   5

  // Testing Horizontal Pretty Print
  prettyPrintHoriz(root);

  free(root);
  free(n2);
  free(n3);
  free(n4);
  free(n5);

  return 0;
}

And this is what I am producing from my code:

            0

      0

   0


            1

      2           3

   4     5


Conclusion

That’s all for today. This is my sixth round of the “#100daysofcode” challenge. I will be continuing my work from round five into round six. I am currently working through the book “Cracking the Coding Interview” by Gayle Laakmann McDowell. My goal is to become more familiar with algorithms and data structures. This goal was derived from my goal to better understand operating systems and key programs that I use in the terminal regularly e.g. Git. This goal was in term derived from my desire to better understand the fundamental tools used for coding outside of popular GUIs. This in turn was derived from my desire to be a better back-end developer.

I have no idea if my path is correct but I am walking down this road anyways. Worst case scenario I learn a whole bunch of stuff that will help me out on my own personal projects.