Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion .gitignore
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
19 changes: 13 additions & 6 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# stl-javascript ![NPM](https://img.shields.io/npm/l/stl-javascript) ![npm](https://img.shields.io/npm/dt/stl-javascript) ![npm](https://img.shields.io/npm/v/stl-javascript)
# stl-javascript ![NPM](https://img.shields.io/npm/l/stl-javascript) ![npm](https://img.shields.io/npm/dt/stl-javascript) ![npm](https://img.shields.io/npm/v/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

Expand Down Expand Up @@ -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]
Copy link

Copilot AI Aug 17, 2025

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.

Suggested change
console.log(bst.levelOrder()) // -> [15, 10, 20]
console.log(bst.levelOrder(root)) // -> [15, 10, 20]

Copilot uses AI. Check for mistakes.
console.log(bst.leftView()) // -> [15, 10]
console.log(bst.rightView()) // -> [15, 20]
Copy link

Copilot AI Aug 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The leftView method call is missing the required node parameter. It should be bst.leftView(root) to match the implementation.

Suggested change
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 uses AI. Check for mistakes.
console.log(bst.topView()) // -> [10, 15, 20] // leftmost -> rightmost horizontal distance
Copy link

Copilot AI Aug 17, 2025

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.

Suggested change
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 uses AI. Check for mistakes.
Copy link

Copilot AI Aug 17, 2025

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.

Suggested change
console.log(bst.topView()) // -> [10, 15, 20] // leftmost -> rightmost horizontal distance
console.log(bst.topView(root)) // -> [10, 15, 20] // leftmost -> rightmost horizontal distance

Copilot uses AI. Check for mistakes.
```

Notes:
Expand Down Expand Up @@ -123,10 +130,10 @@ Notes:
### PostFix Conversion

use the following code to get the answer of postfix expression
```javascript
```javascript
const {postFixConversion} = require('stl-javascript/uses/postfixConversion')
console.log(postFixConversion("235*+8-") //answer will be 9
console.log(postFixConversion(23*+)) //it will return cannot perform postfix expression
console.log(postFixConversion("235*+8-")) // -> 9
console.log(postFixConversion("23*+")) // -> "Can't perform postfix evaluation"
```
## Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Expand Down
239 changes: 143 additions & 96 deletions nonLinear/binarySearchTree.js
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 }
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "stl-javascript",
"version": "3.0.2",
"version": "3.1.0",
"description": "I try to create easy way to use stack, queue and some data structure in Javascript",
"main": "index.js",
"scripts": {
Expand Down
Loading
Loading