Day 55: Tree Vertical Pretty Print Example

I started trying to reproduce the example binary tree pretty prints from the article I’m reading. Let me tell, it’s not going well. First I got hung up on a misplaced ; , now my tab formatting is off. It’s a great first step but wow I am familiar with this type of work. Formatting console text is something I have always taken for granted. Never again! lol

TLDR;

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

  • Continued 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. It has been a struggle but I am making some progress. I am replicating the Vertical Tree print first and then I will try to replicate the horizontal one next. After that I might actually be able to produce the grid based print concept I have in my head. Only time and effort will get me there though.

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;

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

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

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

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

}

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

2
                        5
                4

Yeah… So I got some work to do LOL.


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.