Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mikolalysenko/cartesian-tree
Linear time Cartesian tree data structure construction
https://github.com/mikolalysenko/cartesian-tree
Last synced: about 2 months ago
JSON representation
Linear time Cartesian tree data structure construction
- Host: GitHub
- URL: https://github.com/mikolalysenko/cartesian-tree
- Owner: mikolalysenko
- License: mit
- Created: 2014-03-28T20:36:50.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2014-03-28T20:38:01.000Z (over 10 years ago)
- Last Synced: 2024-04-28T08:46:46.390Z (8 months ago)
- Language: JavaScript
- Size: 121 KB
- Stars: 9
- Watchers: 6
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
cartesian-tree
==============
Constructs a [Cartesian tree](http://en.wikipedia.org/wiki/Cartesian_tree) for an array in linear time.# Example
```javascript
var createCartesianTree = require("cartesian-tree")
var util = require("util")var array = [9, 3, 7, 1, 8, 12, 10, 20, 15, 18, 5]
var tree = createCartesianTree(array)
console.log(util.inspect(tree.root, {depth: 10}))
```Output:
```javascript
{ value: 1,
index: 3,
left:
{ value: 3,
index: 1,
left: { value: 9, index: 0 },
right: { value: 7, index: 2 } },
right:
{ value: 5,
index: 10,
left:
{ value: 8,
index: 4,
right:
{ value: 10,
index: 6,
left: { value: 12, index: 5 },
right:
{ value: 15,
index: 8,
left: { value: 20, index: 7 },
right: { value: 18, index: 9 } } } } } }
```# Install
```
npm install cartesian-tree
```# API
### `require("cartesian-tree")(array[,compare])`
Creates a Cartesian tree from the given array* `array` is a JavaScript array
* `compare` is an optional comparison function for ranking the elements in the tree**Returns** An object containing two properties:
* `root` which is the root node of the Cartesian tree
* `nodes` which is an array of length `array.length` where the `i`th entry corresponds the Cartesian tree node associated to the `i`th entry in `array`Each node in the tree has the following properties:
* `value` which is the value of the node
* `index` which is its occurence in `array`
* `left` which is a reference to the left child
* `right` which is a reference to the right child# Credits
(c) 2014 Mikola Lysenko. MIT License