Skip to content

Instantly share code, notes, and snippets.

@Prottoy2938
Last active March 12, 2020 06:46
Show Gist options
  • Save Prottoy2938/5d8459c292641ef7d886b4b8baa42771 to your computer and use it in GitHub Desktop.
Save Prottoy2938/5d8459c292641ef7d886b4b8baa42771 to your computer and use it in GitHub Desktop.
Couple of Tree Traversal Methods in JavaScript - Includes `breadth-first-search`, `depth-first-search` (depth-first-search-PreOrder, depth-first-search-postOrder, depth-first-search-InOrder)
//IMPORTANT
//aLL the tree traversal comments are written in here are based on BST or Binary Search Tree.
//IMPORTANT
//============================================================================================================
//breadth_First_Search
//This method, visits tree's node horizontally and pushes them. It uses queue to keep track of its work.
//Example: 10
// 6 15
// 3 8 20
//result node would be =[10, 6, 15, 3, 8, 20]
const breadth_First_Search = tree => {
let node = tree.root;
let queue = [];
let visited = [];
queue.push(node);
while (queue.length) {
node = queue.shift();
visited.push(node.value);
if (node.left) queue.push(node.left);
if (node.right) queue.push(node.right);
}
return visited;
};
const totalNodeBFS = breadth_First_Search(BST_ROOT);
//============================================================================================================
//DFS_PreOrder
//This method, visits the one specific side(either left or right) at once, and after the whole side is finished, then visits the other side.
// as it goes through the tree, it pushes visited node continuously.
//Example: 10
// 6 15
// 3 8 20
//result node would be =[10, 6, 3, 8, 15, 20]
const DFS_PreOrder = tree => {
let data = [];
function traverse(node) {
data.push(node.value);
if (node.left) traverse(node.left);
if (node.right) traverse(node.right);
}
traverse(tree.root);
return data;
};
//============================================================================================================
//DFS_PostOrder
//this method is similar to DFS_PreOrder. Only difference is that, the DFS_PreOrder pushes node as it goes through,
//but DFS_PostOrder pushes node once it reaches the end and moves backwards from there as it pushes.
//Example: 10
// 6 15
// 3 8 20
//result node would be =[3, 8, 6, 20, 15, 10]
const DFS_PostOrder = tree => {
let data = [];
function traverse(node) {
data.push(node.value);
if (node.left) traverse(node.left);
if (node.right) traverse(node.right);
}
traverse(tree.root);
return data;
};
//============================================================================================================
//DFS_InOrder
//this method is similar to DFS_PreOrder. Main difference is that, once it finishes visiting/pushing one side, it visits the parent node and pushes it,
//and after that it goes to the other side. It happens recursively.
//One important thing, It returns sorted data from smaller to larger.
//Example: 10
// 6 15
// 3 8 20
//result node would be =[3, 6, 8, 10, 15, 20]
const DFS_INOrder = tree => {
let data = [];
function traverse(node) {
if (node.left) traverse(node.left);
data.push(node.value);
if (node.right) traverse(node.right);
}
traverse(tree.root);
return data;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment