Day 56: Multidimensional Char Arrays in C..

I was able to complete the Vertical Tree Pretty Print function. It’s not half bad but when I started working on the horizontal print I was lost again. I think the difference is clearly in the usage of char arrays to store and arrange the output before actually printing it. I am still a freshy when it comes to C so I need to take things a bit slower and try to really breakdown my problem into tangible small steps. So far so good. I haven’t finished yet but I feel better about my approach.

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 was able to successfully replicate the vertical print in C. My code looks very similar to the actual example. It was cool to see that since I didn’t look at it until I was successful with my own implementation. I started working on the horizontal pretty print but I will need more time. I am still fairly unfamiliar with this type of task. I have to work slower with more tests to ensure I stay motivated and engaged. Too much repeated failure always makes me feel stuck and reluctant to continue working.

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.

Vertical Output Example

                              15

                    7

                              14

          3

                              13

                    6

                              12

1

                              11

                    5

                              10

          2

                              9

                    4

                              8

So far this is my code:

#include <stdio.h>
#include <stdlib.h>

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

void prettyPrintVertRec(treenode* root, int level){
  if(root == NULL) return;

  prettyPrintVertRec(root->right, 1 + level);

  for(int i = 0; i < level; ++i)
    printf("\t");

  printf("%i\n", root->value);
  prettyPrintVertRec(root->left, 1 + level);
}

void prettyPrintVert(treenode* root){
  if(root == NULL){
    printf("Empty Tree\n");
    return;
  }
  printf("--------------------\n\n");

  prettyPrintVertRec(root->right, 1);
  printf("%i\n", root->value);
  prettyPrintVertRec(root->left, 1);

  printf("\n--------------------");
}

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 Pretty Print Tree Vertically
  prettyPrintVert(root);
}

And this is what I am producing from my code:

--------------------

        3
1
                5
        2
                4

--------------------

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.