◐ Shell
reader mode source ↗
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
File filter
Conversations
Jump to
Diff view
Apply and reload
Show whitespace
Diff view
Apply and reload
17 changes: 10 additions & 7 deletions Data-Structures/Tree/BinarySearchTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ const Node = (function Node() {
visit(output = (value) => console.log(value)) {
// Recursively go left
if (this.left !== null) {
this.left.visit()
}
// Print out value
output(this.value)
// Recursively go right
if (this.right !== null) {
this.right.visit()
}
}

Expand Down Expand Up @@ -116,20 +116,23 @@ const Tree = (function () {
}

// Inorder traversal
traverse() {
if (!this.root) {
// No nodes are there in the tree till now
return
}
this.root.visit()
}

// Start by searching the root
search(val) {
const found = this.root.search(val)
if (found !== null) {
return found.value
}
// not found
return null
}
Expand Down
66 changes: 66 additions & 0 deletions Data-Structures/Tree/test/BinarySearchTree.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
Toggle all file notes Toggle all file annotations