Socket
Socket
Sign inDemoInstall

binary-search-tree-js

Package Overview
Dependencies
0
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    binary-search-tree-js

Binary Search Tree


Version published
Maintainers
1
Install size
4.26 kB
Created

Readme

Source

Binary Search Tree

Binary Search Tree

Install

$ npm install --save binary-search-tree-js 

Usage

var Node = require("tree-node-js")
var BST = require("binary-search-tree-js")

var node1 = Node(22)
var tree = BST(node1)

var node1 = Node(10)
var node2 = Node(2)
tree.insert(node1)
tree.insert(node2)

tree.inorder(node1, function(node){
  console.log(node)
})

API

Initialize

BST(rootNode)

NameTypeDescription
rootNodeNodeA node we want to assign as the root node

Returns: BST instance

var Node = require("tree-node-js")
var BST = require("binary-search-tree-js")

var node1 = Node(22)
var tree = BST(node1)

Instance Methods

insert(node)

NameTypeDescription
nodeNodeA node we want to insert into the BST

Returns: undefined, that was inserted

var Node = require("tree-node-js")
var BST = require("binary-search-tree-js")

var node1 = Node(22)
var tree = BST(node1)

var node1 = Node(10)
var node2 = Node(2)
tree.insert(node1)
tree.insert(node2)

kthSmallest(k)

NameTypeDescription
kNumberRepresent the Kth element you want

Returns: Node, that represents the node that is the k smallest

var Node = require("tree-node-js")
var BST = require("binary-search-tree-js")

var node1 = Node(22)
var tree = BST(node1)
var node1 = Node(10)
var node2 = Node(2)
tree.insert(node1)
tree.insert(node2)

console.log(tree.kthSmallest(2)) // {value: 10, ....}

lca(n1, n2)

NameTypeDescription
n1 & n2NumberNubmers representing the data values of the nodes that you want to find the common ancestor for.

Returns: Node, that represents the node that is the lowest common ancestor

var Node = require("tree-node-js")
var BST = require("binary-search-tree-js")

var node1 = Node(22)
var tree = BST(node1)
var node1 = Node(10)
var node2 = Node(2)
tree.insert(node1)
tree.insert(node2)

console.log(tree.lca(2, 10)) // {value: 10, ....}

FAQs

Last updated on 09 Dec 2015

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc