Depth first and breadth first search on trees in Javascript

Connor Leech
Employbl
Published in
2 min readFeb 28, 2018

--

Binary trees are a common data structure for accessing data quickly. Breadth first and depth first traversal are two important methodologies to understand when working with trees. Before we get to that though, let’s review the binary tree data structure.

Please note that a binary search tree and binary trees are not the same. Binary search trees are a type of data structure where the value on the left node is less than the parent value and the right value is greater than the parent value. Binary tree is where each node has two connections, irrespective of value.

Example of a binary search tree (source)

With data structures, you can perform four primary types of actions: Accessing, Searching, Inserting, and Deleting.

I recommend watching this video from HackerRank with Gayle Laakmann McDowell, author of Cracking the Coding Interview. She covers data structures, DFS and BFS at a high level and the implementation details of each algorithm.

To be clear, graphs and trees are the data structures. DFS and BFS are the algorithms. We use data structures…

--

--

Connor Leech
Employbl

Girl Dad x 2. Cofounder @Employbl. Software Engineer @CommentSold.