Day 48: Basic Tree in C

Started on implementing a basic binary tree in C. It’s cool since this time I am a tiny bit more familiar with the language and so the example code I have been seeing with all the pointers isn’t as scary. I’m getting in my reps and starting to like doing these exercises that once seemed horrific. Now that’s progress right there!

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. I continued my work on Tree data structures. I started writing an implementation of a binary tree data structure in C I am doing so by following a YouTube Video. I thought about trying to do it without any references and decided I would be asking for problems since I am just here to get some reps with implementing data structures for the first time. For example if you are just starting to exercise at the gym, it does not make a big difference if you start with the machines instead of free weight exercises. If the machines help make it easier to start than go with that. It’s about building momentum and maintaining progress for me.

Binary Tree Implementation in C So Far

/* Basic Tree Implementation in C
 *
 * */

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

// Ironically although all nodes except root have one edge we are 
// constructing our nodes to have edges for children instead of an 
// edge associated with our parents. It feels more natural this way
// although I wonder if there is a benefit to constructing our node
// to have only a pointer to it's parent and build the tree that way
// .
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;
  }

}


int main(){
  treenode* root = createNode(20);

  printf("The root value is: %i", root->value);

  return 0;
}

Outputs:

The root value is: 20

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.