GithubHelp home page GithubHelp logo

make-github-pseudonymous-again / js-bst Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 2.39 MB

:seedling: Binary search tree library in JavaScript

License: GNU Affero General Public License v3.0

JavaScript 100.00%
javascript agpl binary-search-trees searching search-trees computer-science sorting data-structures tree

js-bst's Introduction

Binary search tree data structures for JavaScript. See docs. Parent is @aureooms/js-data-structures.

Description

This README regroups projects focusing on implementing search tree data structures with JavaScript. This project itself does not contain any code.

👶 Children

📜 Reference

js-bst's People

Contributors

gitter-badger avatar greenkeeperio-bot avatar make-github-pseudonymous-again avatar

Stargazers

 avatar

Watchers

 avatar

js-bst's Issues

hasBinarySearchProperty

import {isSorted} from '@aureooms/js-sort';
const hasBinarySearchProperty = (compare, tree) => {
    const array = [...tree];
    return isSorted(compare, array, 0, array.length);
};
const hasBinarySearchPropertyAndDoesNotContainDuplicates = (compare, tree) => {
    const strictCompare = (a,b) => compare(a,b) <= 0 ? -1 : 1;
    // const strictCompare = (a,b) => compare(a,b) < 0 ? -1 : 1; // ugly because the choice of `<=` vs `<` depends on the implementation of `hasBinarySearchProperty` and actually does not work in the general case
    return hasBinarySearchProperty(strictCompare, tree);
const hasBinarySearchProperty = (compare, node, left = undefined, right = undefined) => {
    if (node === null) return true;
    if (left !== undefined && compare(node.key, left) < 0) return false;
    if (right !== undefined && compare(node.key, right) > 0) return false;
    //     if (right !== undefined && compare(right, node.key) < 0) return false; // if using the `strictCompare` trick
    return hasBinarySearchProperty(compare, node.left, left, node.key) && hasBinarySearchProperty(compare, node.right, node.key, right);
};

const hasBinarySearchPropertyAndDoesNotContainDuplicates = (compare, node, left = undefined, right = undefined) => {
    if (node === null) return true;
    if (left !== undefined && compare(node.key, left) <= 0) return false;
    if (right !== undefined && compare(node.key, right) >= 0) return false;
    return hasBinarySearchProperty(compare, node.left, left, node.key) && hasBinarySearchProperty(compare, node.right, node.key, right);
};

See https://en.wikipedia.org/wiki/Binary_search_tree#Verification

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.