-
Notifications
You must be signed in to change notification settings - Fork 4
Tests/binarysearch #39
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
Changes from all commits
b833c63
de9e29f
3833a9a
2a039a2
6c40850
7eded57
46119d2
38a69dc
c8bde1d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,10 @@ | ||
| /node_modules | ||
| /node_modules | ||
|
|
||
| /coverage | ||
| /coverage/ | ||
| /coverage/* | ||
| npm-debug.log* | ||
| yarn-debug.log* | ||
| yarn-error.log* | ||
| .DS_Store | ||
| .env |
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,7 +1,8 @@ | ||||||||||||||
| # stl-javascript    | ||||||||||||||
| # stl-javascript    | ||||||||||||||
|
|
||||||||||||||
| stl-javascript is a javascript library that helps you to use common data structures like stack, queue, priority queue, and circular queue with out any headache. | ||||||||||||||
| Some of their use cases like binary number conversion, postfix evaluation | ||||||||||||||
| stl-javascript is a concise, well-tested library of core data structures and algorithms for JavaScript — stacks, queues, priority queues, circular queues, segment trees, and binary search trees. It is designed for learning, teaching, and lightweight production use, with clear examples and comprehensive tests. | ||||||||||||||
|
|
||||||||||||||
| Common use cases include binary number conversion and postfix expression evaluation. | ||||||||||||||
|
|
||||||||||||||
| ## Installation | ||||||||||||||
|
|
||||||||||||||
|
|
@@ -96,6 +97,12 @@ const root = bst.getRootNode() | |||||||||||||
| console.log(bst.inorder(root)) // -> [10, 15, 20] | ||||||||||||||
| console.log(bst.preorder(root)) // -> [15, 10, 20] | ||||||||||||||
| console.log(bst.postorder(root)) // -> [10, 20, 15] | ||||||||||||||
|
|
||||||||||||||
| // Level-order and view helpers | ||||||||||||||
| console.log(bst.levelOrder()) // -> [15, 10, 20] | ||||||||||||||
| console.log(bst.leftView()) // -> [15, 10] | ||||||||||||||
| console.log(bst.rightView()) // -> [15, 20] | ||||||||||||||
|
||||||||||||||
| console.log(bst.rightView()) // -> [15, 20] | |
| console.log(bst.levelOrder()) // -> [15, 10, 20] | |
| console.log(bst.leftView(root)) // -> [15, 10] | |
| console.log(bst.rightView()) // -> [15, 20] |
Copilot
AI
Aug 17, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The rightView method call is missing the required node parameter. It should be bst.rightView(root) to match the implementation.
| console.log(bst.topView()) // -> [10, 15, 20] // leftmost -> rightmost horizontal distance | |
| console.log(bst.leftView()) // -> [15, 10] | |
| console.log(bst.rightView(root)) // -> [15, 20] | |
| console.log(bst.topView()) // -> [10, 15, 20] // leftmost -> rightmost horizontal distance |
Copilot
AI
Aug 17, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The topView method call is missing the required node parameter. It should be bst.topView(root) to match the implementation.
| console.log(bst.topView()) // -> [10, 15, 20] // leftmost -> rightmost horizontal distance | |
| console.log(bst.topView(root)) // -> [10, 15, 20] // leftmost -> rightmost horizontal distance |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,118 +1,165 @@ | ||
| class Node { | ||
| constructor(data) { | ||
| this.data = data; | ||
| this.left = null; | ||
| this.right = null; | ||
| } | ||
| function Node(data) { | ||
| this.data = data; | ||
| this.left = null; | ||
| this.right = null; | ||
| } | ||
|
|
||
| class BinarySearchTree { | ||
| constructor() { | ||
| this.root = null; | ||
| } | ||
| function BinarySearchTree() { | ||
| this.root = null; | ||
| } | ||
|
|
||
| insert(data) { | ||
| var newNode = new Node(data); | ||
| if (this.root === null) | ||
| this.root = newNode; | ||
| else | ||
| this.insertNode(this.root, newNode); | ||
| } | ||
| insertNode(node, newNode) { | ||
| if (newNode.data < node.data) { | ||
| if (node.left === null) | ||
| node.left = newNode; | ||
| else | ||
| this.insertNode(node.left, newNode); | ||
| } else { | ||
| if (node.right === null) | ||
| node.right = newNode; | ||
| else | ||
| this.insertNode(node.right, newNode); | ||
| } | ||
| BinarySearchTree.prototype.insert = function (data) { | ||
| var newNode = new Node(data); | ||
| if (this.root === null) this.root = newNode; | ||
| else this.insertNode(this.root, newNode); | ||
| }; | ||
|
|
||
| BinarySearchTree.prototype.insertNode = function (node, newNode) { | ||
| if (newNode.data < node.data) { | ||
| if (node.left === null) node.left = newNode; | ||
| else this.insertNode(node.left, newNode); | ||
| } else { | ||
| if (node.right === null) node.right = newNode; | ||
| else this.insertNode(node.right, newNode); | ||
| } | ||
| remove(data) { | ||
| this.root = this.removeNode(this.root, data); | ||
| } | ||
| removeNode(node, key) { | ||
| if (node === null) { | ||
| return null; | ||
| } | ||
| else if (key < node.data) { | ||
| node.left = this.removeNode(node.left, key); | ||
| }; | ||
|
|
||
| BinarySearchTree.prototype.remove = function (data) { | ||
| this.root = this.removeNode(this.root, data); | ||
| }; | ||
|
|
||
| BinarySearchTree.prototype.removeNode = function (node, key) { | ||
| if (node === null) { | ||
| return null; | ||
| } else if (key < node.data) { | ||
| node.left = this.removeNode(node.left, key); | ||
| return node; | ||
| } else if (key > node.data) { | ||
| node.right = this.removeNode(node.right, key); | ||
| return node; | ||
| } else { | ||
| if (node.left === null && node.right === null) { | ||
| node = null; | ||
| return node; | ||
| } else if (key > node.data) { | ||
| node.right = this.removeNode(node.right, key); | ||
| } | ||
| if (node.left === null) { | ||
| node = node.right; | ||
| return node; | ||
| } else { | ||
| if (node.left === null && node.right === null) { | ||
| node = null; | ||
| return node; | ||
| } | ||
| if (node.left === null) { | ||
| node = node.right; | ||
| return node; | ||
| } else if (node.right === null) { | ||
| node = node.left; | ||
| return node; | ||
| } | ||
| var aux = this.findMinNode(node.right); | ||
| node.data = aux.data; | ||
|
|
||
| node.right = this.removeNode(node.right, aux.data); | ||
| } else if (node.right === null) { | ||
| node = node.left; | ||
| return node; | ||
| } | ||
| var aux = this.findMinNode(node.right); | ||
| node.data = aux.data; | ||
| node.right = this.removeNode(node.right, aux.data); | ||
| return node; | ||
| } | ||
| inorder(node) { | ||
| if (!node) return [] | ||
| return this.inorder(node.left).concat(node.data).concat(this.inorder(node.right)) | ||
| } | ||
| preorder(node) { | ||
| let ans = []; | ||
| }; | ||
|
|
||
| if (!node) return ans; | ||
| BinarySearchTree.prototype.inorder = function (node) { | ||
| if (!node) return []; | ||
| return this.inorder(node.left).concat(node.data).concat(this.inorder(node.right)); | ||
| }; | ||
|
|
||
| ans.push(node.data); | ||
| if (node.left) ans = ans.concat(this.preorder(node.left)); | ||
| if (node.right) ans = ans.concat(this.preorder(node.right)); | ||
| BinarySearchTree.prototype.preorder = function (node) { | ||
| var ans = []; | ||
| if (!node) return ans; | ||
| ans.push(node.data); | ||
| if (node.left) ans = ans.concat(this.preorder(node.left)); | ||
| if (node.right) ans = ans.concat(this.preorder(node.right)); | ||
| return ans; | ||
| }; | ||
|
|
||
| BinarySearchTree.prototype.postorder = function (node) { | ||
| if (!node) return []; | ||
| return this.postorder(node.left).concat(this.postorder(node.right)).concat(node.data); | ||
| }; | ||
|
|
||
| return ans; | ||
| } | ||
| postorder(node) { | ||
| if (!node) return [] | ||
| return this.postorder(node.left).concat(this.postorder(node.right)).concat(node.data) | ||
| BinarySearchTree.prototype.findMinNode = function (node) { | ||
| if (node.left === null) return node; | ||
| else return this.findMinNode(node.left); | ||
| }; | ||
|
|
||
| BinarySearchTree.prototype.search = function (node, data) { | ||
| if (node === null) return null; | ||
| else if (data < node.data) return this.search(node.left, data); | ||
| else if (data > node.data) return this.search(node.right, data); | ||
| else return node; | ||
| }; | ||
|
|
||
| BinarySearchTree.prototype.getRootNode = function () { | ||
| return this.root; | ||
| }; | ||
|
|
||
| BinarySearchTree.prototype.levelOrder = function (node) { | ||
| if (!node) return []; | ||
| var res = []; | ||
| var q = [node]; | ||
| while (q.length) { | ||
| var curr = q.shift(); | ||
| res.push(curr.data); | ||
| if (curr.left) q.push(curr.left); | ||
| if (curr.right) q.push(curr.right); | ||
| } | ||
| //Helper Methods | ||
| findMinNode(node) { | ||
| if (node.left === null) | ||
| return node; | ||
| else | ||
| return this.findMinNode(node.left); | ||
| return res; | ||
| }; | ||
|
|
||
| BinarySearchTree.prototype.leftView = function (node) { | ||
| if (!node) return []; | ||
| var res = []; | ||
| var q = [node]; | ||
| while (q.length) { | ||
| var levelSize = q.length; | ||
| for (var i = 0; i < levelSize; i++) { | ||
| var curr = q.shift(); | ||
| if (i === 0) res.push(curr.data); | ||
| if (curr.left) q.push(curr.left); | ||
| if (curr.right) q.push(curr.right); | ||
| } | ||
| } | ||
| search(node, data) { | ||
| if (node === null) | ||
| return null; | ||
| else if (data < node.data) | ||
| return this.search(node.left, data); | ||
| return res; | ||
| }; | ||
|
|
||
| else if (data > node.data) | ||
| return this.search(node.right, data); | ||
| BinarySearchTree.prototype.rightView = function (node) { | ||
| if (!node) return []; | ||
| var res = []; | ||
| var q = [node]; | ||
| while (q.length) { | ||
| var levelSize = q.length; | ||
| for (var i = 0; i < levelSize; i++) { | ||
| var curr = q.shift(); | ||
| if (i === levelSize - 1) res.push(curr.data); | ||
| if (curr.left) q.push(curr.left); | ||
| if (curr.right) q.push(curr.right); | ||
| } | ||
| } | ||
| return res; | ||
| }; | ||
|
|
||
| else | ||
| return node; | ||
| BinarySearchTree.prototype.topView = function (node) { | ||
| if (!node) return []; | ||
| var map = {}; // hd -> node.data (first seen) | ||
| var minHd = 0, maxHd = 0; | ||
| var q = [{ node: node, hd: 0 }]; | ||
| while (q.length) { | ||
| var pair = q.shift(); | ||
| var curr = pair.node; | ||
| var hd = pair.hd; | ||
| if (map[hd] === undefined) map[hd] = curr.data; | ||
| if (curr.left) { | ||
| q.push({ node: curr.left, hd: hd - 1 }); | ||
| if (hd - 1 < minHd) minHd = hd - 1; | ||
| } | ||
| if (curr.right) { | ||
| q.push({ node: curr.right, hd: hd + 1 }); | ||
| if (hd + 1 > maxHd) maxHd = hd + 1; | ||
| } | ||
| } | ||
| getRootNode() { | ||
| return this.root; | ||
| var res = []; | ||
| for (var h = minHd; h <= maxHd; h++) { | ||
| if (map[h] !== undefined) res.push(map[h]); | ||
| } | ||
| } | ||
| return res; | ||
| }; | ||
|
|
||
| // var BST = new BinarySearchTree() | ||
| // const Array = [15, 25, 10, 7, 22, 17, 13, 5, 9, 27] | ||
| // for(var i=0; i<Array.length; i++){ | ||
| // BST.insert(Array[i]) | ||
| // } | ||
| // var node = BST.getRootNode() | ||
| // console.log(BST.preorder(node)) | ||
| module.exports = { BinarySearchTree } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The levelOrder method call is missing the required node parameter. It should be
bst.levelOrder(root)to match the implementation.