# Representing a Binary Tree in Go Code

In this video we learn how to represent a binary tree in Go code. Once we have the basic structure in place, we then go a step further and define an input file format that we can use to read in arbitrary binary trees moving forward to test our algorithms with.

This post is part of the Let's Learn Algorithms series where we learn how algorithms work, see how to implement them, and then spend some time working on practice problems to reinforce how the implementation details work, as well as to help you learn to recognize problems that could be solved using any particular algorithm.

The series is inspired by feedback on reddit and based on my personal experience with teachers and programming team coaches that I had the pleasure of working with at UCF. If you have any feedback, feel free to reach out - jon@calhoun.io.

You can view all of the videos in the Let's Learn Algorithms Graph Theory subsection to see if there is one covering a particular topic you are interested in. You can also check out the transcripts for the video below.

• 0:00 - before we can move on to implementing a
• 0:02 - DFS we first need to look at how we
• 0:04 - might represent trees in our code now
• 0:06 - this section might be a little bit
• 0:09 - confusing towards the end but I think
• 0:11 - it's important to sort of follow through
• 0:13 - with it because I want to give you a way
• 0:15 - to you read different types of trees in
• 0:17 - your code rather than just having the
• 0:18 - same tree and using it every example I
• 0:20 - want to be able to give you a couple
• 0:21 - trees that you can actually test your
• 0:23 - code and see if it works so in this
• 0:25 - section we're not only going to show
• 0:27 - what code I would use to represent the
• 0:28 - tree in our code but will also go over a
• 0:31 - file input format that we can use to you
• 0:33 - know read the different trees and that
• 0:36 - way we can work with many different
• 0:37 - trees make sure our codes working
• 0:38 - correctly and really get a feel for how
• 0:44 - just how do we represent a tree like
• 0:46 - this in our code so you can see that
• 0:48 - this isn't obviously if you it's not
• 0:50 - like a complete tree there's not a leaf
• 0:52 - for every single node but there's only
• 0:54 - just four nodes in it and we've got this
• 0:55 - root note of eight and we've got it has
• 0:57 - two children the for the 12 and then the
• 1:00 - four has one child the six but it
• 1:02 - doesn't have a left child and the 12 on
• 1:04 - the other hand has no children same with
• 1:06 - the six so the first thing I want to
• 1:09 - note is that each vertex or node has a
• 1:13 - value in this case they're all integers
• 1:15 - so we're going to move forward just
• 1:16 - assuming they're integers so we're going
• 1:18 - to create this type node it's going to
• 1:20 - be a struct and it's going to have a
• 1:22 - value field that represents the integer
• 1:24 - value now that's you know pretty easy to
• 1:28 - get by the next part we're going to talk
• 1:30 - about is is a little bit harder and it's
• 1:31 - how do you represent these children for
• 1:33 - each different nodes so this eight has
• 1:35 - two children and we need to sort of
• 1:36 - represent that so to do this we're going
• 1:39 - to have a left and a right child and our
• 1:41 - struct and they're both going to be
• 1:42 - pointers to node the reason they're
• 1:45 - going to be pointers is that there are
• 1:46 - cases like the for the 6 and the 12
• 1:49 - where one of these children might not be
• 1:52 - present and in that case we need to
• 1:54 - represent it with nil so you can see
• 1:56 - here that fours left child is nil six is
• 1:59 - left and right or both nil and 12s left
• 2:01 - and right or both know so by using
• 2:03 - pointers for these two values we can set
• 2:06 - them to nil whenever one's not present
• 2:10 - the leaf nodes are the easiest ones to
• 2:15 - initialize and to visualize it first
• 2:17 - because they don't rely on other nodes
• 2:19 - being present you can see here in this
• 2:21 - code snippet that we can construct this
• 2:23 - six node really easily because we just
• 2:25 - set the left and right to nil and we
• 2:27 - don't have to have any other nodes
• 2:28 - present other nodes like this for are a
• 2:34 - little bit trickier because we either
• 2:36 - need to have the six node already
• 2:38 - instantiated when we create the four so
• 2:39 - that we can assign that you know that
• 2:41 - the link on the right hand side or we
• 2:44 - need to assign it after the six has been
• 2:46 - created so in this case we create the
• 2:49 - four and it has a value of four and then
• 2:52 - we create the six node and it has a
• 2:53 - value of six and then after all the
• 2:55 - nodes are created we set fours right
• 2:58 - hand child so for right we set it to six
• 3:01 - so in this case it the pointer on the
• 3:03 - right hand side is now pointing to the
• 3:04 - six node
• 3:10 - so as I said rather than hard coding out
• 3:12 - this graph and doing like we did right
• 3:14 - here in this example rather than doing a
• 3:16 - bunch of code like this where we have to
• 3:17 - manually create every single node we're
• 3:20 - going to look at a way of reading in an
• 3:21 - input file and being able to create a
• 3:24 - graph from it so I'm going to go through
• 3:27 - all of this here at a moment and I've
• 3:28 - tried to color coded a little bit and if
• 3:30 - there's I assume that some of you might
• 3:32 - be colorblind and I apologize for that
• 3:34 - I'm going to try to use arrows and talk
• 3:37 - and highlight things so that's a little
• 3:39 - bit easier to follow along but I'm going
• 3:41 - to do my best but what I want to show
• 3:43 - you is that the sample input on the
• 3:44 - bottom right is what we're trying to
• 3:45 - create this this node has or this graph
• 3:48 - has five nodes they have the values 1 2
• 3:51 - 3 4 & 6 so you can see here that for has
• 3:56 - two children the two and the six values
• 3:58 - and two has two children the one the
• 4:00 - three values all right so reading
• 4:04 - through the format of the data the first
• 4:06 - rule is that the first number we're
• 4:08 - always going to get in our input is N
• 4:09 - and this represents the number of nodes
• 4:12 - in our graph so you can see here that
• 4:14 - this n number is the number five and it
• 4:17 - is the total number of nodes in this
• 4:20 - graph you can see here that we have one
• 4:22 - two three four five nodes so it's going
• 4:24 - to be a five the next five lines and
• 4:28 - they're going to be n lines so whatever
• 4:30 - this first number is there's going to be
• 4:31 - that many lines following it so in this
• 4:33 - case there's five lines so one two three
• 4:36 - four five the next five lines of input
• 4:39 - will each have three numbers on them the
• 4:43 - first number is going to be the value of
• 4:45 - the node so in this case the one is the
• 4:48 - one for this node here the three is the
• 4:51 - three for this note here the second
• 4:53 - number is going to be the index of the
• 4:56 - left child now this is important because
• 4:58 - the index isn't going to be the value of
• 5:01 - the left child it's going to be the
• 5:02 - index in this quarter of nine items were
• 5:05 - getting so if I click here you can see
• 5:08 - that assume that the index starts at
• 5:11 - zero and then the index goes to 1 then
• 5:13 - the index goes to two then three then
• 5:15 - four now these index won't be provided
• 5:17 - to us they're not in the input file but
• 5:20 - you can assume that when you're starting
• 5:22 - to read through you could write a for
• 5:23 - loop so it says
• 5:23 - for I equals 0 to 4 so you can say that
• 5:28 - the first line is going to be index 0
• 5:29 - the second line is going to be index 1
• 5:31 - so in this case if we click through this
• 5:35 - you can see that this first line of
• 5:37 - index 0 is going to map to this node
• 5:39 - here so it's got the value 1 and that's
• 5:43 - that's what these indexes for i do want
• 5:46 - to stop and just take a second i should
• 5:48 - have covered this earlier the first
• 5:50 - number is the value the second number is
• 5:53 - the index of the left child and the
• 5:55 - third number is the index of the right
• 5:56 - child so when they're negative 1 assume
• 6:00 - that there are no children because
• 6:02 - negative 1 is not a valid index so just
• 6:04 - assume that there are no children in
• 6:06 - that case that's what this bottom rule
• 6:07 - says so in this case we're saying the
• 6:11 - first index is 0 so our first node is it
• 6:14 - index 0 has a value of one negative one
• 6:17 - on the left side so no chilled on the
• 6:18 - left negative one of the rights and your
• 6:20 - children on the right so then we go to
• 6:22 - the second line of our input or the
• 6:24 - second line of the end lines and it has
• 6:27 - a value of 3 and since it has negative 1
• 6:29 - negative 1 it has no children so that's
• 6:31 - this note here
• 6:33 - then we move on to the thus end x equals
• 6:37 - 2 and in this case the value is to that
• 6:40 - just happens to be the same as the index
• 6:42 - but that was just luck and then the
• 6:44 - index of its left side is 0 so you can
• 6:46 - see up here the index of the first one
• 6:48 - that has the value of one that's where
• 6:50 - it's getting this one from the left
• 6:51 - child so it's saying point to this first
• 6:53 - line of input that's the one that you're
• 6:55 - going to use for the left child and then
• 6:57 - it's right child is going to be the
• 6:59 - index one so in this case this three
• 7:02 - value is its right child so then we get
• 7:04 - to the index equals three and in this
• 7:07 - case the value is six and it has no
• 7:09 - children
• 7:10 - lastly we get to index equals 4 and this
• 7:13 - happens to have the value of 4 and its
• 7:16 - left child is the is the item we read
• 7:18 - with index 2 and it's right child as the
• 7:21 - item i read with index 3
• 7:23 - so index 2 is this number two here are
• 7:26 - the one with the value too so that's how
• 7:28 - you see this left child and index 3 is
• 7:30 - this one here that has the six value so
• 7:34 - that's how it got the six over here
• 7:37 - so here's where I'm trying to show you
• 7:40 - visually that this two points to this
• 7:42 - line up here that has indexed to and it
• 7:45 - happens to be the one with the value to
• 7:46 - and then the left and right children of
• 7:48 - index is 0 and 1
• 7:51 - so I'm going to say let's go ahead and
• 7:53 - try coding this we will take the sample
• 7:55 - input file and will also write a
• 7:57 - function real quickly to print out what
• 7:59 - we're doing so that you can actually see
• 8:01 - you know what's going along the way
• 8:04 - so let's try coding this to see what we
• 8:07 - get we're going to take the same sample
• 8:09 - input file and we're going to you know
• 8:11 - use that to try to create our graph but
• 8:13 - before we do that I'm also going to want
• 8:15 - to create a print function that will
• 8:16 - allow us to print out each of the items
• 8:18 - in our graph so that we can sort of
• 8:19 - visualize it to see you know is this
• 8:21 - reading correctly or is this working
• 8:23 - correctly because it's always nice to
• 8:25 - have a way of sort of testing out what
• 8:26 - you're doing and it's hard to print out
• 8:28 - a graph like this it's much easier just
• 8:29 - to print out the numbers and to say what
• 8:31 - it's left and right children are
• 8:33 - so I am going to copy all of these
• 8:37 - numbers and I'm going to open up I'm
• 8:41 - going to first do DFS n so this is just
• 8:44 - our input file so this is what we've
• 8:47 - been working on I just put the five on
• 8:49 - the first line and then all these other
• 8:50 - numbers going down so this is just going
• 8:52 - to be our input file but right now we
• 8:54 - don't need that first we're going to
• 8:58 - to create this type so we already did
• 9:00 - the type in the and the slides so I'm
• 9:03 - just going to sort of go through this
• 9:04 - pretty quickly we've got the value which
• 9:07 - is an integer we've got our left child
• 9:08 - which is a node we've got a right child
• 9:10 - which is a node so we can save this the
• 9:15 - next thing we want to do is we want to
• 9:17 - just go ahead and write a print function
• 9:18 - that prints out a node and makes it real
• 9:21 - easy for us to do that so we're going to
• 9:23 - say funk print node it's going to take
• 9:26 - in a node and it's just going to print
• 9:29 - out something to our terminal that sort
• 9:31 - of helps us see what the note is so the
• 9:33 - first thing we're going to do is format
• 9:34 - duck print end up value we always want
• 9:37 - to see the value of our nodes we're
• 9:39 - going to put value and then end up value
• 9:42 - we also want to put a new line after
• 9:45 - every single print so I'm going to put
• 9:46 - this at the very end because we're going
• 9:48 - to have a couple of the lines of print
• 9:49 - on the same line
• 9:51 - so from here I'm just going to go ahead
• 9:53 - and test this which means that we need
• 9:55 - to do funk main and we need to create a
• 9:58 - node to test with so I'm going to say
• 10:00 - test is a new node and we're going to
• 10:06 - say value is 1 2 3 then we're going to
• 10:11 - write node test so let's go run mango
• 10:17 - and you can see it's printing out the
• 10:19 - value of one two three all right so we
• 10:22 - have that printing out but we need to do
• 10:24 - next is we need to at least make sure
• 10:28 - that we're printing out the left and the
• 10:29 - right child of every node so that we can
• 10:31 - at least see what those are we don't
• 10:33 - want to print out the actual you know
• 10:36 - the pointers here we want to print out
• 10:38 - the actual values of those nodes so I'm
• 10:40 - just going to show you now if you do
• 10:41 - format print a print line test this is
• 10:47 - going to print out nil nil here which
• 10:49 - nil is somewhat useful but if we assign
• 10:51 - those to anything for example we have
• 10:53 - right value is 333 and then if we say
• 11:00 - test up write equals right now if we
• 11:03 - print this out you'll notice that all we
• 11:05 - get here is a pointer it's not really
• 11:07 - that useful it's kind of hard to know
• 11:08 - it's kind of hard to see what's going on
• 11:10 - there so rather than you know just
• 11:12 - letting that be we are going to write a
• 11:16 - finish up our print node function so
• 11:19 - that it will actually print out what the
• 11:21 - right knee left values are as we go look
• 11:23 - at the nodes to do that we're first
• 11:26 - gonna have to check does this node have
• 11:28 - a left child so if n dot left is not
• 11:31 - equal to nil if it's not know that means
• 11:34 - we can print something out so we're
• 11:35 - going to do a format duck print we're
• 11:38 - going to say left a number to n dot left
• 11:41 - value so we're just going to print out
• 11:43 - the value of the left child we don't
• 11:45 - want to print out anything else that's a
• 11:46 - little bit overkill so we'll just print
• 11:48 - out the value so we can copy all of that
• 11:51 - and we can change it all to do the same
• 11:53 - thing for the right if I could type
• 11:56 - today that is
• 11:58 - so we have if n dot right is not equal
• 12:00 - to no then we're going to print out the
• 12:03 - right-hand sides value and then after
• 12:05 - all that will print the new line so now
• 12:07 - we can go ahead and rerun our program
• 12:09 - and you'll see here that the thumb print
• 12:12 - line line is just printing out the value
• 12:14 - nil and a pointer but the function that
• 12:16 - we wrote the print node function will
• 12:18 - print out the value is 1 2 3 and the
• 12:20 - right child's value is 3 3 3
• 12:23 - alright so we have a nice print node
• 12:25 - function that'll really help us out
• 12:27 - along the way so the next thing we want
• 12:29 - to do now that we have that is we just
• 12:31 - want to go ahead and try to read in this
• 12:33 - input file so assuming you've used some
• 12:38 - of the you know some of the ways to read
• 12:39 - an input file I'm just going to kind of
• 12:41 - go through this if you haven't ever read
• 12:43 - in an input file before or anything like
• 12:45 - that just go ahead and leave a leave a
• 12:48 - comment email me something like that and
• 12:50 - i will try to get you some resources for
• 12:52 - that i'm sure there are some online i
• 12:53 - just need to find them and link them
• 12:56 - so we're going to go ahead and say far
• 13:00 - and int so this is going to be the end
• 13:02 - integer that we're reading in first I'm
• 13:04 - going to name it n because that's what
• 13:05 - it was called in our you know our slide
• 13:07 - and I think it's a little bit easier
• 13:09 - just to keep that consistent so we're
• 13:11 - going to say funks can f go % d which is
• 13:16 - an integer and we're going to say and n
• 13:23 - so we're going to go ahead and print out
• 13:25 - and after we scan it in and I'm just
• 13:27 - doing that to make sure this is correct
• 13:28 - so now that we've done all that we need
• 13:31 - to run our program and we need to make
• 13:34 - sure this works so you see that whenever
• 13:36 - I run the program it stops and it just
• 13:38 - sits here so it's doing that because
• 13:40 - it's waiting for me to type in a number
• 13:42 - like 1 so if i type in one it reads it
• 13:45 - in and then it prints it out so that's
• 13:46 - the end number it's running so i showed
• 13:48 - you earlier that i created this file
• 13:49 - called DFS n I'm go ahead and show you
• 13:54 - it's right here here I can hell s and
• 13:55 - show it in this directory so whenever I
• 13:58 - have this file I want to send that in as
• 14:00 - these you know I don't want to type in
• 14:01 - my input every time so what I'm going to
• 14:03 - do is I'm going type go run mango and
• 14:05 - then with the less than sign DFS in this
• 14:09 - will pipe the DFS in file as my input
• 14:12 - for this program when I run it so you
• 14:15 - can see here that now when I run it I
• 14:17 - don't see the input because it's getting
• 14:18 - piped in but it reads in the number five
• 14:20 - as N and it prints it out so I can even
• 14:22 - show you that and be like n is and you
• 14:28 - can see that it says n is 5 so it's
• 14:30 - reading this very first line as 5 and
• 14:32 - that's what we expected
• 14:34 - alright so we have n now we need to sort
• 14:37 - of go through all of the numbers of n so
• 14:40 - we're gonna have n rows and we need to
• 14:42 - try to create the nodes for every line
• 14:43 - so the first thing we want to do is do
• 14:46 - VAR nodes
• 14:49 - a note so this is going to be a slice of
• 14:51 - nodes and we're going to make it ahead
• 14:54 - of time so we know that there's going to
• 14:56 - be n of these so we're just going to go
• 14:57 - ahead and create this slice of nodes and
• 14:59 - the length is going to be n
• 15:03 - so once we know that we can go ahead and
• 15:06 - say for I is 0 I is less than n I plus
• 15:10 - plus so this will give us the index is 0
• 15:12 - through 4 so if i go back here you can
• 15:15 - see how i have the index is 0 through 4
• 15:17 - that's how i'm getting the indexes as
• 15:19 - i'm writing this for loop and I is going
• 15:21 - to be that index so then on every single
• 15:24 - line I'm going to say I need three
• 15:30 - variables here i need the I need the
• 15:34 - value of the node I need the index of
• 15:36 - the left child in the index of the right
• 15:37 - child so we're going to be VAR Val index
• 15:41 - left index right and these are all
• 15:46 - integers so I'm going to funct scanf % d
• 15:52 - % d % d so % d is just the way of
• 15:55 - reading at an integer so that's what
• 15:56 - we're doing here at the scanf and that's
• 15:57 - the part that I said that if you don't
• 16:00 - find a resource for it depending on what
• 16:03 - language if you're not following along
• 16:05 - with go if you're doing another language
• 16:06 - like Java you might be using something
• 16:08 - like a scanner and calling next int
• 16:11 - something along those lines but if
• 16:13 - you're using C or one of those languages
• 16:15 - you're probably pretty familiar with
• 16:16 - scanf so we're going to go ahead and
• 16:20 - read this in with the value is the first
• 16:22 - one so that's the value of the node the
• 16:24 - index left is the next one and the index
• 16:27 - right is the third value on that line so
• 16:30 - we're going to go ahead and print these
• 16:32 - out so we're going to do value well
• 16:38 - we're going to do index left index left
• 16:44 - i'm going to do index right and then
• 16:49 - index right so i just want to go ahead
• 16:51 - and print these all out and make sure
• 16:52 - that they're good you can see here that
• 16:54 - is complaining because i'm not using
• 16:56 - these nodes so i'm going to comment them
• 16:57 - out for the time being I just want to go
• 17:00 - correctly
• 17:03 - so we got value 1 index left negative 1
• 17:07 - and X right negative one so I'm going to
• 17:08 - go to our input file that looks correct
• 17:11 - so if I continue looking at these you
• 17:14 - can see that all of these appear to be
• 17:16 - correct they're matching up with this
• 17:17 - output or input file that I got it out
• 17:20 - up here so it looks like we're reading
• 17:22 - and values correctly the next thing we
• 17:24 - need to do is actually set these values
• 17:25 - on our nodes so we need to do first off
• 17:31 - I need to uncomment the nodes and then I
• 17:33 - needed to do nodes and then we're at the
• 17:35 - current index of I so we're in deriving
• 17:37 - iterating through these with the indexes
• 17:38 - and we're going to say dot value equals
• 17:43 - Val so we don't need to instantiate this
• 17:46 - node because when we go went ahead and
• 17:49 - with empty nodes so we have empty note
• 17:51 - objects we just need to set all the
• 17:52 - different pieces on the node so we save
• 17:54 - nodes I value equals Val nodes i dot
• 18:00 - left so this is where we could run into
• 18:03 - a problem what we want to do is we want
• 18:06 - to see nodes i dot left equals nodes
• 18:07 - index left but the problem is if index
• 18:11 - left is negative 1 this is going to
• 18:12 - raise an error so I'm just going to go
• 18:17 - to this we don't want just want the node
• 18:18 - but if i run this it's going to rut
• 18:21 - result in an air something is going to
• 18:23 - panik's i'm just going to show you that
• 18:24 - really quickly and i'm going to move
• 18:26 - this print line to the top so you can
• 18:28 - see which line it does this one but i
• 18:31 - just want to show it to you in case you
• 18:32 - happen to run into this
• 18:34 - so we have this panic runtime error
• 18:39 - index out of range so what happened is
• 18:42 - we use this index negative one and
• 18:43 - that's not a valid index so what we need
• 18:46 - to do is we need to say if index left is
• 18:50 - greater than or equal to 0 which is all
• 18:53 - the valid indexes we could possibly have
• 18:55 - then and only then are we going to go
• 18:59 - ahead and make this assignment and then
• 19:02 - we're also going to do the same thing if
• 19:03 - index rights greater than equal to 0
• 19:05 - nodes I'd write equals and nodes context
• 19:11 - right so what we're doing here in this
• 19:14 - this part here this part I'm go ahead
• 19:17 - and put a space between them this first
• 19:19 - part or the second part technically is
• 19:22 - saying in our list of nodes so are in
• 19:25 - our slice of nodes we want to get the
• 19:27 - one at index left so when we go back
• 19:30 - here the DFS remember how these numbers
• 19:32 - reference indexes and not the actual
• 19:33 - value that was you stored in that node
• 19:35 - so we want to get the one that's in the
• 19:37 - index they're represented by it so since
• 19:40 - all of our nodes are already
• 19:41 - instantiated up here we can just go
• 19:43 - ahead and reference it even if we
• 19:44 - haven't set the value for it or anything
• 19:46 - we can reference it because it's already
• 19:48 - been created the + sign is just saying
• 19:50 - that we want to reference the pointer to
• 19:52 - it you know instead of just referencing
• 19:53 - a copy of it or something like that so
• 19:55 - we want to point to the pointer of it
• 19:58 - so once we do all of that we should have
• 20:01 - our nodes list pretty good i'm going to
• 20:03 - go ahead and print line nodes and this
• 20:06 - probably isn't going to print out
• 20:07 - anything useful well you can the one
• 20:10 - thing you can look at real quickly here
• 20:11 - is you can see which ones have nil and
• 20:13 - nil for its left and right child so it
• 20:17 - looks like the one with the value of two
• 20:19 - has two children and the one with the
• 20:20 - value of four has two children so let's
• 20:22 - go ahead and look here and see if that's
• 20:23 - right for has two children two has two
• 20:26 - children nobody else has any children so
• 20:28 - that looks like our graph is you at
• 20:30 - least close to what it's supposed to be
• 20:31 - the last thing we're going to do is
• 20:34 - going to do for each node in the range
• 20:38 - of nodes we're going to do print node
• 20:42 - with the node
• 20:45 - and we need to get the address to it
• 20:48 - technically we don't need that but we
• 20:53 - or a pointer but that's fine so now that
• 20:57 - we have that let's go ahead and run this
• 20:58 - and you can see that i'm going to go
• 21:01 - ahead and get rid of this line up here I
• 21:03 - just don't want that output to be there
• 21:06 - so let's run it again you can see that n
• 21:09 - is 5 our first node has a value of one
• 21:11 - and no children so let's go over here
• 21:13 - value of one no children that looks
• 21:15 - correct I'm going to go back to the
• 21:18 - terminal our second node has a value of
• 21:21 - three and no children so you can see
• 21:23 - here value three no children that looks
• 21:26 - correct
• 21:28 - our third node has a value of two and
• 21:30 - its left child is index one or sorry has
• 21:34 - a value of one and it's right child has
• 21:36 - a value of three so here we're printing
• 21:37 - out the actual values not the indexes so
• 21:40 - let's look at to hear its left child has
• 21:43 - a value of one that is correct and it's
• 21:44 - right child as a value of three so it
• 21:46 - looks like that is what we expected
• 21:48 - there so now we have the six right here
• 21:52 - it has no children and we have the same
• 21:54 - thing in our print out has a value of
• 21:56 - six and no children and lastly we have
• 21:59 - the node with the value of for the left
• 22:02 - child has a value of two and the right
• 22:03 - child has a value of six so we go to for
• 22:06 - the left child has a value of two that
• 22:08 - is correct the right child has a value
• 22:10 - of six so that is also correct
• 22:13 - so the upside to this is that now that
• 22:16 - we have a way to read in these graphs we
• 22:18 - should be able to create trees and you
• 22:21 - do our DFS's and RBF esses and actually
• 22:23 - test them using these graphs that we've
• 22:25 - created so I know that there's a whole
• 22:27 - lot here what I'm going to suggest is
• 22:30 - that if even if you don't fully
• 22:33 - understand what's going on here just go
• 22:37 - the code copied and then we're going to
• 22:38 - pull all this code out into its own
• 22:40 - function so i'm going to call this funk
• 22:43 - read and it's going to return a slice of
• 22:47 - nodes so the reason I'm doing this is
• 22:52 - once we get the notes back we'll shoot
• 22:54 - we should be able to write our BFS and
• 22:56 - our DFS and all of our other algorithms
• 22:58 - just using them we shouldn't need
• 22:59 - everything else from here so I'm going
• 23:01 - to go ahead and just say breed is our
• 23:03 - way of sort of reading the input and
• 23:05 - returning our slice of nodes so that
• 23:07 - later on when we're writing our code we
• 23:09 - can just reference this without having
• 23:10 - to rewrite it every time so that means
• 23:13 - that at the end of this we need to
• 23:14 - return the nodes and then to test that
• 23:17 - this is still working we need to do a
• 23:19 - funk main and we'll do nodes is equal to
• 23:24 - whatever we get back from read
• 23:27 - and then lastly we're going to ahead and
• 23:29 - pull this print loop out of here because
• 23:31 - we don't need to print it out every time
• 23:32 - we read it we also don't need to print
• 23:34 - out and every time we read it so we get
• 23:36 - rid of those two print statements you
• 23:38 - can see here that read is just this
• 23:40 - function now where we read an end we
• 23:42 - read in each little line for the nodes
• 23:44 - and then we return them and then up here
• 23:46 - we'll go ahead and paste this for loop
• 23:49 - where we are printing out all the nodes
• 23:51 - and we're going to go ahead and just
• 23:53 - test it real quick
• 23:56 - alright so our how puts exactly the same
• 23:58 - and that's exactly what we wanted we
• 24:01 - just wanted to move some code around and
• 24:03 - just put it inside of its own function
• 24:04 - so that moving forward we can do all of
• 24:07 - this and then on this time we could do
• 24:08 - DFS nodes and we can maybe say find the
• 24:11 - number one but we aren't doing that just
• 24:13 - yet that will be the next video
• 24:16 - alright hopefully that helps if you need
• 24:20 - a copy of all the source let me know I'm
• 24:21 - going to try to get a copy of it on the
• 24:23 - go playground and I'll try to link it
• 24:25 - from this video if you're enjoying this
• 24:29 - series I'd really appreciate it if you
• 24:31 - could head over to my blog at Calhoun do
• 24:33 - and head down to the mailing list and
• 24:36 - sign up so i won't spam you or do
• 24:40 - the mailing list i will send you a free
• 24:42 - sample from my book web development with
• 24:44 - go and then i'll send you an email about
• 24:46 - once a week just letting you know about
• 24:48 - new articles or new videos or really any
• 24:50 - new free content that i'm sending our
• 24:52 - that i'm releasing publishing and i'll
• 24:54 - also let you know about you know what
• 24:56 - i'm working on in the coming weeks and
• 24:57 - try to get some feedback on what
• 24:59 - everybody thinks i should focus on and
• 25:01 - what they'd like to hear me talk about
• 25:02 - and every once in a while when I release
• 25:04 - a new product a new course or a new book
• 25:06 - or something like that I will send an
• 25:08 - email to the mailing list often times
• 25:10 - it'll be accompanied with a free sample
• 25:12 - or a discount code or something like
• 25:13 - that basically just letting you know
• 25:15 - about it and if you like the stuff that
• 25:17 - I'm writing chances are you'll you know
• 25:18 - at least appreciate some of that and I
• 25:21 - definitely really appreciate any of the
• 25:22 - purchases because they help support me
• 25:24 - and help me a lot to keep creating free
• 25:25 - material like this thanks so much
Written by
Jon Calhoun

Jon Calhoun is a full stack web developer who teaches about Go, web development, algorithms, and anything programming. If you haven't already, you should totally check out his Go courses.

Previously, Jon worked at several statups including co-founding EasyPost, a shipping API used by several fortune 500 companies. Prior to that Jon worked at Google, competed at world finals in programming competitions, and has been programming since he was a child.

More in this series

This post is part of the series, Let's Learn Algorithms.