{"name":"flat-tree","version":"1.4.0","author":{"url":"@mafintosh","name":"Mathias Buus"},"license":"MIT","_id":"flat-tree@1.4.0","homepage":"https://github.com/mafintosh/flat-tree","bugs":{"url":"https://github.com/mafintosh/flat-tree/issues"},"dist":{"shasum":"d976551e829b50f42a5213c913ccf9d81935181d","tarball":"http://registry-npm.ywwl.com/flat-tree/download/flat-tree-1.4.0.tgz","integrity":"sha512-rEDTk90PKCcj37+e2dDUYaasFA4PJ+8j//7PhelApHj12sBVThAZqcHNOALLeoPVjmkeXC9uX6GnAnXMS+bWHw==","signatures":[{"sig":"MEUCICVK0v43NI0SD1wH6c7GoT8mT12I46wPCfEjQU2NoV+4AiEAuMIRM689QGPQNHW7ptPc6YTlXvsqLJjXfS5+SbL0AK4=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"size":3215,"noattachment":false,"key":"/flat-tree/-/flat-tree-1.4.0.tgz"},"main":"index.js","_from":".","_shasum":"d976551e829b50f42a5213c913ccf9d81935181d","gitHead":"ca54a6f05cccbaa240729a38107226cda4dfddf8","scripts":{"test":"tape test.js"},"_npmUser":{"name":"mafintosh","email":"mathiasbuus@gmail.com"},"repository":{"url":"https://github.com/mafintosh/flat-tree.git","type":"git"},"_npmVersion":"2.14.12","description":"A series of functions to map a binary tree to a list","directories":{},"_nodeVersion":"4.3.1","dependencies":{},"devDependencies":{"tape":"^4.0.0"},"_npmOperationalInternal":{"tmp":"tmp/flat-tree-1.4.0.tgz_1457378219839_0.5223089377395809","host":"packages-12-west.internal.npmjs.com"},"readme":"# flat-tree\n\nA series of functions to map a binary tree to a list\n\n```\nnpm install flat-tree\n```\n\n## Usage\n\nYou can represent a binary tree in a simple flat list using the following structure\n\n```\n      3\n  1       5\n0   2   4   6  ...\n```\n\nThis module exposes a series of functions to help you build and maintain this data structure\n\n``` js\nvar tree = require('flat-tree')\nvar list = []\n\nvar i = tree.index(0, 0) // get array index for depth: 0, offset: 0\nvar j = tree.index(1, 0) // get array index for depth: 1, offset: 0\n\n// use these indexes to store some data\n\nlist[i] = 'a'\nlist[j] = 'b'\nlist[tree.parent(i)] = 'parent of a and b'\n```\n\n## API\n\n#### `index = tree.index(depth, offset)`\n\nReturns an array index for the tree element at the given depth and offset.\n\n#### `parentIndex = tree.parent(index)`\n\nReturns the index of the parent element in tree.\n\n#### `siblingIndex = tree.sibling(index)`\n\nReturns the index of this elements sibling.\n\n#### `children = tree.children(index)`\n\nReturns an array `[leftChild, rightChild]` with the indexes of this elements children.\nIf this element does not have any children it returns `null`.\n\n#### `range = tree.spans(index)`\n\nReturns the range (inclusive) the tree root at `index` spans.\nFor example `tree.spans(3)` would return `[0, 6]` (see the usage example).\n\n#### `index = tree.leftSpan(index)`\n\nReturns the left spanning in index in the tree `index` spans.\n\n#### `index = tree.rightSpan(index)`\n\nReturns the right spanning in index in the tree `index` spans.\n\n#### `count = tree.count(index)`\n\nReturns how many nodes (including parent nodes) a tree contains.\n\n#### `count = tree.countLeaves(index)`\n\nReturns how many nodes (excluding parent nodes) a tree contains.\n\n#### `depth = tree.depth(index)`\n\nReturns the depth of an element.\n\n#### `offset = tree.offset(index, [depth])`\n\nReturns the relative offset of an element.\n\n#### `roots = tree.fullRoots(index)`\n\nReturns a list of all the full roots (subtrees where all nodes have either 2 or 0 children) `<` index.\nFor example `fullRoots(8)` returns `[3]` since the subtree rooted at `3` spans `0 -> 6` and the tree\nrooted at `7` has a child located at `9` which is `>= 8`.\n\n#### `iterator = tree.iterator(index)`\n\nCreate a stateful tree iterator starting at a given index.\nThe iterator exposes the following methods.\n\n#### `index = iterator.next()`\n\nMove the iterator the next item in the tree.\n\n#### `index = iterator.prev()`\n\nMove the iterator the prev item in the tree.\n\n#### `iterator.seek(index)`\n\nMove the iterator the this specific tree index.\n\n#### `index = iterator.parent()`\n\nMove the iterator to the current parent index.\n\n#### `index = iterator.leftChild()`\n\nMove the iterator to the current left child index.\n\n#### `index = iterator.rightChild()`\n\nMove the iterator to the current right child index.\n\n#### `index = iterator.leftSpan()`\n\nMove the iterator to the current left span index.\n\n#### `index = iterator.rightSpan()`\n\nMove the iterator to the current right span index.\n\n#### `bool = iterator.isLeft()`\n\nIs the iterator at a left sibling?\n\n#### `bool = iterator.isRight()`\n\nIs the iterator at a right sibling?\n\n#### `index = iterator.sibling()`\n\nMove the iterator to the current sibling.\n\n#### `count = iterator.count()`\n\nReturns how many nodes (including parent nodes) the current tree contains.\n\n#### `count = iterator.countLeaves()`\n\nReturns how many nodes (excluding parent nodes) the current tree contains.\n\n## See also\n\n- [mafintosh/print-flat-tree][print]: A cli that can pretty print flat-trees.\n- [bcomnes/flattree][ftg]: A port of the node module to Go.\n- [arablocks/flat-tree.c](c): A port of the module to C\n- [datkt/flat-tree][kt]: A port of the module to Kotlin\n- [datrs/flat-tree][rs]: A port to Rust.\n- [bcomnes/flattree][ftg]: A port to Go.\n- [noffle/cl-flat-tree][clft]: A port to Common Lisp.\n- [dukeraphaelng/flat_tree][cr]: A port to Crystal.\n\n## License\n\nMIT\n\n[print]: https://github.com/mafintosh/print-flat-tree\n[rs]: https://github.com/datrs/flat-tree\n[ftg]: https://github.com/bcomnes/flattree\n[c]: https://github.com/arablocks/flat-tree.c\n[kt]: https://github.com/datkt/flat-tree\n[clft]: https://github.com/noffle/cl-flat-tree\n[cr]: https://github.com/dukeraphaelng/flat_tree\n","publish_time":1457378221886,"maintainers":[{"name":"mafintosh","email":"mathiasbuus@gmail.com"}],"_cnpm_publish_time":1457378221886,"dist-tags":{"latest":"1.13.0"}}