Skip to content

Commit 14ff373

Browse files
committed
tree slides
1 parent 1ce2f74 commit 14ff373

File tree

2 files changed

+36
-36
lines changed

2 files changed

+36
-36
lines changed

slides/graphtheory/trees.txt

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,64 @@
1-
Trees are a special form of graph
21

3-
They come in directed and undirected flavors
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.
43

5-
Definitions:
6-
An acyclic connected graph
7-
A connected graph with N-1 edges
8-
A graph in which any two vertices are connected by exactly one path
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?
95

6+
Only the last graph isn't a tree, but why is it not a tree?
107

11-
Rooted trees
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.
129

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.
1311

14-
child, parent
1512

16-
root node
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+
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.
1715

18-
Only the root node in a rooted tree doesn't have a parent, although sometimes it
19-
could be useful for the root node to have itself as a parent (e.g filesystem).
16+
The downside however is that storing your tree as a list lacks the structure to efficiently query all the neighbors of a node.
2017

21-
leaf node - nodes with no children
18+
An alternative and more popular representation is to store an undirected tree as an adjacency list which maps nodes to all its neighbors.
2219

23-
subtrees
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.
2421

25-
Binary tree is a tree for which every node has at most two child nodes
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.
2623

27-
(steal examples from binary tree slides)
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.
2825

2926

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.
3029

31-
>>> Solve simple tree problem
30+
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+
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.
3232

33+
Now related to binary trees are binary search trees which are trees which satisfy the BST invariant. The BST invariant states that for every node x the values in the left subtree are less than or equal to x and that the values in the right subtree are greater than or equal to x. This nice little property enables you to quickly search through a tree and retrieve a certain value if it exists which is particularly handy. All the trees on this slide are BSTs except for the last one because 1 is not greater than or equal to 3.
3334

34-
function Height(node):
35+
It's often common to require uniqueness on the values of your tree so that you don't end up with duplicate values. To resolve this issue change the invariant to be strictly less than rather than less than or equal to.
3536

37+
Now let's talk about how to store these rooted trees. Rooted trees are naturally defined recursively in a top down manner.
3638

37-
public boolean isValidBST(TreeNode root) {
38-
return validate(root, Long.MIN_VALUE, Long.MAX_VALUE);
39-
}
39+
In practice, you always maintain a pointer reference to the root node so that you can access the tree and its contents.
4040

41-
public boolean validate(TreeNode node, long min, long max) {
42-
// Reached bottom of tree
43-
if (node == null) return true;
44-
45-
// Verify invariant
46-
if (min >= node.val || max <= node.val) return false;
47-
48-
return validate(node.left, min, node.val) && validate(node.right, node.val, max);
49-
}
41+
Then each node also has access to a list of all its children, which are also called child nodes. In this slides, the orange node is the current node we have a reference to and the purple nodes are its children. All the bottom or leaf nodes don't have any children.
5042

43+
It's also sometimes useful to maintain a pointer to a node’s parent node in case you need to traverse up the tree. This effectively makes the edges in the tree bidirectional. Again, if the current node is the orange node than the purple node in the case in the parent node.
5144

52-
Root tree:
53-
select a node to be the root node
54-
recursively do a BFS and attach children while making sure not to
55-
point back to the parent
56-
starting from any node
45+
However, maintaining an explicit reference to the parent node isn’t usually necessary because you can access a node’s parent on a recursive function's callback.
5746

5847

48+
Another really neat way of storing a rooted tree if it is a binary tree is in a flattened array.
49+
50+
In the flattened array representation, each node has an assigned index position based on where it is in the tree. For example, the node with value 5 in orange is associated with the index 4 in the array. The tree is actually an array, the diagrams here is just a visual representation of what the tree would look like.
51+
52+
Similarly, this node with a value of 2 has an index of 6.
53+
54+
Even nodes which aren’t currently present have an index because they can be
55+
mapped back to a unique position in the "index tree" (gray tree).
56+
57+
In this format, the root node is always at index 0 in the array, so you always know where your starting point is. Another advantage of this format is that the child nodes of node i can be access relative to position i.
58+
59+
For example, if we're at position 2 in the array, we know that the left and right children of the node at index 2 is given by 2 times i plus 1 and 2 times i plus 2. Therefore the children of the node at index 2 can be found at positions 5 and 6. Reciprocally, this means if we have a node we know what the index of the parent node should be.
60+
5961

6062

6163

62-
Reminder: Avatar tonight @ 6:30
6364

64-
I'll be making some food if you want to spare yourself cooking :)

slides/graphtheory/trees_slides.key

47.1 KB
Binary file not shown.

0 commit comments

Comments
 (0)