Skip to main content

5. Everything is Searchable

Chapter 5: Everything is Searchable

📄️ 5.2 Depth First Search

Depth First Search (DFS) is a search method where, upon encountering a new node, you immediately traverse that new node. Therefore, traversal requires a Last In, First Out (LIFO) stack, which can also be implemented using recursion, equivalent to using a stack. In a tree structure, since traversal always invokes the new node, it appears as if progressing in the "depth" direction. In Python, collections.deque can be used to implement the stack in C++. However, in most cases, using vector in C++ or list in Python is preferred as these structures not only support LIFO but also allow random access.