1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
//! Binary Tree implementations
use std::borrow::Borrow;
use std::collections::VecDeque;

use linalg::{Matrix, BaseMatrix, Vector};
use learning::error::Error;

use super::{KNearest, KNearestSearch, get_distances, dist};

/// Binary tree
#[derive(Debug)]
pub struct BinaryTree<B: BinarySplit> {
    // Binary tree leaf size
    leafsize: usize,
    // Search data
    data: Option<Matrix<f64>>,
    // Binary tree
    root: Option<Node<B>>
}

impl<B: BinarySplit> Default for BinaryTree<B> {
    /// Constructs default binary-tree (kd-tree or ball-tree) seach.
    /// Each leaf contains 30 elements at maximum.
    ///
    /// # Examples
    ///
    /// ```
    /// use rusty_machine::learning::knn::{KDTree, BallTree};
    /// let _ = KDTree::default();
    /// let _ = BallTree::default();
    /// ```
    fn default() -> Self {
        BinaryTree {
            leafsize: 30,
            data: None,
            root: None
        }
    }
}

/// Binary splittable
pub trait BinarySplit: Sized {

    /// Build branch from passed args
    fn build(data: &Matrix<f64>, remains: Vec<usize>,
             dim: usize, split: f64, min: Vector<f64>, max: Vector<f64>,
             left: Node<Self>, right: Node<Self>)
        -> Node<Self>;

    /// Return a tuple of left and right node. First node is likely to be
    /// closer to the point
    unsafe fn maybe_close<'s, 'p>(&'s self, point: &'p [f64])
        -> (&'s Node<Self>, &'s Node<Self>);

    /// Return distance between the point and myself
    fn dist(&self, point: &[f64]) -> f64;

    /// Return left node
    fn left(&self) -> &Node<Self>;
    /// Return right node
    fn right(&self) -> &Node<Self>;
}

/// Kd-tree branch
#[derive(Debug)]
pub struct KDTreeBranch {
    /// dimension (column) to split
    dim: usize,
    /// split value
    split: f64,

    /// min and max of bounding box
    /// i.e. hyper-rectangle contained in the branch
    min: Vector<f64>,
    max: Vector<f64>,

    // link to left / right node
    // - left node contains rows which the column specified with
    //   ``dim`` is less than ``split`` value.
    // - right node contains greater than or equal to ``split`` value
    left: Box<Node<KDTreeBranch>>,
    right: Box<Node<KDTreeBranch>>,
}

/// Ball-tree branch
#[derive(Debug)]
pub struct BallTreeBranch {
    /// dimension (column) to split
    dim: usize,
    /// split value
    split: f64,

    /// ball centroid and its radius
    center: Vector<f64>,
    radius: f64,

    // link to left / right node, see KDTreeBranch comment
    left: Box<Node<BallTreeBranch>>,
    right: Box<Node<BallTreeBranch>>,
}

/// Kd-tree implementation
pub type KDTree = BinaryTree<KDTreeBranch>;

/// Ball-tree implementation
pub type BallTree = BinaryTree<BallTreeBranch>;

impl BinarySplit for KDTreeBranch {

    fn build(_: &Matrix<f64>, _: Vec<usize>,
             dim: usize, split: f64, min: Vector<f64>, max: Vector<f64>,
             left: Node<Self>, right: Node<Self>) -> Node<Self> {

        let b = KDTreeBranch {
            dim: dim,
            split: split,
            min: min,
            max: max,
            left: Box::new(left),
            right: Box::new(right)
        };
        Node::Branch(b)
    }

    unsafe fn maybe_close<'s, 'p>(&'s self, point: &'p [f64])
        -> (&'s Node<Self>, &'s Node<Self>) {

        if *point.get_unchecked(self.dim) < self.split {
            (&self.left, &self.right)
        } else {
            (&self.right, &self.left)
        }
    }

    fn dist(&self, point: &[f64]) -> f64 {
        let mut d = 0.;
        for ((&p, &mi), &ma) in point.iter()
                                     .zip(self.min.iter())
                                     .zip(self.max.iter()) {
            if p < mi {
                d += (mi - p) * (mi - p);
            } else if ma < p {
                d += (ma - p) * (ma - p);
            }
            // otherwise included in the hyper-rectangle
        }
        d.sqrt()
    }

    fn left(&self) -> &Node<Self> {
        self.left.borrow()
    }

    fn right(&self) -> &Node<Self> {
        self.right.borrow()
    }
}

impl BinarySplit for BallTreeBranch {

    fn build(data: &Matrix<f64>, remains: Vec<usize>,
             dim: usize, split: f64, _: Vector<f64>, _: Vector<f64>,
             left: Node<Self>, right: Node<Self>) -> Node<Self> {

        // calculate centroid (mean)
        // TODO: cleanup using .row()
        let mut center: Vec<f64> = vec![0.; data.cols()];
        for &i in &remains {
            let row: Vec<f64> = data.select_rows(&[i]).into_vec();
            for (c, r) in center.iter_mut().zip(row.iter()) {
                *c += *r;
            }
        }
        let len = remains.len() as f64;
        for c in &mut center {
            *c /= len;
        }
        let mut radius = 0.;
        for &i in &remains {
            let row: Vec<f64> = data.select_rows(&[i]).into_vec();
            let d = dist(&center, &row);
            if d > radius {
                radius = d;
            }
        }

        let b = BallTreeBranch {
            dim: dim,
            split: split,
            center: Vector::new(center),
            radius: radius,
            left: Box::new(left),
            right: Box::new(right)
        };
        Node::Branch(b)
    }

    unsafe fn maybe_close<'s, 'p>(&'s self, point: &'p [f64])
        -> (&'s Node<Self>, &'s Node<Self>) {

        if *point.get_unchecked(self.dim) < self.split {
            (&self.left, &self.right)
        } else {
            (&self.right, &self.left)
        }
    }

    fn dist(&self, point: &[f64]) -> f64 {
        let d = dist(self.center.data(), point);
        if d < self.radius {
            0.
        } else {
            d - self.radius
        }
    }

    fn left(&self) -> &Node<Self> {
        self.left.borrow()
    }

    fn right(&self) -> &Node<Self> {
        self.right.borrow()
    }
}

/// Binary tree node (either branch or leaf)
#[derive(Debug)]
pub enum Node<B: BinarySplit> {
    /// Binary tree branch
    Branch(B),
    /// Binary tree leaf
    Leaf(Leaf)
}

/// Binary tree leaf
#[derive(Debug)]
pub struct Leaf {
    children: Vec<usize>
}

impl Leaf {
    fn new(children: Vec<usize>) -> Self {
        Leaf {
            children: children
        }
    }
}

impl<B: BinarySplit> BinaryTree<B> {

    /// Constructs binary-tree (kd-tree or ball-tree) seach.
    /// Specify leafsize which is maximum number to be contained in each leaf.
    ///
    /// # Examples
    ///
    /// ```
    /// use rusty_machine::learning::knn::{KDTree, BallTree};
    /// let _ = KDTree::new(10);
    /// let _ = BallTree::new(50);
    /// ```
    pub fn new(leafsize: usize) -> Self {
        BinaryTree {
            leafsize: leafsize,
            data: None,
            root: None
        }
    }

    /// Select next split dimension and value. Returns tuple with 6 elements
    /// - split dim
    /// - split value
    /// - remains for left node
    /// - remains for right node
    /// - updated max for left node
    /// - updated min for right node
    fn select_split(&self, data: &Matrix<f64>, mut remains: Vec<usize>,
                    mut dmin: Vector<f64>, mut dmax: Vector<f64>)
        -> (usize, f64, Vec<usize>, Vec<usize>, Vector<f64>, Vector<f64>){

        // avoid recursive call
        loop {
            // split columns which has the widest range
            let (dim, d) = (&dmax - &dmin).argmax();

            // Use midpoint rule, see "On the Efficiency of Nearest Neighbor Searching
            // with Data Clustered in Lower Dimensions (Maneewongvatan and Mount, 1999)"
            // ToDo: use unsafe get (v0.4.0?)
            // https://github.com/AtheMathmo/rulinalg/pull/104
            let split = unsafe {
                dmin.data().get_unchecked(dim) + d / 2.0
            };

            // split remains
            let mut l_remains: Vec<usize> = Vec::with_capacity(remains.len());
            let mut r_remains: Vec<usize> = Vec::with_capacity(remains.len());
            unsafe {
                for r in remains {
                    if *data.get_unchecked([r, dim]) < split {
                        l_remains.push(r);
                    } else {
                        r_remains.push(r);
                    }
                }
            }
            r_remains.shrink_to_fit();
            l_remains.shrink_to_fit();

            if l_remains.is_empty() {
                // all rows are in r_remains. re-split r_remains
                remains = r_remains;
                dmin[dim] = split;
            } else if r_remains.is_empty() {
                // all rows are in l_remains. re-split l_remains
                remains = l_remains;
                dmax[dim] = split;
            } else {
                // new hyper-rectangle's min / max
                let mut l_max = dmax.clone();
                // ToDo: use unsafe mut (v0.4.0?)
                // https://github.com/AtheMathmo/rulinalg/pull/104
                l_max[dim] = split;

                let mut r_min = dmin.clone();
                r_min[dim] = split;

                return (dim, split, l_remains, r_remains, l_max, r_min);
            }
        };
    }

    /// find next binary split
    fn split(&self, data: &Matrix<f64>, remains: Vec<usize>,
             dmin: Vector<f64>, dmax: Vector<f64>) -> Node<B> {

        if remains.len() < self.leafsize {
            Node::Leaf(Leaf::new(remains))
        } else {

            // ToDo: avoid this clone
            let (dim, split, l_remains, r_remains, l_max, r_min) =
                self.select_split(data, remains.clone(), dmin.clone(), dmax.clone());

            let l_node = self.split(data, l_remains, dmin.clone(), l_max);
            let g_node = self.split(data, r_remains, r_min, dmax.clone());
            B::build(data, remains, dim, split, dmin, dmax, l_node, g_node)
        }
    }

    /// find leaf contains search point
    fn search_leaf<'s, 'p>(&'s self, point: &'p [f64], k: usize)
        -> Result<(KNearest, VecDeque<&'s Node<B>>), Error> {

        if let (&Some(ref root), &Some(ref data)) = (&self.root, &self.data) {

            let mut queue: VecDeque<&Node<B>> = VecDeque::new();
            queue.push_front(root);

            loop {
                // pop first element
                let current: &Node<B> = queue.pop_front().unwrap();
                match *current {
                    Node::Leaf(ref l) => {
                        let distances = get_distances(data, point, &l.children);
                        let kn = KNearest::new(k, l.children.clone(), distances);
                        return Ok((kn, queue));
                    },
                    Node::Branch(ref b) => {
                        // the current branch must contains target point.
                        // store the child branch which contains target point to
                        // the front, put other side on the back.
                        let (close, far) = unsafe {
                            b.maybe_close(point)
                        };
                        queue.push_front(close);
                        queue.push_back(far);
                    }
                }
            }
        } else {
            Err(Error::new_untrained())
        }
    }
}

/// Can search k-nearest items
impl<B: BinarySplit> KNearestSearch for BinaryTree<B> {

    /// build data structure for search optimization
    fn build(&mut self, data: Matrix<f64>) {
        let remains: Vec<usize> = (0..data.rows()).collect();
        let dmin = min(&data);
        let dmax = max(&data);
        self.root = Some(self.split(&data, remains, dmin, dmax));
        self.data = Some(data);
    }

    /// Serch k-nearest items close to the point
    fn search(&self, point: &[f64], k: usize) -> Result<(Vec<usize>, Vec<f64>), Error> {
        if let Some(ref data) = self.data {
            let (mut query, mut queue) = self.search_leaf(point, k)?;
            while !queue.is_empty() {
                let current = queue.pop_front().unwrap();

                match *current {
                    Node::Leaf(ref l) => {
                        let distances = get_distances(data, point, &l.children);
                        let mut current_dist = query.dist();

                        for (&i, d) in l.children.iter().zip(distances.into_iter()) {
                            if d < current_dist {
                                current_dist = query.add(i, d);
                            }
                        }
                    },
                    Node::Branch(ref b) => {
                        let d = b.dist(point);
                        if d < query.dist() {
                            queue.push_back(b.left());
                            queue.push_back(b.right());
                        }
                    }
                }
            }
            Ok(query.get_results())
        } else {
            Err(Error::new_untrained())
        }
    }
}


/// min
fn min(data: &Matrix<f64>) -> Vector<f64> {
    // ToDo: use rulinalg .min (v0.4.1?)
    // https://github.com/AtheMathmo/rulinalg/pull/115
    let mut results = Vec::with_capacity(data.cols());
    for i in 0..data.cols() {
        results.push(data[[0, i]]);
    }
    for row in data.row_iter() {
        for (r, v) in results.iter_mut().zip(row.iter()) {
            if *r > *v {
                *r = *v;
            }
        }
    }
    Vector::new(results)
}

/// max
fn max(data: &Matrix<f64>) -> Vector<f64> {
    // ToDo: use rulinalg .max (v0.4.1?)
    // https://github.com/AtheMathmo/rulinalg/pull/115
    let mut results = Vec::with_capacity(data.cols());
    for i in 0..data.cols() {
        results.push(data[[0, i]]);
    }
    for row in data.row_iter() {
        for (r, v) in results.iter_mut().zip(row.iter()) {
            if *r < *v {
                *r = *v;
            }
        }
    }
    Vector::new(results)
}

#[cfg(test)]
mod tests {

    use linalg::{Vector, Matrix, BaseMatrix};
    use super::super::KNearestSearch;
    use super::{KDTree, BallTree, min, max};

    use super::{Node, BinarySplit, Leaf};

    // return node's leaf reference, for testing purpose
    fn as_leaf<B: BinarySplit>(n: &Node<B>) -> &Leaf {
        match n {
            &Node::Leaf(ref leaf) => leaf,
            _ => panic!("Node is not leaf")
        }
    }

    // return node's branch reference, for testing purpose
    fn as_branch<B: BinarySplit>(n: &Node<B>) -> &B {
        match n {
            &Node::Branch(ref branch) => branch,
            _ => panic!("Node is not branch")
        }
    }

    #[test]
    fn test_kdtree_construct() {
        let m = Matrix::new(5, 2, vec![1., 2.,
                                       8., 0.,
                                       6., 10.,
                                       3., 6.,
                                       0., 3.]);
        let mut tree = KDTree::new(3);
        tree.build(m);

        // split to [0, 1, 4] and [2, 3] with columns #1
        let root = tree.root.unwrap();
        let b = as_branch(&root);
        assert_eq!(b.dim, 1);
        assert_eq!(b.split, 5.);
        assert_eq!(b.min, Vector::new(vec![0., 0.]));
        assert_eq!(b.max, Vector::new(vec![8., 10.]));

        // split to [0, 4] and [1] with columns #0
        let bl = as_branch(b.left());
        let br = as_leaf(b.right());
        assert_eq!(bl.dim, 0);
        assert_eq!(bl.split, 4.);
        assert_eq!(bl.min, Vector::new(vec![0., 0.]));
        assert_eq!(bl.max, Vector::new(vec![8., 5.]));
        assert_eq!(br.children, vec![2, 3]);

        let bll = as_leaf(bl.left());
        let blr = as_leaf(bl.right());
        assert_eq!(bll.children, vec![0, 4]);
        assert_eq!(blr.children, vec![1]);
    }

    #[test]
    fn test_kdtree_search() {
        let m = Matrix::new(5, 2, vec![1., 2.,
                                       8., 0.,
                                       6., 10.,
                                       3., 6.,
                                       0., 3.]);
        let mut tree = KDTree::new(3);
        tree.build(m);

        // search first leaf
        let (kn, _) = tree.search_leaf(&vec![3., 4.9], 1).unwrap();
        assert_eq!(kn.pairs, vec![(0, (2.0f64 * 2.0f64 + 2.9f64 * 2.9f64).sqrt())]);

        // search tree
        let (ind, dist) = tree.search(&vec![3., 4.9], 1).unwrap();
        assert_eq!(ind, vec![3]);
        assert_eq!(dist, vec![1.0999999999999996]);

        let (ind, dist) = tree.search(&vec![3., 4.9], 3).unwrap();
        assert_eq!(ind, vec![3, 0, 4]);
        assert_eq!(dist, vec![1.0999999999999996, 3.5227829907617076, 3.551056180912941]);

        // search first leaf
        let (kn, _) = tree.search_leaf(&vec![3., 4.9], 2).unwrap();
        assert_eq!(kn.pairs, vec![(0, (2.0f64 * 2.0f64 + 2.9f64 * 2.9f64).sqrt()),
                                  (4, (3.0f64 * 3.0f64 + (4.9f64 - 3.0f64) * (4.9f64 - 3.0f64)).sqrt())]);
        // search tree
        let (ind, dist) = tree.search(&vec![3., 4.9], 2).unwrap();
        assert_eq!(ind, vec![3, 0]);
        assert_eq!(dist, vec![1.0999999999999996, 3.5227829907617076]);
    }

    #[cfg(feature = "datasets")]
    #[test]
    fn test_kdtree_search_iris_2cols() {
        use super::super::super::super::datasets::iris;

        let dataset = iris::load();
        let data = dataset.data().select_cols(&[0, 1]);

        let mut tree = KDTree::new(10);
        tree.build(data);

        // search tree
        let (ind, dist) = tree.search(&vec![5.8, 3.6], 4).unwrap();
        assert_eq!(ind, vec![18, 85, 36, 14]);
        assert_eq!(dist, vec![0.22360679774997858, 0.2828427124746193, 0.31622776601683783, 0.3999999999999999]);

        let (ind, dist) = tree.search(&vec![7.0, 2.6], 4).unwrap();
        assert_eq!(ind, vec![76, 108, 102, 107]);
        assert_eq!(dist, vec![0.28284271247461895, 0.31622776601683783, 0.41231056256176585, 0.4242640687119283]);
    }

    #[cfg(feature = "datasets")]
    #[test]
    fn test_kdtree_search_iris() {
        use super::super::super::super::datasets::iris;

        let dataset = iris::load();
        let data = dataset.data();

        let mut tree = KDTree::new(10);
        tree.build(data.clone());

        // search tree
        let (ind, dist) = tree.search(&vec![5.8, 3.1, 3.8, 1.2], 8).unwrap();
        assert_eq!(ind, vec![64, 88, 82, 95, 99, 96, 71, 61]);
        assert_eq!(dist, vec![0.360555127546399, 0.3872983346207417, 0.41231056256176596,
                              0.4242640687119288, 0.4472135954999579, 0.4690415759823433,
                              0.4795831523312721, 0.5196152422706636]);

        let (ind, dist) = tree.search(&vec![6.5, 3.5, 3.2, 1.3], 10).unwrap();
        assert_eq!(ind, vec![71, 64, 74, 82, 79, 61, 65, 97, 75, 51]);
        assert_eq!(dist, vec![1.1357816691600549, 1.1532562594670799, 1.2569805089976533,
                              1.2767145334803702, 1.2767145334803702, 1.284523257866513,
                              1.2845232578665131, 1.2884098726725122, 1.3076696830622023,
                              1.352774925846868]);
    }

    #[test]
    fn test_kdtree_dim_selection() {
        let m = Matrix::new(5, 2, vec![1., 2.,
                                       3., 0.,
                                       2., 10.,
                                       3., 6.,
                                       1., 3.]);
        let mut tree = KDTree::new(3);
        tree.build(m);

        // split to [0, 1, 4] and [2, 3] with columns #1
        let root = tree.root.unwrap();
        let b = as_branch(&root);
        assert_eq!(b.dim, 1);
        assert_eq!(b.split, 5.);
        assert_eq!(b.min, Vector::new(vec![1., 0.]));
        assert_eq!(b.max, Vector::new(vec![3., 10.]));

        // split to [0, 1] and [4] with columns #1
        let bl = as_branch(b.left());
        assert_eq!(bl.dim, 1);
        assert_eq!(bl.split, 2.5);
        assert_eq!(bl.min, Vector::new(vec![1., 0.]));
        assert_eq!(bl.max, Vector::new(vec![3., 5.]));

        let br = as_leaf(b.right());
        assert_eq!(br.children, vec![2, 3]);

        let bll = as_leaf(bl.left());
        let blr = as_leaf(bl.right());
        assert_eq!(bll.children, vec![0, 1]);
        assert_eq!(blr.children, vec![4]);
    }

    #[test]
    fn test_kdtree_dim_selection_biased() {
        let m = Matrix::new(5, 2, vec![1., 0.,
                                       3., 0.,
                                       2., 20.,
                                       3., 0.,
                                       1., 0.]);
        let mut tree = KDTree::new(3);
        tree.build(m);

        // split to [0, 1, 3, 4] and [2] with columns #1
        let root = tree.root.unwrap();
        let b = as_branch(&root);
        assert_eq!(b.dim, 1);
        assert_eq!(b.split, 10.);
        assert_eq!(b.min, Vector::new(vec![1., 0.]));
        assert_eq!(b.max, Vector::new(vec![3., 20.]));

        // split to [0, 4] and [1, 3] with columns #0
        let bl = as_branch(b.left());
        assert_eq!(bl.dim, 0);
        assert_eq!(bl.split, 2.);
        assert_eq!(bl.min, Vector::new(vec![1., 0.]));
        assert_eq!(bl.max, Vector::new(vec![3., 10.]));

        let br = as_leaf(b.right());
        assert_eq!(br.children, vec![2]);

        let bll = as_leaf(bl.left());
        let blr = as_leaf(bl.right());
        assert_eq!(bll.children, vec![0, 4]);
        assert_eq!(blr.children, vec![1, 3]);
    }

    #[test]
    fn test_kdtree_untrained() {
        let tree = KDTree::default();

        let e = tree.search_leaf(&vec![3., 4.9], 1);
        assert!(e.is_err());

        let e = tree.search(&vec![3., 4.9], 1);
        assert!(e.is_err());
    }

    #[test]
    fn test_balltree_construct() {
        let m = Matrix::new(5, 2, vec![1., 2.,
                                       8., 0.,
                                       6., 10.,
                                       3., 6.,
                                       0., 3.]);
        let mut tree = BallTree::new(3);
        tree.build(m);

        // split to [0, 1, 4] and [2, 3] with columns #1
        let root = tree.root.unwrap();
        let b = as_branch(&root);
        assert_eq!(b.dim, 1);
        assert_eq!(b.split, 5.);
        assert_eq!(b.center, Vector::new(vec![18. / 5., 21. / 5.]));
        // distance between the center and [2]
        let exp_d: f64 = (6. - 3.6) * (6. - 3.6) + (10. - 4.2) * (10. - 4.2);
        assert_eq!(b.radius, exp_d.sqrt());

        // split to [0, 4] and [1] with columns #0
        let bl = as_branch(b.left());
        let br = as_leaf(b.right());
        assert_eq!(bl.dim, 0);
        assert_eq!(bl.split, 4.);
        assert_eq!(bl.center, Vector::new(vec![3., 5. / 3.]));
        // distance between the center and [1]
        let exp_d: f64 = (3. - 8.) * (3. - 8.) + 5. / 3. * 5. / 3.;
        assert_eq!(bl.radius, exp_d.sqrt());
        assert_eq!(br.children, vec![2, 3]);

        let bll = as_leaf(bl.left());
        let blr = as_leaf(bl.right());
        assert_eq!(bll.children, vec![0, 4]);
        assert_eq!(blr.children, vec![1]);
    }

    #[test]
    fn test_balltree_search() {
        let m = Matrix::new(5, 2, vec![1., 2.,
                                       8., 0.,
                                       6., 10.,
                                       3., 6.,
                                       0., 3.]);
        let mut tree = BallTree::new(3);
        tree.build(m);

        // search first leaf
        let (kn, _) = tree.search_leaf(&vec![3., 4.9], 1).unwrap();
        assert_eq!(kn.pairs, vec![(0, (2.0f64 * 2.0f64 + 2.9f64 * 2.9f64).sqrt())]);

        // search tree
        let (ind, dist) = tree.search(&vec![3., 4.9], 1).unwrap();
        assert_eq!(ind, vec![3]);
        assert_eq!(dist, vec![1.0999999999999996]);

        let (ind, dist) = tree.search(&vec![3., 4.9], 3).unwrap();
        assert_eq!(ind, vec![3, 0, 4]);
        assert_eq!(dist, vec![1.0999999999999996, 3.5227829907617076, 3.551056180912941]);

        // search first leaf
        let (kn, _) = tree.search_leaf(&vec![3., 4.9], 2).unwrap();
        assert_eq!(kn.pairs, vec![(0, (2.0f64 * 2.0f64 + 2.9f64 * 2.9f64).sqrt()),
                                  (4, (3.0f64 * 3.0f64 + (4.9f64 - 3.0f64) * (4.9f64 - 3.0f64)).sqrt())]);
        // search tree
        let (ind, dist) = tree.search(&vec![3., 4.9], 2).unwrap();
        assert_eq!(ind, vec![3, 0]);
        assert_eq!(dist, vec![1.0999999999999996, 3.5227829907617076]);
    }

    #[cfg(feature = "datasets")]
    #[test]
    fn test_balltree_search_iris() {
        use super::super::super::super::datasets::iris;

        let dataset = iris::load();
        let data = dataset.data();

        let mut tree = BallTree::new(10);
        tree.build(data.clone());

        // search tree
        let (ind, dist) = tree.search(&vec![5.8, 3.1, 3.8, 1.2], 8).unwrap();
        assert_eq!(ind, vec![64, 88, 82, 95, 99, 96, 71, 61]);
        assert_eq!(dist, vec![0.360555127546399, 0.3872983346207417, 0.41231056256176596,
                              0.4242640687119288, 0.4472135954999579, 0.4690415759823433,
                              0.4795831523312721, 0.5196152422706636]);

        let (ind, dist) = tree.search(&vec![6.5, 3.5, 3.2, 1.3], 10).unwrap();
        assert_eq!(ind, vec![71, 64, 74, 82, 79, 61, 65, 97, 75, 51]);
        assert_eq!(dist, vec![1.1357816691600549, 1.1532562594670799, 1.2569805089976533,
                              1.2767145334803702, 1.2767145334803702, 1.284523257866513,
                              1.2845232578665131, 1.2884098726725122, 1.3076696830622023,
                              1.352774925846868]);
    }

    #[test]
    fn test_balltree_dim_selection_biased() {
        let m = Matrix::new(5, 2, vec![1., 0.,
                                       3., 0.,
                                       2., 20.,
                                       3., 0.,
                                       1., 0.]);
        let mut tree = BallTree::new(3);
        tree.build(m);

        // split to [0, 1, 3, 4] and [2] with columns #1
        let root = tree.root.unwrap();
        let b = as_branch(&root);
        assert_eq!(b.dim, 1);
        assert_eq!(b.split, 10.);
        assert_eq!(b.center, Vector::new(vec![10. / 5., 20. / 5.]));
        // distance between the center and [2]
        let exp_d: f64 = (2. - 2.) * (2. - 2.) + (4. - 20.) * (4. - 20.);
        assert_eq!(b.radius, exp_d.sqrt());

        // split to [0, 4] and [1, 3] with columns #0
        let bl = as_branch(b.left());
        assert_eq!(bl.dim, 0);
        assert_eq!(bl.split, 2.);
        assert_eq!(bl.center, Vector::new(vec![8. / 4., 0.]));
        // distance between the center and [0]
        let exp_d: f64 = (2. - 1.) * (2. - 1.);
        assert_eq!(bl.radius, exp_d.sqrt());

        let br = as_leaf(b.right());
        assert_eq!(br.children, vec![2]);

        let bll = as_leaf(bl.left());
        let blr = as_leaf(bl.right());
        assert_eq!(bll.children, vec![0, 4]);
        assert_eq!(blr.children, vec![1, 3]);
    }

    #[test]
    fn test_balltree_untrained() {
        let tree = BallTree::default();

        let e = tree.search_leaf(&vec![3., 4.9], 1);
        assert!(e.is_err());

        let e = tree.search(&vec![3., 4.9], 1);
        assert!(e.is_err());
    }

    #[test]
    fn test_min_max() {
        let data = Matrix::new(3, 2, vec![1., 2.,
                                          2., 4.,
                                          3., 1.]);
        assert_eq!(min(&data), Vector::new(vec![1., 1.]));
        assert_eq!(max(&data), Vector::new(vec![3., 4.]));
    }
}