Depth-first search (DFS)
Walking a tree or graph by going down one branch to the bottom before coming back and trying another. Preorder, inorder and postorder are forms of DFS.
Example
With root 1, left 2 (with 4 and 5) and right 3, the preorder is 1, 2, 4, 5, 3.
Test yourself: Depth-first search (DFS)
Walking a tree
In preorder, the root is visited ___ its children.
Pre means before. With root 5 and children 3 and 8, the preorder is 5, 3, 8.
Walking a tree
In postorder, the tree's root always comes ___.
First the 2 whole subtrees, then the root. In a 15-node tree, the root is the 15th.
Walking a tree
In a search tree with smaller values on the left, inorder returns the values ___.
With root 8, left 3 and right 10, the inorder is 3, 8, 10: left (smaller), root, right (larger).
0/3
â–¶ PlayLessons that use it
Walking a tree
By levels or by branches
Trees in an array
No references, just math
Binary search trees
Smaller on the left, larger on the right
AVL trees
A tree that straightens itself
Walking a graph
BFS and DFS, now with cycles
Choosing how to search
Brute force or using the order
Merge sort
Split in half and merge in order
Rebuilding a tree
Root, left and right
Tower of Hanoi
n − 1, 1 and n − 1
Backtracking
Try, and if it fails, go back
