Created
May 29, 2022 14:54
-
-
Save fazeelanizam13/b9a6dca158374b63aeb48cc3761468db to your computer and use it in GitHub Desktop.
Implementation of depth-first search algorithm on a binary search tree in JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node { | |
// stores given value and points to nothing | |
constructor (value) { | |
this.value = value | |
this.left = null | |
this.right = null | |
} | |
} | |
class Tree { | |
// initially just has a root variable with null assigned | |
constructor () { | |
this.root = null | |
} | |
// helper functions | |
// starting from root, traverses through a tree/subtree and | |
// inserts new node depending on its value | |
traverseAndInsert = (root, node) => { | |
// if new node value less than root | |
if (node.value < root.value) { | |
// if left node null, make new node the left node | |
if (root.left === null) root.left = node | |
// else get traverseAndInsert to find the correct place in left subtree to insert new node | |
else this.traverseAndInsert(root.left, node) | |
// if new node value greater than root | |
} else { | |
// if right node null, make new node the right node | |
if (root.right === null) root.right = node | |
// else get traverseAndInsert to find the correct place in right subtree to insert new node | |
else this.traverseAndInsert(root.right, node) | |
} | |
} | |
// end of helper functions | |
// to insert nodes to tree | |
insert = value => { | |
// create new node with given value | |
let node = new Node(value) | |
// if root null, make new node the root | |
if (this.root === null) this.root = node | |
// if not, get traverseAndInsert to find the correct place to insert new node | |
else this.traverseAndInsert(this.root, node) | |
} | |
// -------- DFS variations ---------- | |
// left subtree -> root -> right subtree | |
traverseInOrder = root => { | |
if (root !== null) { | |
this.traverseInOrder(root.left) | |
console.log(root.value) | |
this.traverseInOrder(root.right) | |
} | |
} | |
// root -> left subtree -> right subtree | |
traversePreOrder = root => { | |
if (root !== null) { | |
console.log(root.value) | |
this.traversePreOrder(root.left) | |
this.traversePreOrder(root.right) | |
} | |
} | |
// left subtree -> right subtree -> root | |
traversePostOrder = root => { | |
if (root !== null) { | |
this.traversePostOrder(root.left) | |
this.traversePostOrder(root.right) | |
console.log(root.value) | |
} | |
} | |
} | |
// tests | |
// let tree = new Tree() | |
// tree.insert(7) | |
// tree.insert(24) | |
// tree.insert(9) | |
// tree.insert(36) | |
// tree.insert(21) | |
// console.log(tree.root) | |
// tree.traverseInOrder(tree.root) | |
// console.log('-----') | |
// tree.traversePreOrder(tree.root) | |
// console.log('-----') | |
// tree.traversePostOrder(tree.root) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment