https://github.com/wdalmut/radix-tree
An example of radix tree (not space optimized)
https://github.com/wdalmut/radix-tree
javascript radix-tree suggestions
Last synced: 3 months ago
JSON representation
An example of radix tree (not space optimized)
- Host: GitHub
- URL: https://github.com/wdalmut/radix-tree
- Owner: wdalmut
- Created: 2018-09-08T07:27:26.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2018-09-08T11:34:32.000Z (about 7 years ago)
- Last Synced: 2025-03-15T13:41:41.259Z (7 months ago)
- Topics: javascript, radix-tree, suggestions
- Language: JavaScript
- Size: 6.84 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Radix Tree kata
A simple (not space optimized) radix tree example
```js
const { add, get, del, keys } = require('./src');
```## Add elements
```js
let root = {};
root = add('test', 'value', root);
```## Get existing elements
```js
get('test', root);
'value'
```## Remove existing elements
```js
root = del('test', root);
```## Get all keys of a prefix (autocomplete)
```js
keys('te', root)
console.log(suggests)
['test', 'testa', 'tester']
```## Create extra features
Prepare a set of functional helpers
```js
const curry2 = fn => a => b => fn(a,b)
const map = (fn, data) => data.map(fn)
const nthArg = (pos) => (...args) => args[pos]
const compose = (g, f) => (...args) => g(f.apply(null, args))
const flip = (fn) => (...args) => fn.apply(fn, args.slice().reverse())
const assoc = (key, value, data) => Object.assign({}, {[key]:value}, data)
const converge = (fn, paths) => (...args) => fn.apply(null, paths.map((fn) => fn.apply(null, args)))
const zipObj = (keys, values) => keys.reduce((memo, item, pos) => assoc(item, values[pos], memo), {})
```### Get all existing keys
```js
const allKeys = curry2(keys)('');allKeys(root);
['test', 'testa']
```### Get all values for a given prefix
```js
const values = converge(map, [compose(curry2(flip(get)), nthArg(1)), keys])values('te', root)
['value', 'value2']
```### Get all elements of a given prefix
```js
const all = converge(zipObj, [keys, values])all('te', root)
{ testa: 'value2', test: 'value' }
```