{"id":28089130,"url":"https://github.com/adasarpan404/stl-javascript","last_synced_at":"2025-07-06T11:36:46.034Z","repository":{"id":41298698,"uuid":"497477316","full_name":"adasarpan404/stl-javascript","owner":"adasarpan404","description":"An Open Source Project for STL Javascript","archived":false,"fork":false,"pushed_at":"2024-02-27T15:06:25.000Z","size":561,"stargazers_count":11,"open_issues_count":0,"forks_count":4,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-02T02:39:23.077Z","etag":null,"topics":["algorithm","data-structures","hacktoberfest","heap","javascript","queue","stack"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/adasarpan404.png","metadata":{"files":{"readme":"Readme.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-05-29T03:07:57.000Z","updated_at":"2025-04-01T02:44:29.000Z","dependencies_parsed_at":"2024-02-27T03:27:37.899Z","dependency_job_id":"d62ac650-7bd8-47b9-bac0-641026bc941b","html_url":"https://github.com/adasarpan404/stl-javascript","commit_stats":{"total_commits":52,"total_committers":9,"mean_commits":5.777777777777778,"dds":0.4807692307692307,"last_synced_commit":"d5c501bc70c69e51b411dda850c88f590f8d4788"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adasarpan404%2Fstl-javascript","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adasarpan404%2Fstl-javascript/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adasarpan404%2Fstl-javascript/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adasarpan404%2Fstl-javascript/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/adasarpan404","download_url":"https://codeload.github.com/adasarpan404/stl-javascript/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253850542,"owners_count":21973662,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["algorithm","data-structures","hacktoberfest","heap","javascript","queue","stack"],"created_at":"2025-05-13T12:53:59.498Z","updated_at":"2025-05-13T12:54:00.417Z","avatar_url":"https://github.com/adasarpan404.png","language":"JavaScript","readme":"# 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) \r\n\r\nstl-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. \r\nSome of their use cases like binary number conversion, postfix evaluation\r\n\r\n## Installation\r\n\r\nUse the package manager [npm](https://www.npmjs.com/) to install STL-JAVASCRIPT.\r\n\r\n```bash\r\nnpm install stl-javascript\r\n```\r\nor you can use [yarn](https://yarnpkg.com/)\r\n```bash\r\nyarn add stl-javascript\r\n```\r\n## Usage\r\n### Stack\r\nA stack is a conceptual structure that combines a set of similarities and is based on the principle of end-to-end (LIFO). It is a type of invisible, commonly used data with two main functions, push and pop. Push and pop are made on the very top, which has just been added to the stack. The push function adds an object to the stack while the pop function removes the element from the surface. The stack concept is used to organize and organize memory on computers.\r\n```javascript\r\nconst {stack} = require('stl-javascript')\r\n\r\nconst stack_example = new stack();\r\n\r\nconsole.log(stack_example.isEmpty())\r\n//it will return true because in the starting stack will be empty\r\nconsole.log(stack_example.pop())\r\n// it will return underflow \r\nstack_example.push(10); // This pushes the number to the top of the stack\r\nstack_example.push(20);\r\nstack_example.push(30);\r\n\r\nconsole.log(stack_example.pop()) // this will return 30 , because 30 is at the top of the stack\r\nconsole.log(stack_example.peek()); // this will return 30, because 30 is at the top of the stack \r\n// this function is for getting top of the stack\r\nconsole.log(stack_example.printStack())// this will return \"10 20\"\r\n// This function is used for printing stack\r\n\r\n```\r\n\r\n\r\n### Queue\r\nA line is a sequential structure that follows the way certain tasks are performed. The order is First In First Out (FIFO). A good example of a line is any line of customer service where the first-time customer is provided first. The difference between the stacks and the line is minus. In the stem we take out something that has just been added; in line, we delete a newly added item.\r\n\r\n```javascript\r\nconst {queue}= require('stl-javascript')\r\nconst queue_example = new queue();\r\n\r\nconsole.log(queue_example.isEmpty()) //this will true because queue is empty at starting\r\nconsole.log(queue_example.dequeue()) // this will return underflow because queue is empty at starting\r\n\r\nqueue_example.enqueue(10)\r\n// this will be used to insert element in queue\r\nqueue_example.enqueue(20)\r\nqueue_example.enqueue(30)\r\n\r\nconsole.log(queue_example.dequeue()) // this will return 10 because 10 is at the front of the queue\r\nconsole.log(queue_example.front()) //this will return 20 because 20 is at the front of the queue\r\nconsole.log(queue_example.printQueue()) // this will return \"20 30 \". \r\n// this function is used for printing the queue\r\n```\r\n### Priority Queue\r\nPriority Queue is an abstract data type, which is similar to a queue, however, in the priority queue, every element has some priority. The priority of the elements in a priority queue determines the order in which elements are removed from the priority queue. Therefore all the elements are either arranged in an ascending or descending order.\r\n\r\nSo, a priority Queue is an extension of the queue with the following properties. \r\n\r\nEvery item has a priority associated with it.\r\nAn element with high priority is dequeued before an element with low priority.\r\nIf two elements have the same priority, they are served according to their order in the queue.\r\n```javascript\r\nconst {PriorityQueue} = require('stl-javascript/nonLinear/priorityQueue')\r\n\r\nconst PriorityQueue_example = new PriorityQueue();\r\n\r\nPriorityQueue_example.enqueue('Ram', 1);\r\nPriorityQueue_example.enqueue('Rohit', 3);\r\nPriorityQueue_example.enqueue('Mohit', 2);\r\n\r\nconsole.log(PriorityQueue_example.printPQueue()) // this will print \"Ram Mohit Rohit \" , elements are ordered due to priority\r\n\r\nconsole.log(PriorityQueue_example.isEmpty()) // this print false\r\n// this function will be used for checking queue is empty\r\n\r\nconsole.log(PriorityQueue_example.front()) // this print front of the queue e.g Ram\r\nconsole.log(PriorityQueue_example.rear()) // this print rear of the queue e.g Rohit \r\nconsole.log(PriorityQueue_example.dequeue()) // this print 'Ram'\r\n// in queue data structure, items are deleted from the front.\r\n\r\n\r\n```\r\n\r\n### Binary Search Tree\r\nBinary Search Tree is a node-based binary tree data structure which has the following properties:  \r\n\r\n* The left subtree of a node contains only nodes with keys lesser than the node’s key.\r\n\r\n* The right subtree of a node contains only nodes with keys greater than the node’s key.\r\n\r\n* The left and right subtree each must also be a binary search tree. \r\n\r\n* There must be no duplicate Node\r\n\r\n```javascript \r\n\r\n// create an object for the BinarySearchTree\r\nconst {BinarySearchTree} = require('stl-javascript/nonLinear/binarySearchTree')\r\nvar BST = new BinarySearchTree();\r\n \r\n// Inserting nodes to the BinarySearchTree\r\nBST.insert(15);\r\nBST.insert(25);\r\nBST.insert(10);\r\nBST.insert(7);\r\nBST.insert(22);\r\nBST.insert(17);\r\nBST.insert(13);\r\nBST.insert(5);\r\nBST.insert(9);\r\nBST.insert(27);\r\n                         \r\n//          15\r\n//         /  \\\r\n//        10   25\r\n//       / \\   / \\\r\n//      7  13 22  27\r\n//     / \\    /\r\n//    5   9  17\r\n \r\nvar root = BST.getRootNode();\r\n             \r\n// prints 5 7 9 10 13 15 17 22 25 27\r\nBST.inorder(root);\r\n             \r\n// Removing node with no children\r\nBST.remove(5);\r\n             \r\n             \r\n//          15\r\n//         /  \\\r\n//        10   25\r\n//       / \\   / \\\r\n//      7  13 22  27\r\n//       \\    /\r\n//        9  17\r\n             \r\n                         \r\nvar root = BST.getRootNode();\r\n             \r\n// prints 7 9 10 13 15 17 22 25 27\r\nBST.inorder(root);\r\n             \r\n// Removing node with one child\r\nBST.remove(7);\r\n             \r\n//          15\r\n//         /  \\\r\n//        10   25\r\n//       / \\   / \\\r\n//      9  13 22  27\r\n//            /\r\n//           17\r\n             \r\n             \r\nvar root = BST.getRootNode();\r\n \r\n// prints 9 10 13 15 17 22 25 27\r\nBST.inorder(root);\r\n             \r\n// Removing node with two children\r\nBST.remove(15);\r\n     \r\n//          17\r\n//         /  \\\r\n//        10   25\r\n//       / \\   / \\\r\n//      9  13 22  27\r\n \r\nvar root = BST.getRootNode();\r\nconsole.log(\"inorder traversal\");\r\n \r\n// prints 9 10 13 17 22 25 27\r\nBST.inorder(root);\r\n             \r\nconsole.log(\"postorder traversal\");\r\nBST.postorder(root);\r\nconsole.log(\"preorder traversal\");\r\nBST.preorder(root);\r\n\r\n```\r\n\r\n### Segment Tree\r\nSegment tree is a data structure that stores the information about an array in the form of tree, where each node of the tree represents a certain range of the array and carries the answer of that range. This allows one to answer range queries efficiently and at the same time, carry out faster modifications in the array. \r\n\r\nThe root node of the segment tree represents the full range of the array whereas the leaf nodes represent individual elements. A node's left child represents the left half of that node's range whereas its right child represents the right half of the node's range. In this implementation of the segment tree, any node that represents a range `l` to `r` will carry the sum of the range `l` to `r`.\r\n\r\nThe update function can be used to update the value at a particular index of the array. The query function can be used to obtain the sum of the range of all values from `start` index to `end` index, both included.\r\n\r\n```js\r\nconst {SegmentTree} = require('stl-javascript/nonLinear/segmentTree')\r\nlet arr = [15, 25, 10, 8, 22]\r\nlet segmentTree = new SegmentTree(arr);\r\n// The segment tree generated for this array\r\n//          80\r\n//         /  \\\r\n//        50   30\r\n//       / \\   / \\\r\n//      40 10 8  22\r\n//     / \\    \r\n//    15  25\r\n\r\n// Printing the sum of the value from the index 1 to 3, n this case, it is 43\r\nconsole.log(segmentTree.query(1,3))\r\n\r\n// Updating the value at index 1, setting it to 52\r\nsegmentTree.update(1,52)\r\n\r\n// Updated segment tree\r\n//          107\r\n//         /  \\\r\n//        77   30\r\n//       / \\   / \\\r\n//      67 10 8  22\r\n//     / \\    \r\n//    15  52\r\n\r\n// Printing the updated sum for the range 1 to 3, now the sum will be 70\r\nconsole.log(segmentTree.query(1,3))\r\n```\r\n\r\n## Application\r\n### PostFix Conversion \r\n\r\nuse the following code to get the answer of postfix expression\r\n```javascript \r\nconst {postFixConversion} = require('stl-javascript/uses/postfixConversion')\r\nconsole.log(postFixConversion(\"235*+8-\") //answer will be 9\r\nconsole.log(postFixConversion(23*+)) //it will return cannot perform postfix expression\r\n```\r\n## Contributing \r\nPull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.\r\n\r\nPlease make sure to update tests as appropriate.\r\n\r\n## License\r\n[MIT](https://github.com/adasarpan404/stl-javascript/blob/master/LICENSE)\r\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fadasarpan404%2Fstl-javascript","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fadasarpan404%2Fstl-javascript","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fadasarpan404%2Fstl-javascript/lists"}