Git Product home page Git Product logo

flat-tree's Introduction

flat-tree

A series of functions to map a binary tree to a list

npm install flat-tree

Usage

You can represent a binary tree in a simple flat list using the following structure

      3
  1       5
0   2   4   6  ...

This module exposes a series of functions to help you build and maintain this data structure

var tree = require('flat-tree')
var list = []

var i = tree.index(0, 0) // get array index for depth: 0, offset: 0
var j = tree.index(1, 0) // get array index for depth: 1, offset: 0

// use these indexes to store some data

list[i] = 'a'
list[j] = 'b'
list[tree.parent(i)] = 'parent of a and b'

API

index = tree.index(depth, offset)

Returns an array index for the tree element at the given depth and offset.

parentIndex = tree.parent(index)

Returns the index of the parent element in tree.

siblingIndex = tree.sibling(index)

Returns the index of this elements sibling.

children = tree.children(index)

Returns an array [leftChild, rightChild] with the indexes of this elements children. If this element does not have any children it returns null.

range = tree.spans(index)

Returns the range (inclusive) the tree root at index spans. For example tree.spans(3) would return [0, 6] (see the usage example).

index = tree.leftSpan(index)

Returns the left spanning in index in the tree index spans.

index = tree.rightSpan(index)

Returns the right spanning in index in the tree index spans.

count = tree.count(index)

Returns how many nodes (including parent nodes) a tree contains.

count = tree.countLeaves(index)

Returns how many nodes (excluding parent nodes) a tree contains.

depth = tree.depth(index)

Returns the depth of an element.

offset = tree.offset(index, [depth])

Returns the relative offset of an element.

roots = tree.fullRoots(index)

Returns a list of all the full roots (subtrees where all nodes have either 2 or 0 children) < index. For example fullRoots(8) returns [3] since the subtree rooted at 3 spans 0 -> 6 and the tree rooted at 7 has a child located at 9 which is >= 8.

iterator = tree.iterator(index)

Create a stateful tree iterator starting at a given index. The iterator exposes the following methods.

index = iterator.next()

Move the iterator the next item in the tree.

index = iterator.prev()

Move the iterator the prev item in the tree.

iterator.seek(index)

Move the iterator the this specific tree index.

index = iterator.parent()

Move the iterator to the current parent index.

index = iterator.leftChild()

Move the iterator to the current left child index.

index = iterator.rightChild()

Move the iterator to the current right child index.

index = iterator.leftSpan()

Move the iterator to the current left span index.

index = iterator.rightSpan()

Move the iterator to the current right span index.

bool = iterator.isLeft()

Is the iterator at a left sibling?

bool = iterator.isRight()

Is the iterator at a right sibling?

index = iterator.sibling()

Move the iterator to the current sibling.

count = iterator.count()

Returns how many nodes (including parent nodes) the current tree contains.

count = iterator.countLeaves()

Returns how many nodes (excluding parent nodes) the current tree contains.

See also

License

MIT

flat-tree's People

Contributors

bcomnes avatar dukeraphaelng avatar jwerle avatar kasperisager avatar mafintosh avatar noffle avatar ralphtheninja avatar yoshuawuyts avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

flat-tree's Issues

C impl

Hi there!
This is bin numbering as per 7574, right?
I have a header-only C impl. Interested?

Use `id` instead of `index`

It seems to me that the data referred to as index is not really an index. At least it's not the node index when building the merkle-tree. It's a bit confusing when the nodes are stored in a flat data structure (e.g., a list).

When building the merkle-tree, there is an inherent ordering which is the order in which each hash is computed (using this order requires the least amount of memory to build the merkle-tree):

  1. first data block is received, its hash is computed (A)
  2. second data block is received, its hash is computed (B)
  3. hash of (AB) is computed (C)
  4. third data block is received, hashes computed (D)
  5. fourth data block is received, hashes computed (E)
  6. hash of (DE) is computed (F)
  7. hash of (CF) is computed (G)
    ...

The index for the above hashes in the flat-tree are the following:

  • A: 0
  • B: 2
  • C: 1
  • D: 4
  • E: 6
  • F: 5
  • G: 3

Maybe there is a reason for using index to refer to these nodes, but I could not find one. So, if there is no specific reason for this name, I propose to use id or maybe position instead.

(Same issue in datrs: datrs/flat-tree#30)

fullRoots and passing in results array

@mafintosh I noticed that fullRoots takes an optional array as second argument, but I can't find any usage of this in any dependent module. This was added two years ago. It's also not documented.

Mind if I clean that up?

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.