|
1 | 1 |
|
2 |
| -Hello and welcome back, in today's video we're going to start tackling trees which are a very common type of graph. In particular, we're going to look at how we computationally represent and store trees as well as a few simple algorithms to get started. |
| 2 | +Hello and welcome back, my name is William, and in today's video we're going to start tackling trees. In particular, we're going to look at what trees are and how we computationally store and represent trees. |
3 | 3 |
|
4 | 4 | Conceptually it's fair to say most people know what I mean when I say I'm working with a tree, or that something is structured as a tree. Below are four graphs, but one of them is not a tree, do you know which one?
|
5 | 5 |
|
6 |
| -Only the last graph isn't a tree, but why is it not a tree? |
| 6 | +Only the last graph is not a tree, but why is it not a tree? |
7 | 7 |
|
8 |
| -It's because we define a tree as being an undirected graph with no cycles, and that's the key thing to remember. You can see that the rightmost graph has a cycle and is therefore not a tree. However, there's an even easier way to check whether a graph is a tree or not. |
| 8 | +It is because we define a tree as being an undirected graph with no cycles, and that's the key thing to remember. You can see that the rightmost graph has a cycle, and is therefore not a tree. However, there's an even easier way to check whether a graph is a tree or not. |
9 | 9 |
|
10 |
| -Each tree has exactly n nodes and n-1 edges. If we count up all the nodes and edges of each graph you can see that all the trees have one less edge than the number of nodes except for the rightmost graph which is not a tree. |
| 10 | +Each tree has exactly n nodes and n-1 edges. If we count up all the nodes and edges of each graph you can see that all the trees have one less edge than the number of nodes, except for the rightmost graph which is not a tree. |
11 | 11 |
|
| 12 | +We now know what trees are, but where do they appear in computer science and in the real world? Here are a few examples where you might encounter a tree structure: |
| 13 | + |
| 14 | + First is your computer's file system which consists of directories, subdirectories and files which is inherently a tree. |
| 15 | + |
| 16 | + Another place you see trees are in social hierarchies where you often see CEO's, kings, priests and generals at the top; and interns, servants, children and the lower class at the bottom. |
| 17 | + |
| 18 | + Trees are used to decompose source code and mathematical expressions into abstract syntax trees for easy evaluation. For example, the math expression you see on this slides can be broken down into a tree structure. |
| 19 | + |
| 20 | + Every webpage you visit can be throught of as a tree structure due to HTML's nested tag structure. This structure is used to tell your browser how to easily render the page. |
| 21 | + |
| 22 | + Another large application of trees is in game theory to model decisions and courses of action. On this slide is the famous prisoner's dilemma problem and it's four different outcomes. |
| 23 | + |
| 24 | + There are many many more applications of trees in computer science and in the real world, but we're not going to cover them all today. However, it's worth mentioning that in computer science the place you'll most often see trees are as part of data structures, many of which are listed below. |
| 25 | + |
| 26 | +Now we need to talk about how we actually store and represent these undirected trees. |
| 27 | + |
| 28 | +First, you will need to label the nodes of your tree by indexing it, preferably from 0 to n non-inclusive like the one on the left of this slide. |
12 | 29 |
|
13 |
| -Now we need to talk about how we actually store and represent these undirected trees. First, you will need to label the nodes of your tree by indexing it, preferably from 0 to n non-inclusive like the one on the left of this slide. |
14 | 30 | A simple way to store a tree is as an edge list, which is simply a list of undirected edges indicating which two nodes have an edge between them. The great thing about this representation is that it's super fast to iterate over and cheap to store.
|
15 | 31 |
|
16 |
| -The downside however is that storing your tree as a list lacks the structure to efficiently query all the neighbors of a node. |
| 32 | +The downside however, is that storing your tree as a list lacks the structure to efficiently query all the neighbors of a node. |
17 | 33 |
|
18 |
| -An alternative and more popular representation is to store an undirected tree as an adjacency list which maps nodes to all its neighbors. |
| 34 | +This is why the adjacency list is usually a more popular representation to store an undirected tree. In this representation, you store a mapping between a node and all its neighbors. |
19 | 35 |
|
20 |
| -For example, node 4 has the neighbors 1, 5 and 8. In reality we're actually representing these undirected edges as pairs of directed edges. |
| 36 | +For example, node 4 has the neighbors 1, 5 and 8 so in the adjacency list, node 4 maps to the list containing 1, 5 and 8 respectively. |
21 | 37 |
|
22 |
| -You could also so a tree as an adjacency matrix where having a 1 in a particular cell means that the nodes which correspond to the row and column value have an edge between them. |
| 38 | +You could also store a tree as an adjacency matrix of size n by n where having a 1 in a particular cell means that the nodes which corresponding to the row/column values have an edge between them. |
23 | 39 |
|
24 |
| -However, in practice I would say to always avoid storing trees an an adjacency matrix because it's a huge waste of space to use n squared memory and only use roughly 2n of the matrix cells. |
| 40 | +However, in practice I would say to always avoid storing a tree as an adjacency matrix because it's a huge waste of space. You would not ever want to allocate n squared memory and only use roughly 2n of the matrix cells, it just doesn't make sense. |
25 | 41 |
|
26 | 42 |
|
27 |
| -I can't keep talking about trees without mentioning rooted trees which are trees with a designated root node. On most rooted trees the edges point away from the root node although it's also possible to have edges that point towards the root node, but that's much rarer from my experience. |
28 |
| -Generally speaking, rooted trees are far easier to work with than undirected tree because they have a well defined structure and allow for recursive algorithm implementations. |
| 43 | +Alright, I can't keep talking about trees without mentioning rooted trees which are trees with a designated root node. I have highlighted the root node in orange, and most rooted trees as you'll notice have directed edges which point away from the root node, however it's also possible to have edges which point towards the root node, but those trees are much rarer in my experience. |
| 44 | +Generally speaking, rooted trees are far easier to work with than undirected trees because they have a well defined structure and allow for recursive algorithm implementations. |
29 | 45 |
|
30 | 46 | Related to rooted trees are binary trees which are trees for which every node has at most two child nodes. The first two trees on this slide are binary trees, but the last one is not because it has a node with more than 2 child nodes.
|
31 | 47 | You don't often see binary tree manifest themselves in the real world, for the most part binary trees are artificially created and integrated as part of data structures to guarantee efficient access and queries on data.
|
@@ -190,7 +206,7 @@ For example, if we're at position 2 in the array, we know that the left and righ
|
190 | 206 |
|
191 | 207 |
|
192 | 208 |
|
193 |
| - But why does this work? Suppose we want to find the height at node a, the root node, then we can do so by |
| 209 | +But why does this work? Suppose we want to find the height at node a, the root node, then we can do so by |
194 | 210 |
|
195 | 211 |
|
196 | 212 |
|
|
0 commit comments