Coder Social home page Coder Social logo

adamshaylor / okasaki-rb-tree-js Goto Github PK

View Code? Open in Web Editor NEW
0.0 3.0 0.0 136 KB

An implementation of the Okasaki red-black tree in JavaScript

License: ISC License

JavaScript 97.04% HTML 2.96%
red-black-tree functional-data-structure functional-javascript immutable binary-search-tree

okasaki-rb-tree-js's Introduction

okasaki-rb-tree

An implementation of the Okasaki red-black tree in JavaScript

Overview

In 1999, Chris Okasaki wrote a research paper called functional pearls: red-black trees in a functional setting. In it, he demonstrated a functional, immutable way to implement a red-black tree in Haskell.

This is an implementation of Okasaki’s work—specifically his rotations—in JavaScript. The names of the rotations in the library are based on an in-order traversal of the the imbalanced subtrees described in figure 1 on page 3.

All the data structures generated in this library are done so with Object.create(). Although JavaScript will let you add your own properties to them, its own are immutable.

Example

import createTree from 'okasaki-rb-tree';
const emptyTree = createTree();
const threeTree = emptyTree.insert(3);
emptyTree.root; // null
threeTree.root; // {color: "black", value: 3, left: null, right: null}

API

createTree(options)

  • options.root: the root node of the tree (default: null).
  • options.aEqualsB: the equality function used for searches and insertions, e.g. for case-insensitivity (default: (a, b) => a === b).
  • options.aIsLessThanB: the comparator function used for searches and insertions (default: (a, b) => a < b).

tree

  • tree.root: node or null.
  • tree.aEqualsB: see createTree(options).
  • tree.aIsLessThanB: see createTree(options).
  • tree.search(value): returns the value if found, null if not.
  • tree.insert(value): returns a new, rebalanced tree with the new value. NB: tree instances are immutable; the original tree will remain unchanged.
  • tree.insertBatch(values): pass an array and it returns a new, rebalanced tree with its values.
  • tree.make(properties): returns a new, rebalanced tree with properties merged with the original.

node

  • node.color: "red" or "black".
  • node.value: an arbitrary insert-ed value.
  • node.leftChild: node or null.
  • node.rightChild: node or null.
  • node.make(properties): returns a new, rebalanced subtree with properties merged with the original.

Todo

  • Write unit tests for node.
  • Write unit tests for rotations.
  • Switch extensions from .js to .mjs to support node’s experimental module mode once Jest is ready.
  • Plan how to handle falsey insertion values. (Disallow or configure?)
  • Add tree.remove(). (Okasaki’s original paper didn’t include one.)
  • Add Rollup to convert to CommonJS?

okasaki-rb-tree-js's People

Contributors

adamshaylor avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.