Skip to main content
On this page

@std/data-structures

Overview Jump to heading

Data structures for use in algorithms and other data manipulation.

import { BinarySearchTree } from "@std/data-structures";
import { assertEquals } from "@std/assert";

const values = [3, 10, 13, 4, 6, 7, 1, 14];
const tree = new BinarySearchTree<number>();
values.forEach((value) => tree.insert(value));

assertEquals([...tree], [1, 3, 4, 6, 7, 10, 13, 14]);
assertEquals(tree.min(), 1);
assertEquals(tree.max(), 14);
assertEquals(tree.find(42), null);
assertEquals(tree.find(7), 7);
assertEquals(tree.remove(42), false);
assertEquals(tree.remove(7), true);
assertEquals([...tree], [1, 3, 4, 6, 10, 13, 14]);

Add to your project Jump to heading

deno add jsr:@std/data-structures

See all symbols in @std/data-structures on

What “data structures” means here Jump to heading

In this package, “data structures” are purpose-built containers with clear invariants and predictable performance characteristics that go beyond JavaScript’s built-ins (Array, Map, Set). They provide capabilities like priority ordering (heaps), ordered search and iteration (trees), reversible lookups (bidirectional maps), and fixed-shape storage (2D arrays).

  • Most structures implement Iterable, so you can spread or for..of them.
  • Many accept a comparator (for example, ascend/descend) to control order.

When to use what Jump to heading

  • BinaryHeap (priority queue): schedule work, pick top-k items, Dijkstra/Best‑First search. Default is a max‑heap; supply a custom comparator to change priority.
  • RedBlackTree (self‑balancing BST): maintain a sorted set of values with fast min/max and in‑order iteration even under adversarial input.
  • BinarySearchTree (unbalanced): simpler tree for mostly random inputs or educational use; supports traversal orders and min/max.
  • BidirectionalMap (unstable): two‑way lookups when both keys and values are unique (for example, ID ↔ code).
  • D2Array (unstable): fixed‑size 2D grid (boards, matrices) with O(1) access.

Examples Jump to heading

import { BinaryHeap } from "@std/data-structures";

// Priority queue (max-heap by default)
const pq = BinaryHeap.from([5, 1, 3]);
pq.pop(); // 5

// Custom priority: shortest string first
const shortestFirst = new BinaryHeap<string>((a, b) => b.length - a.length);
shortestFirst.push("bbb", "a", "cc");
shortestFirst.pop(); // "a"
import { ascend, RedBlackTree } from "@std/data-structures";

const t = RedBlackTree.from([5, 1, 9], ascend<number>);
[...t]; // [1, 5, 9]
t.min(); // 1
t.max(); // 9
// Unstable submodules
import { BidirectionalMap } from "@std/data-structures/unstable-bidirectional-map";
import { D2Array } from "@std/data-structures/unstable-2d-array";

const bimap = new BidirectionalMap<string, number>();
bimap.set("alice", 1);
bimap.getReverse(1); // "alice"

const grid = new D2Array<number>(3, 2, 0); // width=3, height=2, initial=0
grid.resize(4, 2);
grid.insert(1, 1, 42);
grid.get(1, 1); // 42

Did you find what you needed?

Privacy policy