You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: slides/graphtheory/trees.txt
+14-14Lines changed: 14 additions & 14 deletions
Original file line number
Diff line number
Diff line change
@@ -84,34 +84,34 @@ Alright, that's all I have for this video, thank you for watching, please like a
84
84
85
85
86
86
87
-
Hello and welcome, my name is William, and in today's video we're going to look at some simple tree algorithms. This video is aimed at all you beginner's just starting computer science who are still learning how to write recursive code.
88
-
We're going to have a look at two pretty classic problems today, these are the types of problems you might encounter as warm up problems in an interview.
87
+
Hello and welcome, my name is William, and in today's video we're going to look at some simple tree algorithms. This video is aimed at all you beginner's just starting out who are still learning how to write code and especially recursive code. We're going to have a look at two problems today, and these are the types of problems which you might encounter as warm up questions in a job interview.
89
88
90
89
<problem1>
91
90
Alright, let's start with our first problem. For this problem we need to find the sum of all the leaf node values in a tree.
92
91
93
-
For instance, given this tree we want to sum up all the bottom nodes which have no children.
92
+
For instance, given this tree we want to sum up all the bottom nodes.
94
93
95
-
That would be all these nodes in red for a total of 9. If you're keen, you can
96
-
pause this video and give it a try.
94
+
That would be all these nodes in red for a total of 9. If you're keen, you
95
+
pause this video and give this problem a try.
97
96
98
97
<press>
99
98
100
-
Like all rooted tree problems we start with a reference to the root node. To solve this problem all we need to do is perform a tree traversal and identify leaf nodes while we do the traversal.
99
+
Like all rooted tree problems we start with a reference to the root node. To solve this problem all we need to do is perform a tree traversal and sum up the value of the leaf nodes while we do the traversal.
101
100
Two popular traversal algorithms are doing a Breadth First Search and Depth First Search. On trees, the preferred traversal method is typically a DFS because it lends itself to be easily implemented recursively.
102
101
It's pretty simple, watch how the animation does it. You start at the root and plunge down the tree depth first.
103
102
104
103
<do animation>
105
104
106
-
At the end when we sum up all the values we can see that the sum of the leaf nodes should be 9. Now let's have a look at some pseudo-code
105
+
<after the slide with "= 9" appears>
106
+
At the end when we sum up all the values, and we can see that the sum of the leaf nodes is 9. Now let's have a look at some pseudo-code for how this is implemented.
107
107
108
-
The algorithm is pretty simple, but it may look strange at first if you haven't seen much recursive code.
108
+
The algorithm is quite short, but it may look strange at first if you haven't seen much recursive code.
109
109
110
110
We call the leafSum function by passing in a reference to the root node as a starting point.
111
111
112
-
We begin by handling the special case where the tree is empty and return zero in such a situation.
112
+
The first thing we do is handle the special case where the tree is empty and return zero in such a situation.
113
113
114
-
The next thing we do is check if the current node is a leaf node, and if it is we return the value stored in the leaf node.
114
+
next we check if the current node is a leaf node, and if it is we return the value stored in the leaf node.
115
115
116
116
The isLeaf method checks if a node is a leaf node by counting all its children. If the number of child nodes is zero then we know the node is a leaf node.
117
117
@@ -130,9 +130,9 @@ We're going to have a look at two pretty classic problems today, these are the t
130
130
131
131
For example, on this slide "h of a" has a value of 3, but "h of b" has a value of 2 and "h of e" has a value of zero.
132
132
133
-
By themselves, leaf nodes such as node e don't have children, so they don’t add any additional height to the tree. So we can conclude that as a base case the height of any leaf node should be zero.
133
+
By themselves, leaf nodes such as node e don't have children, so they don’t add any additional height to the tree. So we can conclude that as a base case, the height of any leaf node should be zero.
134
134
135
-
Now, assuming node x is not a leaf node, we're able to formulate a recurrence relation for the height, which is that the height of the subtree rooted at node x is the maximum of the height of x's left and right children plus one.
135
+
Now, assuming node x is not a leaf node, we're able to formulate a recurrence relation for the height, which is that the height of the subtree rooted at node x is the maximum of the height of x's left and right subtrees plus one.
136
136
137
137
Let's have a closer look at how this works with an example. Suppose we have this tree and we want to compute its height.
138
138
@@ -185,7 +185,7 @@ We're going to have a look at two pretty classic problems today, these are the t
185
185
I want to take a moment and go back to the previous statement and remark on a simplification we can exploit.
186
186
What do you reckon happens if we remove checking whether a node is a leaf node or not, do you think the algorithm would still behave correctly? Pause the video and think this over.
187
187
188
-
Oddly enough, removing the leaf node check still makes the algorithm work correctly. This works because the base case has adopted a new meaning, instead of the base case checking for leaf nodes, it's now checking whether a node is null and returning -1. Returning -1 now not only checks for the empty tree case, but it also helps in correcting for the right height.
188
+
Oddly enough, removing the leaf node check still makes the algorithm work correctly. This works because the base case has adopted a new meaning, instead of the base case checking for leaf nodes, it's now checking whether a node is null and returning -1. Returning -1 now not only checks for the empty tree case, but it also helps in correcting for the right tree height.
189
189
190
190
Let's see what I mean by correcting for the right height. If our new base case is now checking for null nodes instead of the leaf nodes, then our tree one unit taller, so we need to correct for this.
191
191
@@ -201,7 +201,7 @@ We're going to have a look at two pretty classic problems today, these are the t
201
201
202
202
Once we reach a null node, return -1
203
203
204
-
On the recursive callback remember that we always add +1
204
+
On the recursive callback remember that we always add +1 with our recurrence.
0 commit comments