{"name":"flat-tree","version":"1.13.0","description":"A series of functions to map a binary tree to a list","main":"index.js","dependencies":{},"devDependencies":{"brittle":"^3.3.2","standard":"^17.1.0"},"scripts":{"test":"standard && brittle test.js"},"repository":{"type":"git","url":"git+https://github.com/mafintosh/flat-tree.git"},"author":{"name":"Mathias Buus","url":"@mafintosh"},"license":"MIT","bugs":{"url":"https://github.com/mafintosh/flat-tree/issues"},"homepage":"https://github.com/mafintosh/flat-tree","_id":"flat-tree@1.13.0","gitHead":"b480dc7768c2b35d2b983439ec314d585dd10098","_nodeVersion":"22.16.0","_npmVersion":"10.9.2","dist":{"integrity":"sha512-fT3HIuCPwHhFgJ20QYzDHgUG0zMmFg5cHvFiFo5h+QMSJ28TihsEVY0f8HGliuO+pOzmvjMx1odToeaEWkTnyQ==","shasum":"31f9135e583af3e7ed849a956c5284ffe33d57db","tarball":"http://registry-npm.ywwl.com/flat-tree/download/flat-tree-1.13.0.tgz","fileCount":4,"unpackedSize":14098,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCIQDcoyc52NhBGBpd4Zexp9QOORlQ1qooi/Yw/ZxC4OAaYwIgTpro6W4K0B3DpF9CeuxXtr3cLQr2LUV+9XWMoP9z5Gg="}],"size":4116,"noattachment":false,"key":"/flat-tree/-/flat-tree-1.13.0.tgz"},"_npmUser":{"name":"mafintosh","email":"mathiasbuus@gmail.com"},"directories":{},"maintainers":[{"name":"mafintosh","email":"mathiasbuus@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/flat-tree_1.13.0_1757687826315_0.7096065226003923"},"_hasShrinkwrap":false,"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":1757687826485,"_cnpm_publish_time":1757687826485,"dist-tags":{"latest":"1.13.0"}}