Skip to content

Commit dcf94d6

Browse files
committed
tree slides
1 parent 78e330e commit dcf94d6

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

slides/graphtheory/trees.txt

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,13 +233,51 @@ You can root a tree using any of its nodes. However, be cautious because not eve
233233

234234
In some situations it’s also useful to keep have a reference to the parent node in order to walk up the tree. I illustrated parent node pointers as dotted lines on this slide.
235235

236-
Let's look at an example of how to
236+
Let's look at an example of how to root a tree. One of the best ways to do this is with a depth first search through the original tree and to create the rooted tree during the traversal.
237237

238+
The algorithm starts on the designed root node. The new rooted tree is being displayed on the right.
238239

240+
From the root node, begin the depth first search and add nodes to the rooted tree as the algorithm proceeds. I'll let the animation play and it should be clear what's going on.
239241

242+
<animate>
240243

244+
And that's rooting a tree in a nutshell
241245

246+
Let's have a look at some pseudo code for this.
242247

248+
On this slide I define a data object I will be using to describe a tree node.
249+
250+
Each node has a unique integer id
251+
252+
As well as a reference to its parent pointer. This member is generally optional, but I thought I should include it for completeness. Take note that every node will have a parent pointer except the root node whose parent pointer will be null.
253+
254+
Additionally, each tree node will also have a list of all the child tree nodes it has a reference to.
255+
256+
Here's the algorithm itself, it's relatively short and sweet. The input to the rootTree function takes a graph g as input which is the tree we want to root, the other input is the id of the designated root node. By default this is node 0, but it can be any other node. We assume that the root node exists in g and that the id is valid, otherwise i'm pretty sure this algorithm breaks.
257+
258+
The first line in the rootTree method creates the root tree node object with the id rootId, a parent reference of null and no child nodes.
259+
260+
Then I call the build tree method to start the depth first traversal to root the tree. As input parameters I pass in the graph g, the root node as the current node and the root's parent which is null.
261+
262+
The build tree takes exactly three parameters we just talked about: the graph, the current node, and the current node's parent node reference.
263+
264+
Next we enter a for loop that loops over all the neighbors of the current node, which in turn will become the children of the current node.
265+
266+
Because edges are undirected in the original tree, we absolutely need to avoid a situation where we add a directed edge pointing back to the parent.
267+
268+
So if the parent is null, meaning we're dealing with the root node.
269+
270+
Or the child id is equal to the parent id skip this node.
271+
272+
Otherwise we're dealing with a proper child node, so create a new node and add the child tree node to the list of the current node's children
273+
274+
Afterwards, dig deeper into the tree and do the same thing but for the newly created child node.
275+
276+
Once we have finished iterating over all the neighbors of this node return the current node.
277+
278+
And that's rooting a tree
279+
280+
goodnight.
243281

244282

245283

slides/graphtheory/trees_slides.key

-8.92 KB
Binary file not shown.

0 commit comments

Comments
 (0)