{"id":47603930,"url":"https://github.com/kylehue/my-dsa","last_synced_at":"2026-04-01T19:01:13.561Z","repository":{"id":252337607,"uuid":"840078928","full_name":"kylehue/my-dsa","owner":"kylehue","description":"A collection of reusable data structures in TypeScript.","archived":false,"fork":false,"pushed_at":"2025-10-19T12:59:14.000Z","size":229,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-11-27T09:17:13.570Z","etag":null,"topics":["algorithms","data-structures"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kylehue.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"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":"2024-08-08T23:42:07.000Z","updated_at":"2025-10-19T12:59:17.000Z","dependencies_parsed_at":null,"dependency_job_id":"508366cd-7e75-4bcc-bbae-9ad0ec37dfff","html_url":"https://github.com/kylehue/my-dsa","commit_stats":null,"previous_names":["kylehue/dsa-js"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/kylehue/my-dsa","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kylehue%2Fmy-dsa","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kylehue%2Fmy-dsa/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kylehue%2Fmy-dsa/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kylehue%2Fmy-dsa/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kylehue","download_url":"https://codeload.github.com/kylehue/my-dsa/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kylehue%2Fmy-dsa/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31291007,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-01T13:12:26.723Z","status":"ssl_error","status_checked_at":"2026-04-01T13:12:25.102Z","response_time":53,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["algorithms","data-structures"],"created_at":"2026-04-01T19:00:40.422Z","updated_at":"2026-04-01T19:01:13.553Z","avatar_url":"https://github.com/kylehue.png","language":"TypeScript","readme":"# my-dsa\n\n**my-dsa** is a JavaScript library that provides a collection of reusable data structures.\n\n## Table of Contents\n- [my-dsa](#my-dsa)\n  - [Table of Contents](#table-of-contents)\n  - [Installation](#installation)\n  - [LinkedList](#linkedlist)\n  - [Queue](#queue)\n  - [Deque](#deque)\n  - [Heap](#heap)\n  - [DisjointSet](#disjointset)\n  - [Trie](#trie)\n  - [Quadtree](#quadtree)\n  - [HashGrid](#hashgrid)\n  - [LRUCache](#lrucache)\n  - [SegmentTree](#segmenttree)\n  - [BinarySearchTree](#binarysearchtree)\n  - [IntervalTree](#intervaltree)\n  - [Contributing](#contributing)\n\n## Installation\n\nYou can install using npm:\n```bash\nnpm install my-dsa\n```\n\nor CDN:\n```bash\nhttps://cdn.jsdelivr.net/npm/my-dsa/dist/umd/index.js\n```\nThe library will be accessible through the global name `mydsa`.\n\n## LinkedList\nA Linked List is a way to store a sequence of items where each item points to the next one.\n```ts\nimport { LinkedList } from \"my-dsa\";\n\nlet list = new LinkedList\u003cnumber\u003e();\n\n// Or create a linked list from an array\nlist = LinkedList.fromArray([1, 2, 3]);\n\n// Adds a node to the end of the list\nlet node = list.append(4);\n\n// Adds a node to the start of the list\nlet node = list.prepend(4);\n\n// Removes a node from the list\nlist.deleteNode(node);\n\n// Adds a node after a specific node\nlet newNodeAfter = list.insertAfter(node, 5);\n\n// Adds a node before a specific node\nlet newNodeBefore = list.insertBefore(node, 0);\n\n// Finds a node by value\nlet foundNode = list.find(3);\n\n// Clones the linked list\nlet clone = list.clone();\n\n// Removes all nodes from the list\nlist.clear();\n\n// Get the number of nodes in the list\nlet size = list.size();\n\n// Check if the list is empty\nlet isEmpty = list.isEmpty();\n\n// Convert the list to an array\nlet array = list.toArray();\n\n// Get the head node\nlet headNode = list.head();\n\n// Get the tail node\nlet tailNode = list.tail();\n\n// Iterate through the linked list\nfor (let node of list) {\n   console.log(node.value);\n}\n```\n\n## Queue\nA Queue is a simple way to store items in order, like a line of people. You add items at the back (enqueue) and remove them from the front (dequeue).\n```ts\nimport { Queue } from \"my-dsa\";\n\nlet queue = new Queue\u003cnumber\u003e();\n\n// Or create a queue like this:\nqueue = Queue.fromArray([1, 2, 3]);\n\n// Adds an item in the back\nqueue.enqueue(2);\n\n// Removes an item in front\nlet front = queue.dequeue();\n\n// Get the size of the queue\nlet size = queue.size();\n\n// Clears the queue\nqueue.clear(); \n\n// Check if queue is empty\nlet isEmpty = queue.isEmpty(); \n\n// Peek the front element\nlet front = queue.front();\n\n// Peek the back element\nlet back = queue.back();\n\n// Clones the queue\nlet clone = queue.clone();\n\n// Convert the queue to array\nlet array = queue.toArray();\n\n// Iterate through the queue\nfor (let item of queue) {\n   console.log(item);\n}\n```\n\n## Deque\nDeque or Double-Ended Queue is similar to queue except that you can add an element in front and dequeue from the back. It extends the [Queue](#queue) data structure.\n```ts\nimport { Deque } from \"my-dsa\";\n\nconst deque = new Deque\u003cnumber\u003e();\n\n// Adds an item in front\ndeque.enqueueFront(2);\n\n// Removes an item in the back\ndeque.dequeueBack();\n```\n\n## Heap\nA Heap is a type of tree structure that helps manage a collection of items, where the lowest/highest priority item can be instantly accessed.\n```ts\nimport { Heap } from \"my-dsa\";\n\nlet heap = new Heap\u003cnumber\u003e();\n\n// Or create a heap with a custom comparator\nheap = new Heap\u003cnumber\u003e((a, b) =\u003e b - a); // Max-heap\n\n// Or create a heap from an array\nheap = Heap.fromArray([4, 2, 7, 1]);\n\n// Or create a max heap from an array\nheap = Heap.fromArray([4, 2, 7, 1], (a, b) =\u003e b - a);\n\n// Get the size of the heap\nlet size = heap.size();\n\n// Check if the heap is empty\nlet isEmpty = heap.isEmpty();\n\n// Peek at the top element\nlet top = heap.peek();\n\n// Add elements to the heap\nheap.push(5, 3, 8, 1);\n\n// Remove the top element\nlet top = heap.pop();\n\n// Clears the heap\nheap.clear();\n\n// Clone the heap\nlet clone = heap.clone();\n\n// Convert the heap to an array\nlet array = heap.toArray();\n```\n\n## DisjointSet\nDisjoint Set or Union-Find is useful for efficiently managing and merging groups of connected elements, making it ideal for tasks like tracking connected components in graphs.\n```ts\nimport { DisjointSet } from \"my-dsa\";\n\nlet ds = new DisjointSet\u003cnumber\u003e();\n\n// Add nodes to the disjoint set\nds.add(1);\nds.add(2);\nds.add(3);\n\n// Find the root of the set containing a node\nlet root = ds.find(2);\n\n// Find or add a node to the disjoint set\nlet root = ds.findOrAdd(4);\n\n// Union two sets containing the specified nodes\nlet isAlreadyUnified = ds.union(1, 3);\n\n// Get the size of the disjoint set\nlet size = ds.size();\n```\n\n## Trie\nThis is ideal for managing a dynamic set of strings, allowing for efficient prefix searches, autocomplete features, and maybe spell-checking.\n```ts\nimport { Trie } from \"my-dsa\";\n\n// Create a new trie\nlet trie = new Trie();\n\n// Insert words into the trie\ntrie.insert(\"apple\");\ntrie.insert(\"app\");\n\n// Search for a word in the trie\nlet isFound = trie.search(\"app\");\n\n// Check if any word starts with the given prefix\nlet startsWith = trie.startsWith(\"ap\");\n\n// Delete a word from the trie\nlet isDeleted = trie.delete(\"apple\");\n\n// Get all words in the trie\nlet words = trie.getAllWords();\n\n// Check if the trie is empty\nlet isEmpty = trie.isEmpty();\n\n// Clear all words from the trie\ntrie.clear();\n\n// Find the longest prefix of the given word that exists in the trie\nlet longestPrefix = trie.longestPrefixMatch(\"applepie\");\n\n// Autocomplete words with a given prefix\nlet suggestions = trie.autocomplete(\"app\");\n```\n\n## Quadtree\nThis is useful for querying objects in 2D space, allowing efficient spatial searches like finding nearby objects.\n```ts\nimport { Quadtree } from \"my-dsa\";\n\n// Create a new quadtree with specified bounds and configuration\nlet bounds = { x: 0, y: 0, width: 100, height: 100 };\nlet config = { maxDepth: 5 };\nlet quadtree = new Quadtree(bounds, config);\n\n// Insert an item into the quadtree\nlet item = { x: 10, y: 10, width: 5, height: 5 };\nquadtree.insert(item);\n\n// Retrieve all objects within specified bounds\nlet searchBounds = { x: 5, y: 5, width: 20, height: 20 };\nlet retrievedItems = quadtree.retrieve(searchBounds);\n```\n\n## HashGrid\nSame as quadtree, but a bit simpler and faster.\n```ts\nimport { HashGrid } from \"my-dsa\";\n\nconst hashGrid = new HashGrid();\n\n// Insert\nlet item = { x: 10, y: 10, width: 5, height: 5 };\nhashGrid.insert({});\n\n// Retrieve\nlet searchBounds = { x: 5, y: 5, width: 20, height: 20 };\nlet retrievedItems = hashGrid.retrieve(searchBounds);\n```\n\n## LRUCache\nLRUCache or Least-Recently-Used Cache acts exactly like a hashmap\nexcept that it removes entries that are least recently used when it\nhits the max capacity.\n```ts\nimport { LRUCache } from \"my-dsa\";\n\n// Create an LRUCache with a specific capacity\nlet cache = new LRUCache\u003cstring, number\u003e(3);\n\n// Adds a new element to the cache\ncache.set(\"a\", 1);\ncache.set(\"b\", 2);\n\n// Retrieves a value from the cache by key\nlet value = cache.get(\"a\");\n\n// Check if a key exists in the cache\nlet exists = cache.has(\"b\");\n\n// Update an existing key with a new value\ncache.set(\"a\", 10);\n\n// Deletes an element from the cache\nlet wasDeleted = cache.delete(\"a\");\n\n// Clear all elements from the cache\ncache.clear();\n\n// Get the current size of the cache\nlet size = cache.size();\n\n// Support for-of iteration to loop over entries\nfor (let [key, value] of cache) {\n   console.log(`${key}: ${value}`);\n}\n```\n\n## SegmentTree\nThis is useful if you want to query sub-array operations in logarithmic time.\n```ts\nimport { SegmentTree } from \"my-dsa\";\n\n// Create a SegmentTree (default query is sum)\nlet segTree = new SegmentTree([1, 2, 3, 4, 5]);\n\n// Query the sum over a range\nlet sum = segTree.query(1, 3); // 9\n\n// Update an element at a specific index\nsegTree.update(2, 10);\n\n// Create a SegmentTree for querying min values\nlet minTree = new SegmentTree([3, 5, 2, 7, 1], (a, b) =\u003e Math.min(a, b));\n\n// Query the minimum over a range\n// Note that we have to set the initial value of result to Infinity\nlet min = minTree.query(1, 3, Infinity); // 2\n```\n\n## BinarySearchTree\nA binary search tree allows for efficient searching, insertion, and deletion of elements. This is an implementation of [AVLTree](https://en.wikipedia.org/wiki/AVL_tree), or self-balancing binary search tree.\n```ts\nimport { BinarySearchTree } from \"my-dsa\";\n\n// Create a binary search tree\nlet bst = new BinarySearchTree();\n\n// or create a binary search tree with custom comparator\nbst = new BinarySearchTree((a, b) =\u003e a - b);\n\n// Insert values into the tree\nbst.insert(30);\n\n// Check the size of the tree\nlet size = bst.size();\n\n// Find the minimum and maximum values\nlet min = bst.min();\nlet max = bst.max();\n\n// Delete a value from the tree\nbst.delete(20);\n\n// Filter the tree\nbst.filter((x) =\u003e x \u003e 20);\n\n// Check if the tree is empty\nlet isEmpty = bst.isEmpty();\n\n// Access the root\nlet root = bst.root();\n\n// Create a tree from an array\nlet newTree = BinarySearchTree.fromArray([50, 30, 70, 20, 40]);\n\n// Create a tree from a sorted array (faster)\nlet newTree = BinarySearchTree.fromSortedArray([1, 2, 3, 4]);\n\n// Loop (in-order)\nfor (let value of bst) {\n   console.log(value);\n}\n```\n\n## IntervalTree\nA data structure for managing intervals and querying overlapping intervals.\n```ts\nimport { IntervalTree } from \"my-dsa\";\n\ntype IntervalObject = {\n  name: string;\n  interval: [number, number];\n};\n\n// Create instance\nlet intervalTree = new IntervalTree\u003cIntervalObject\u003e((data) =\u003e data.interval);\n\n// Insert intervals into the tree\nlet sampleData: IntervalObject = { name: \"sample\", interval: [10, 20] };\nintervalTree.insert(sampleData);\n\n// Query overlapping intervals\nlet overlappingIntervals = intervalTree.query(12, 22);\n\n// Check the size of the tree (number of intervals)\nlet size = intervalTree.size();\n\n// Delete an interval from the tree\nintervalTree.delete(sampleData);\n\n// Filter the tree\nintervalTree.filter((x) =\u003e x[0] \u003e 20);\n\n// Delete intervals that overlap within range\nintervalTree.deleteInRange(5, 10);\n\n// Check if the tree is empty\nlet isEmpty = intervalTree.isEmpty();\n\n// Access the root\nlet root = intervalTree.root();\n\n// Clear the tree\nintervalTree.clear();\n\n// Create a tree from an array of intervals\nlet tree = IntervalTree.fromArray\u003cIntervalObject\u003e(array, rangeMapper);\n\n// Query intervals that overlap a specific range\nfor (let interval of intervalTree.query(10, 25)) {\n   console.log(interval.name, interval.interval);\n}\n\n// For-of loop\nfor (let interval of intervalTree) {\n   console.log(interval.name, interval.interval);\n}\n```\n\n## Contributing\nContributions are welcome! Please submit a pull request or open an issue if you have any ideas, improvements, or suggestions.","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkylehue%2Fmy-dsa","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkylehue%2Fmy-dsa","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkylehue%2Fmy-dsa/lists"}