You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Readme.md
+58-57Lines changed: 58 additions & 57 deletions
Original file line number
Diff line number
Diff line change
@@ -1,25 +1,24 @@
1
-
# Fast splay tree [](https://badge.fury.io/js/splaytree)[](https://travis-ci.org/w8r/splay-tree)[](https://codecov.io/gh/w8r/splay-tree)
1
+
# Fast splay tree [](https://badge.fury.io/js/splaytree)[](https://codecov.io/gh/w8r/splay-tree)
2
2
3
3
[Splay-tree](https://en.wikipedia.org/wiki/Splay_tree): **[fast](#benchmarks)**(non-recursive) and **simple**(< 1000 lines of code)
4
4
Implementation is adapted directly from Wikipedia with the same API as [w8r/avl](https://github.com/w8r/avl), to run the benchmarks against other trees.
5
5
6
-
7
6
This tree is based on **top-down** splaying algorithm by D.Sleator. It supports
8
-
- splitting, merging
9
-
- updating of the keys
10
-
- bulk loading of the items into an empty or non-empty tree
*`tree.keys():Array<key>` - Returns the array of keys in order
59
-
*`tree.values():Array<*>` - Returns the array of data fields in order
60
-
*`tree.range(lo, high, function(node) {} [, context]):Tree` - Walks the range of keys in order. Stops, if the visitor function returns a non-zero value.
61
-
*`tree.pop():Node` - Removes smallest node
62
-
*`tree.min():key` - Returns min key
63
-
*`tree.max():key` - Returns max key
64
-
*`tree.minNode():Node` - Returns the node with smallest key
65
-
*`tree.maxNode():Node` - Returns the node with highest key
66
-
*`tree.prev(node):Node` - Predecessor node
67
-
*`tree.next(node):Node` - Successor node
68
-
*`tree.load(keys:Array<*>, [values:Array<*>][,presort=false]):Tree` - Bulk-load items. It expects values and keys to be sorted, but if `presort` is `true`, it will sort keys and values using the comparator(in-place, your arrays are going to be altered).
50
+
-`new SplayTree([comparator])`, where `comparator` is optional comparison function
-`tree.keys():Array<key>` - Returns the array of keys in order
60
+
-`tree.values():Array<*>` - Returns the array of data fields in order
61
+
-`tree.range(lo, high, function(node) {} [, context]):Tree` - Walks the range of keys in order. Stops, if the visitor function returns a non-zero value.
62
+
-`tree.pop():Node` - Removes smallest node
63
+
-`tree.min():key` - Returns min key
64
+
-`tree.max():key` - Returns max key
65
+
-`tree.minNode():Node` - Returns the node with smallest key
66
+
-`tree.maxNode():Node` - Returns the node with highest key
67
+
-`tree.prev(node):Node` - Predecessor node
68
+
-`tree.next(node):Node` - Successor node
69
+
-`tree.load(keys:Array<*>, [values:Array<*>][,presort=false]):Tree` - Bulk-load items. It expects values and keys to be sorted, but if `presort` is `true`, it will sort keys and values using the comparator(in-place, your arrays are going to be altered).
69
70
70
71
**Comparator**
71
72
72
73
`function(a:key,b:key):Number` - Comparator function between two keys, it returns
73
-
*`0` if the keys are equal
74
-
*`<0` if `a < b`
75
-
*`>0` if `a > b`
76
74
77
-
The comparator function is extremely important, in case of errors you might end
78
-
up with a wrongly constructed tree or would not be able to retrieve your items.
79
-
It is crucial to test the return values of your `comparator(a,b)` and `comparator(b,a)`
80
-
to make sure it's working correctly, otherwise you may have bugs that are very
81
-
unpredictable and hard to catch.
75
+
-`0` if the keys are equal
76
+
-`<0` if `a < b`
77
+
-`>0` if `a > b`
78
+
79
+
The comparator function is extremely important, in case of errors you might end
80
+
up with a wrongly constructed tree or would not be able to retrieve your items.
81
+
It is crucial to test the return values of your `comparator(a,b)` and `comparator(b,a)`
82
+
to make sure it's working correctly, otherwise you may have bugs that are very
83
+
unpredictable and hard to catch.
82
84
83
-
**Duplicate keys**
85
+
**Duplicate keys**
84
86
85
-
*`insert()` method allows duplicate keys. This can be useful in certain applications (example: overlapping
86
-
points in 2D).
87
-
*`add()` method will not allow duplicate keys - if key is already present in the tree, no new node is created
87
+
-`insert()` method allows duplicate keys. This can be useful in certain applications (example: overlapping
88
+
points in 2D).
89
+
-`add()` method will not allow duplicate keys - if key is already present in the tree, no new node is created
0 commit comments