binary-tree-neighbors

Utility to find Binary Tree Neighbors

Usage no npm install needed!

<script type="module">
  import binaryTreeNeighbors from 'https://cdn.skypack.dev/binary-tree-neighbors';
</script>

README

Build Status

binary-tree-neighbors

Label nodes of a binary tree with nearest right-hand neighbor

Given a structure like the following:

         O
        / \
       O   O
      /   / \
     O   O   O
    /       / \
   O       O   O
  / \
 O   O

The program will label the tree with the nearest right-hand neighbor as follows:

         O-->null
        / \
       O-->O-->null
      /   / \
     O-->O-->O-->null
    /       / \
   O------>O-->O-->null
  / \
 O-->O-->null

Installation

npm install binary-tree-neighbors --save

Usage

Example

    0
   /
  0
 /
0

Code


var traverse = require('binary-tree-neighbors');

var root = {
  neighbor: null,
  children: [
          {
          children: [
                          null,
                          null
                    ]
        },
        null
      ]
}

console.log("Before\n");

console.log(JSON.stringify(root));

traverse(root, null);

console.log("After\n");

console.log(JSON.stringify(root));

Output


Before

{
    "children": [
        {
            "children": [
                null,
                null
            ]
        },
        null
    ],
    "neighbor": null
}

After

{
    "children": [
        {
            "children": [
                null,
                null
            ],
            "neighbor": null
        },
        null
    ],
    "neighbor": null
}

License

MIT

Copyright (c) 2015 Claudio Procida