Giter VIP home page Giter VIP logo

binary-search-tree's Introduction

Binary Search Tree

An implementation of a binary search tree API in JavaScript

Install

npm install @vanillas/binary-search-tree

Usage

Given a tree strucutre which looks like this:

  100
  / \
 20 500
 / \
10 30
    \
    40

Create the root node and insert several child nodes:

const createNode = require("@vanillas/binary-search-tree")

const rootNode = createNode(100)

rootNode
  .insert(20)
  .insert(500)

rootNode
  .left
  .insert(10)
  .insert(30)

rootNode
  .left
  .right
  .insert(40)

Now you can search easily:

rootNode.search(40)

// {
//   value: 40,
//   left: undefined,
//   right: undefined
// }

rootNode.search(20)

// {
//   value: 20,
//   left: {
//     value: [Getter],
//     left: [Getter],
//     right: [Getter],
//     toList: [Function: toList],
//     isValid: [Function: isValid],
//     insert: [Function: insert],
//     search: [Function: search]
//   },
//   right: {
//     value: [Getter],
//     left: [Getter],
//     right: [Getter],
//     toList: [Function: toList],
//     isValid: [Function: isValid],
//     insert: [Function: insert],
//     search: [Function: search]
//   }
// }

This achieves a hierarchically layered tree which looks like this:

rootNode.toList()

// [
//   [100],
//   [20, 500],
//   [10, 30],
//   [40]
// ]

Alternatively, you can utilize the optional 2nd param to the .insert() method to chain onto that new node you're inserting:

const createNode = require("@vanillas/binary-search-tree")

const rootNode = createNode(100)
  .insert(20, n20 => n20
      .insert(10)
      .insert(30, n30 => n30.insert(40))
  )
  .insert(500)

And while you can add nodes directly to the exact locations you want them, you can also allow them to be placed automatically.

const rootNode = createNode(100)
    .insert(20)
    .insert(500)
    .insert(10)
    .insert(30)
    .insert(40)

Which achieves the same layering as the other two (manual) examples listed earlier:

rootNode.toList()

// [
//   [100],
//   [20, 500],
//   [10, 30],
//   [40]
// ]

TypeScript support

Also works with TypeScript:

import { createNode } from "@vanillas/binary-search-tree"

const rootNode = createNode(100)
    .insert(20, n20 => n20
        .insert(10)
        .insert(30, n30 => n30.insert(40))
    )
    .insert(500)

const result = rootNode.search(40)

binary-search-tree's People

Contributors

arizonatribe avatar

Watchers

 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.