{"name":"flat-tree","version":"1.10.0","author":{"url":"@mafintosh","name":"Mathias Buus"},"license":"MIT","_id":"flat-tree@1.10.0","homepage":"https://github.com/mafintosh/flat-tree","bugs":{"url":"https://github.com/mafintosh/flat-tree/issues"},"dist":{"shasum":"9ea4d52593322678f80bd7ff5fdea79c8d48e452","tarball":"http://registry-npm.ywwl.com/flat-tree/download/flat-tree-1.10.0.tgz","fileCount":6,"integrity":"sha512-tbazIl4MxtOtkPRKVxripIkd0MTFBMGqEPBw1tV4tdgN8DIf/oMYRS3rkmObFw/b7zmaoWPjAbNDRz5QUgY/Ow==","signatures":[{"sig":"MEUCIGIaYi2JxNbH9iEE7Qlo5AfqQgTCpaC95p2FaEPbTxqpAiEA6CjFAfJmO9M1363yy2d1YpuJ5EH9X+nAhOav6CTXfnY=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":18997,"size":5029,"noattachment":false,"key":"/flat-tree/-/flat-tree-1.10.0.tgz"},"main":"index.js","gitHead":"6d9f395f9dd811169d592e2775a5308921ce6687","scripts":{"test":"standard && brittle test.js"},"_npmUser":{"name":"mafintosh","email":"mathiasbuus@gmail.com"},"repository":{"url":"git+https://github.com/mafintosh/flat-tree.git","type":"git"},"_npmVersion":"9.8.1","description":"A series of functions to map a binary tree to a list","directories":{},"_nodeVersion":"18.18.0","dependencies":{},"_hasShrinkwrap":false,"devDependencies":{"brittle":"^3.3.2","standard":"^17.1.0"},"_npmOperationalInternal":{"tmp":"tmp/flat-tree_1.10.0_1699097464443_0.3426848028802809","host":"s3://npm-registry-packages"},"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":1699097464670,"maintainers":[{"name":"mafintosh","email":"mathiasbuus@gmail.com"}],"_cnpm_publish_time":1699097464670,"dist-tags":{"latest":"1.13.0"}}