Elementary Data Structures with JavaScript - Binary trees - PART 3π
devlazar
Posted on February 17, 2021
Table Of Contents
* π€ INTRODUCTION
* π΄BREADTH-FIRST SEARCH
* πDEPTH-FIRST SEARCH
* π¨π»βπ»CODE
* π THANK YOU
π€ INTRODUCTION
Hello, my dear coders! I hope you are all having a great day π
Today, we are going to explore ways to traverse a tree with algorithms designed to optimize the search and traversal. Of course, for our implementation, we will use Vanilla JavaScript.
If you missed the previous part of this article you can check it out right here:
Article No Longer Available
Please feel free to connect with me via Twitter, Instagram or LinkedIn
π΄ BREADTH-FIRST SEARCH
Breadth-first search is characterized by the fact that it focuses on every item, from left to right, on every level before moving to the next.
π DEPTH-FIRST SEARCH
The strategy followed by depth-first search is, as its name implies, to search βdeeperβ in the three whenever possible. Depth-first searches are more concerned with completing a traversal down the whole side of the tree to the leaves than completing every level. There are three main ways to handle this, preOrder, postOrder, and inOrder but theyβre just very slight modifications of each other to change the output order.
1οΈβ£ PREORDER
What preorder does, it processes the root, traverses the left subtree, and after that, traverses the right subtree.
2οΈβ£ POSTORDER
First, we traverse the left subtree, after that, traverse the right subtree and, process the root.
3οΈβ£ INORDER
First, we traverse the left subtree of the root, then we process the root and after that, we traverse the right subtree of the root.
π¨π»βπ» CODE
Here is the code! π
π THANK YOU FOR READING!
References:
School notes...
School books...
Please leave a comment, tell me about you, about your work, comment your thoughts, connect with me!
β SUPPORT ME AND KEEP ME FOCUSED!
Have a nice time hacking! π
Posted on February 17, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.