Day 53: First Off the Top Recursive Function

Still working on my basic tree data structure implementation. I was working on my “Pretty Print” function when I realized I needed to build a depth calculation function to see through my idea. I won’t lie, my code feels kind of NICE. I think it could be cleaner but it was my first real world (purely off the top of my head) recursive function so I’m happy. I’m slowly starting to implement all these things I have been learning it feels great but the journey continues.

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. I have started to build some tooling by adding an unnecessary but cool function that will use a Preorder Traversal and then pretty print the tree to the terminal/console (whatever it’s called LOL). I built a depth calculation function as a utility function for the “Pretty Print” function I am trying to build. It went well. I went with a recursive design since that seems to be a recurring theme with trees. It seems to have gone really well. All the tests I ran produced the correct value so I feel pretty stoked. I didn’t look at any examples or use any references. Everything came from my own head and previous references I had memorized. Feels good.

Current Code with tons of comments LOL

/* Basic Tree Implementation in C
 * 
 * Improvement 1: Improved Print function ("Pretty Print")
 *
 * */

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

int calcDepth(treenode* node){
  int leftDepth = 0, rightDepth = 0;
  
  if(node == NULL)
    return 0;
  
  if(node->left != NULL)
    leftDepth = 1 + calcDepth(node->left);

  if(node->right != NULL)
    rightDepth = 1 + calcDepth(node->right);

  return leftDepth >= rightDepth ? leftDepth : rightDepth;
}

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

  // Test my depth calculation function
  // Current tree Depth = 2
  printf("Depth of tree is: %i", calcDepth(root));


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

  return 0;
}

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.