node package manager
Don’t reinvent the wheel. Reuse code within your team. Create a free org »




npm install binary-tree git clone


node = [address, key, left, right]

Address is a string or a number

address is Address

key is a number

left and right is Address or null

if left or right is a null then right subtree is empty

handler is a function

direction indicates the direction, 1 indicates to the left, 2 indicates to the right

duplicating rule indicates duplicating of nodes, 0 disables duplication, 1 includes left, 2 includes right

merging rule indicates merging of nodes, works as direction

debug is a boolean, indicates throw any errors

Handler variants

(null, null) -> not found

(node, null) -> last node

(node, next) -> not last node

the last returned node - the target node



BinaryTree = require 'binary-tree'


require ['binary-tree'], (BinaryTree) ->


it gives full modularity

getNode = (address, callback) -> callback node # return the node address - address
setLeft = (address, leftLink, callback) ->
    # set at the left sub-node with address - address
    callback node # return the set via the node as getNode
setRight = (address, rightLink, callback) ->
    # set at the right sub-node with address - address
    callback node # return the set via the node as getNode
setNode = (address, leftLink, rightLink, callback) ->
    # set both the left and right subtree of a node with address - address
    callback node # return the set via the node as getNode
getRoot = (callback) -> callback root
setRoot = (link, callback) ->
    # use the link on the root
    callback link

new BinaryTree getNode, setLeft, setRight, setNode, getRoot, setRoot, duplicating, merging, debug

an example can be seen in the test file or Tests.js

to run the tests, you can try vows --spec in console

if you use grunt in console, the main file and the test file will be compiled automatically

Instance node, (node, next) -> next node instance.unsafe.corner node source, direction, (node, next) -> do next instance.unsafe.merge node left, node right, (node, next) -> do next key target, (node, next) -> do next node target, (node, next) -> do next handler node source, node target, (node, next) -> do next instance.attach key target, (node, next) -> do next node target, (node, next) -> do next instance.unsafe.attach node source, node target, (node, next) -> do next instance.detach address target, (node, next) -> do next node target, (node, next) -> do next instance.unsafe.detach node source, node target, (node, next) -> do next



Fork Van Civelik.

Require.js support.


Improved documentation.


In the constructor method is added setNode, immediately setting the left and right subtree, for increased productivity.


Written including, possibly, all behaviors.

Written library performs tests.

The library can be used as a node.js module.

Custom methods passed to the constructor transferred to the sub-object instance.user.