-
Notifications
You must be signed in to change notification settings - Fork 269
Added BST #49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Added BST #49
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
5aeb51b
fix: More meaningful name
ashokdey 421200c
Merge branch 'master' of github.com:knaxus/problem-solving-javascript...
ashokdey 31c5648
refactor: folder structure
ashokdey f32e9aa
update: BST insertion added
ashokdey 7b65d6a
update: preorder traversal
ashokdey 22787dd
update: return pre-order traversal as an array
ashokdey 312e43a
cleanup
ashokdey 34593cb
cleanup
ashokdey 78cb213
update: implementation of Inorder Traversal
ashokdey 5fd958e
update: added Postorder traversal
ashokdey 8fae46e
update: entry for trees in README
ashokdey 3475013
update: search in BST
ashokdey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
7 changes: 7 additions & 0 deletions
src/_DataStructures_/Trees/BST/Node.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module.exports = class Node { | ||
constructor(value) { | ||
this.value = value; | ||
this.leftChild = null; // will be a node | ||
this.rightChild = null; // will be a node | ||
} | ||
}; |
110 changes: 110 additions & 0 deletions
src/_DataStructures_/Trees/BST/index.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
const Node = require('./Node'); | ||
|
||
class BinarySearchTree { | ||
constructor(value) { | ||
this.root = new Node(value); | ||
} | ||
|
||
insert(root, value) { | ||
if (root === null) { | ||
const newNode = new Node(value); | ||
// eslint-disable-next-line no-param-reassign | ||
root = newNode; | ||
return root; | ||
} | ||
|
||
if (value < root.value) { | ||
// eslint-disable-next-line no-param-reassign | ||
root.leftChild = this.insert(root.leftChild, value); | ||
return root; | ||
} | ||
if (value > root.value) { | ||
// eslint-disable-next-line no-param-reassign | ||
root.rightChild = this.insert(root.rightChild, value); | ||
return root; | ||
} | ||
return root; | ||
} | ||
|
||
preorder(root) { | ||
/** returning an array so as to make testing easy */ | ||
let arr = []; | ||
if (root === null) return []; | ||
arr.push(root.value); | ||
|
||
const left = this.preorder(root.leftChild); | ||
arr = [...arr, ...left]; | ||
|
||
const right = this.preorder(root.rightChild); | ||
arr = [...arr, ...right]; | ||
return arr; | ||
} | ||
|
||
inorder(root) { | ||
/** left - root - right */ | ||
if (root === null) return []; | ||
let arr = []; | ||
const left = this.inorder(root.leftChild); | ||
arr = [...left, ...arr]; | ||
|
||
// print root | ||
arr = [...arr, root.value]; | ||
|
||
const right = this.inorder(root.rightChild); | ||
arr = [...arr, ...right]; | ||
return arr; | ||
} | ||
|
||
postorder(root) { | ||
/** left - right - root */ | ||
|
||
if (root === null) return []; | ||
let arr = []; | ||
|
||
const left = this.postorder(root.leftChild); | ||
arr = [...left, ...arr]; | ||
|
||
const right = this.postorder(root.rightChild); | ||
arr = [...arr, ...right]; | ||
|
||
return [...arr, root.value]; | ||
} | ||
|
||
search(root, value) { | ||
if (root === null) return false; | ||
if (value === root.value) return true; | ||
|
||
if (value < root.value) { | ||
return this.search(root.leftChild, value); | ||
} | ||
if (value > root.value) { | ||
return this.search(root.rightChild, value); | ||
} | ||
return false; | ||
} | ||
} | ||
|
||
// const bst = new BinarySearchTree(6); | ||
// console.log(bst.root); | ||
// bst.insert(bst.root, 4); | ||
// bst.insert(bst.root, 9); | ||
// bst.insert(bst.root, 2); | ||
// bst.insert(bst.root, 5); | ||
// bst.insert(bst.root, 8); | ||
// bst.insert(bst.root, 12); | ||
|
||
// console.log(bst.root); | ||
|
||
// const preorder = bst.preorder(bst.root); | ||
// console.log('Preorder Traversal - ', preorder); | ||
|
||
// const inorder = bst.inorder(bst.root); | ||
// console.log('Inorder Traversal - ', inorder); | ||
|
||
// const postorder = bst.postorder(bst.root); | ||
// console.log('Postorder Traversal - ', postorder); | ||
|
||
// const search = 18; | ||
// console.log(`Search for ${search}`, bst.search(bst.root, search)); | ||
|
||
module.exports = BinarySearchTree; |
File renamed without changes.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.