4
4
implementations provided by [ @vadimg ] ( https://github.com/vadimg/js_bintrees )
5
5
and and mixes in features of CanJS that make the data structures observable.
6
6
7
- Note: Currently, the only data structure in the package that is observable is
8
- the ` can.RBTreeList ` , which was adapted from the ` can.RBTree ` data structure
9
- for use in CanJS' [ can-derive plugin] ( https://github.com/canjs/can-derive ) .
10
-
11
7
> - [ Install] ( #install )
12
8
> - [ Use] ( #use )
13
9
> - [ Data Structures] ( #data-structures )
@@ -49,41 +45,33 @@ npm install can-binarytree --save
49
45
Use ` require ` in Node/Browserify workflows to import ` can-binarytree ` like:
50
46
51
47
``` js
52
- var binaryTree = require (' can-binarytree' );
48
+ var can = require (' can' );
49
+ require (' can-binarytree' );
53
50
```
54
51
55
52
Use ` define ` , ` require ` or ` import ` in [ StealJS] ( http://stealjs.com )
56
53
workflows to import ` can-binarytree ` like:
57
54
58
55
``` js
59
- import binaryTree from ' can-binarytree'
56
+ import can from ' can' ;
57
+ import ' can-binarytree' ;
60
58
```
61
59
62
60
Once you've imported ` can-binarytree ` into your project, use it to
63
61
create observable binary tree data structures. The following example
64
- formats and ` console.log ` 's the current list of values as they are
65
- added to a Red-Black Tree List :
62
+ ` console.log ` 's some metadata about each node that's added to a
63
+ ` can.RBTreeList ` :
66
64
67
65
``` js
68
66
var tree = new can.RBTreeList ();
69
67
70
- tree .bind (' add' , function () {
71
- tree .print (function (node ) {
72
- return ' <' + node .data + ' >' ;
73
- });
68
+ tree .bind (' add' , function (ev , nodes ) {
69
+ console .log (node[0 ]);
74
70
})
75
71
76
- tree .push (' Hop' );
77
- // <Hop>
78
-
79
- tree .push (' Skip' );
80
- // <Hop>------
81
- // -----<Skip>
82
-
83
- tree .push (' Jump' );
84
- // -----<Skip>------
85
- // <Hop>------<Jump>
86
-
72
+ tree .push (' Hop' ); // Node {id: 0, parent: null, data: "Hop", left: null, right: null...}
73
+ tree .push (' Skip' ); // Node {id: 2, parent: Node, data: "Skip", left: null, right: null...}
74
+ tree .push (' Jump' ); // Node {id: 4, parent: Node, data: "Jump", left: null, right: null...}
87
75
```
88
76
89
77
@@ -94,12 +82,18 @@ tree.push('Jump');
94
82
- RBTree - A self-balancing binary tree that serves as a key-value store
95
83
- BinTree - A binary tree that is not balanced
96
84
85
+ Note: Currently, the only data structure in the package that is observable is
86
+ the ` can.RBTreeList ` .
87
+
97
88
98
89
## API
99
90
100
91
101
92
### can.RBTreeList
102
93
94
+ A red-black tree that uses a numeric "index" as the "key" in the same way
95
+ a ` can.List ` or native Javascript array might, yet still performs insert and
96
+ remove operations in ` O(log n) ` time.
103
97
104
98
#### .attr()
105
99
0 commit comments