Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mikolalysenko/bfs-tree-layout
Operations for trees stored in BFS layout
https://github.com/mikolalysenko/bfs-tree-layout
Last synced: 3 months ago
JSON representation
Operations for trees stored in BFS layout
- Host: GitHub
- URL: https://github.com/mikolalysenko/bfs-tree-layout
- Owner: mikolalysenko
- License: mit
- Created: 2013-04-12T05:10:00.000Z (almost 12 years ago)
- Default Branch: master
- Last Pushed: 2013-05-14T00:11:06.000Z (over 11 years ago)
- Last Synced: 2024-04-25T06:01:57.471Z (9 months ago)
- Language: JavaScript
- Size: 133 KB
- Stars: 5
- Watchers: 3
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
bfs-tree-layout
===================
Operations on nodes for balanced binary trees stored in bfs layout. These are useful if you are building data structures, like binary search trees, implicitly (ie not storing pointers to subtrees).BFS layout, unlike inorder layout, is much faster for performing binary searches and bounds queries. The disadvantage is that it is much slower at inorder traversal.
## Install
npm install bfs-tree-layout
## ExampleAssume that the tree is filled in level order, and laid out in memory via an bfs traversal. For example:
```
The tree:0
/ \
1 2
/ \ / \
3 4 5 6
/ \ |
7 8 9
```With this picture in mind, here are some operations:
```javascript
var layout = require("bfs-tree-layout")console.log(layout.left(10, 3)) //Prints: 7
console.log(layout.parent(10, 4)) //Prints: 1
console.log(layout.height(10, 2)) //Prints: 1
```
## API
```javascript
var layout = require("bfs-tree-layout")
```**Conventions:**
* `n` is always the size of the tree
* `x` is the index of a node in the tree### `layout.root(n)`
Returns the index of the root of a tree of size n.### `layout.begin(n)`
Returns the index of the first node of the tree### `layout.end(n)`
Returns the index of the last node in the tree### `layout.height(n, x)`
Returns the height of node `x` in a tree of size `n`### `layout.prev(n, x)`
Returns the predecessor of `x` in an in-order traversal### `layout.next(n, x)`
Returns the successor of `x` in an in-order traversal### `layout.parent(n, x)`
Returns the parent of `x` in a tree of size `n`### `layout.left(n, x)`
Returns the left child of `x`### `layout.right(n, x)`
Returns the right child of `x`### `layout.leaf(n, x)`
Returns true if the node x is a leaf### `layout.lo(n, x)`
Returns the left-most ancestor of x### `layout.hi(n, x)`
Returns the right-most ancestor of x# Credits
(c) 2013 Mikola Lysenko. MIT License