Java for English majors

Let me start by saying that I completed a degree in English Literature about 7 years before I completed my degree in Software Engineering. So, when I say "Java for English majors," what I really mean is "Java for Krystal."

I've been using LeetCode to practice coding and today's focus is Binary Tree traversal/manipulation. For this particular exercise, I needed to grab all of the nodes on a level and find their average. Follow this link for an image to help you understand some of the terminology, if you're not already familiar. 

Here's a quick run-through:
  • the circles are called nodes
  • the highest node in the graph is called the root
  • if a circle has another circle beneath it, connected by a line, those circles have a parent/child relationship (respectively)
  • if a circle has multiple circles beneath it, each connected by a line, those attached circles are siblings to one another
  • in a Binary Tree (the kind we're working with), the maximum number of children is 2 (hence 'binary'), referred to as the left and right children
  • each circle/node has specific values associated with it: a value, a link to the left child (if one exists), and a link to the right child (if one exists)
  • sibling nodes exist on the same level, at the same depth, connected to the same parent

Are you following so far? If not, shoot a message over, and I will try to clarify!

So, basically, I was working on finding the averages of all node values at the same depth (the amount associated with the circles on the same levels). I was having the DARNDEST time figuring out the solution. Initially, I misunderstood what the question was asking me to do. I thought I was supposed to find the averages of the sibling pairs and return what was essentially a modified tree. I tried and tried... and tried. This is marked *easy* on the website, by the way. 

Anyway, I looked at their provided solution for guidance. I am not one to accept or provide an answer without understanding it--you know, like those kids in college who submitted answers they found online but didn't really comprehend. I stared at the dang thing and walked through it slowly with a sheet of looseleaf, a pencil, and sample input (fake information to see what the program would compute).

When did it make sense to me? When I started thinking in terms of WORDS and not nodes/queues/lists/insert_some_technical_terminology. So this is why I say this is a post for English majors, because even though I am now trained to write software, I still read it like I would a book.

I used comments to try to explain what was going on. They say that you shouldn't need to comment code because the variables should explain themselves (e.g. List <Double> averages is a list of averages), but I commented everything for the sake of my own learning and possibly yours.


The lesson is this:

When you're writing code, try to write it as though it were a story. It will help others when they are reading your code. It will help you when you're returning to code you haven't seen in a while. When reading code, try to connect concepts to real-world examples that make the content more digestible.

Cheers!
Krystal

Comments

Popular posts from this blog

Let's Git Acquainted

Free Code Camp: CatPhotoApp

Life and Code: An Update