How to traverse a binary tree
A binary tree can be traversed in four usual ways. The first three are recursive and differ only in when the node is visited with…
Many real problems are networks: maps, dependencies, friendships, hierarchies. In this lesson you will go from walking graphs to solving problems with them: tree traversals, common ancestor, shortest weighted paths (Dijkstra), topological order, components with union-find, minimum spanning trees, bipartite graphs and tries. You will learn to choose the right structure and to reason about its cost.
A binary tree can be traversed in four usual ways. The first three are recursive and differ only in when the node is visited with…
When edges have a cost (kilometers, minutes, euros), the path with the fewest edges is no longer necessarily the best. For…
A directed acyclic graph (DAG) represents dependencies: tasks that must be done before others, courses with prerequisites…
The union-find structure (or disjoint sets) answers two questions very fast: “are these two elements in the same group?” and…
A graph is bipartite if its nodes can be divided into two groups with no edges inside each group (for example, people and jobs)…
There are three classic ways to traverse a binary tree recursively: preorder (node, left, right), inorder (left, node, right) and…
In the level-order traversal the root is visited first, then its children, then the grandchildren, and so on. It is done with a…
The lowest common ancestor of two nodes is the deepest node that has both as descendants (a node counts as a descendant of…
In a graph with weights the path with fewer edges is not necessarily the cheapest. Dijkstra ’s algorithm uses a priority queue to…
If some tasks depend on others, a topological order places them so that each task appears after the ones it needs. It only exists…
The union-find structure keeps disjoint groups: each element points to a “parent” and the root identifies the group. Merging two…
A minimum spanning tree connects all the nodes at the lowest possible total cost and without cycles. Kruskal ’s algorithm sorts…
A graph is bipartite if its nodes can be painted with two colours so that no edge joins two nodes of the same colour (equivalent…
A trie (prefix tree) stores words letter by letter in nested dictionaries: words sharing a prefix share a path. It is the typical…
A binary tree is a BST if, for each node, all the values of its left subtree are smaller and all those of the right one are…