algods 0.1.0

A collection of data structures and algorithms
Documentation
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
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
#[cfg(test)]
mod unit_test;
use std::cmp::Ordering;
use std::collections::BTreeMap;

/// Implementation a tree map with the standard library
/// # Examples
/// ```
/// use algods::data_structure::BTreeTable;
/// let mut bt = BTreeTable::new();
/// assert_eq!(bt.len(), 0);
/// bt.insert(0, "0");
/// bt.insert(1, "1");
/// bt.insert(2, "2");
/// assert_eq!(bt.len(), 3);
/// assert_eq!(bt.delete(&1), Some("1"));
/// assert_eq!(bt.len(), 2);
/// ```
#[derive(Debug, Clone, Default)]
pub struct BTreeTable<T, U> {
    tree: BTreeMap<T, U>,
}
impl<T, U> BTreeTable<T, U> {
    /// Tests whether or not the tree is empty.
    /// # Example
    /// ```
    /// use algods::data_structure::BTreeTable;
    /// let mut bt = BTreeTable::new();
    /// bt.insert(1,1);
    /// assert!(!bt.is_empty());
    /// ```
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
    /// Gives the number of (key, value) pairs in the tree.
    /// # Example
    /// ```
    /// use algods::data_structure::BTreeTable;
    /// let bt = BTreeTable::<usize, usize>::new();
    /// assert_eq!(bt.len(),0);
    /// ```
    pub fn len(&self) -> usize {
        self.tree.len()
    }
}
impl<T: Ord, U> BTreeTable<T, U> {
    /// Creates an empty tree instance.
    /// # Example
    /// ```
    /// use algods::data_structure::BTreeTable;
    /// let bt = BTreeTable::<usize, isize>::new();
    /// assert_eq!(bt.len(), 0);
    /// ```
    pub fn new() -> Self {
        Self {
            tree: BTreeMap::<T, U>::new(),
        }
    }
    /// Creates a new tree with an initial (key, value) pair.
    /// # Example
    /// ```
    /// use algods::data_structure::BTreeTable;
    /// let bt = BTreeTable::init("btree", 0);
    /// assert_eq!(bt.len(), 1);
    /// ```
    pub fn init(key: T, value: U) -> Self {
        let mut tree = Self::new();
        tree.insert(key, value);
        tree
    }
    /// Tests whether or not the tree contains a given key.
    /// # Example
    /// ```
    /// use algods::data_structure::BTreeTable;
    /// let bt = BTreeTable::init("btree", "one");
    /// assert!(bt.contains(&"btree"));
    /// ```
    pub fn contains(&self, key: &T) -> bool {
        self.tree.get(key).is_some()
    }
    /// Returns a reference of the value associated to a key if any exists in the tree.
    /// Returns `None` otherwise.
    /// # Example
    /// ```
    /// use algods::data_structure::BTreeTable;
    /// let bt = BTreeTable::init("btree", "one");
    /// assert_eq!(bt.get(&"no btree"), None);
    /// assert_eq!(bt.get(&"btree"), Some(&"one"));
    /// ```    
    pub fn get(&self, key: &T) -> Option<&U> {
        self.tree.get(key)
    }
    /// Inserts a (key, value) pair in the tree.
    /// # Example
    /// ```
    /// use algods::data_structure::BTreeTable;
    /// let mut bt = BTreeTable::<isize, usize>::new();
    /// bt.insert(-1, 2);
    /// bt.insert(-2, 3);
    /// assert_eq!(bt.get(&-2), Some(&3));
    /// assert_eq!(bt.len(), 2);
    /// ```
    pub fn insert(&mut self, key: T, value: U) {
        self.tree.insert(key, value);
    }
    ///
    /// Removes a key from the tree, returning the value assiociated if any.
    /// Otherwise it returns `None`.
    /// # Example
    /// ```
    /// use algods::data_structure::BTreeTable;
    /// let mut bt = BTreeTable::init(1, 2);
    /// assert_eq!(bt.delete(&1), Some(2));
    /// assert_eq!(bt.delete(&10), None);
    /// ```
    pub fn delete(&mut self, key: &T) -> Option<U> {
        self.tree.remove(key)
    }
}
impl<T: Ord, U: Ord> BTreeTable<T, U> {
    /// Returns for the largest key in the tree strictly inferior.
    /// # Example
    /// ```
    /// use algods::data_structure::BTreeTable;
    /// let mut bt = BTreeTable::<isize, usize>::init(1, 0);
    /// bt.insert(-1, 2);
    /// bt.insert(-2, 3);
    /// assert_eq!(bt.strict_floor(&1), Some(&-1));
    /// assert_eq!(bt.strict_floor(&-2), None);
    pub fn strict_floor(&self, key: &T) -> Option<&T> {
        // the largest key in the tree map, strictly inferior to key
        let res = self.tree.range(..key).max();
        if let Some(item) = res {
            Some(item.0)
        } else {
            None
        }
    }
    /// Returns the smallest key larger or equal to the given key.
    /// # Example
    /// ```
    /// use algods::data_structure::BTreeTable;
    /// let mut bt = BTreeTable::<isize, usize>::init(1, 0);
    /// bt.insert(-1, 2);
    /// bt.insert(-2, 3);
    /// assert_eq!(bt.ceil(&1), Some(&1));
    /// assert_eq!(bt.ceil(&-1), Some(&-1));
    /// assert_eq!(bt.ceil(&2), None);
    /// ```
    pub fn ceil(&self, key: &T) -> Option<&T> {
        // the smallest key in the tree map larger ot equal to key
        let res = self.tree.range(key..).min();
        if let Some(item) = res {
            Some(item.0)
        } else {
            None
        }
    }
    /// Returns the list of keys in the tree that are between two keys (low included, high excluded).
    /// # Example
    /// ```
    /// use algods::data_structure::BTreeTable;
    /// let mut bt = BTreeTable::<isize, usize>::init(1, 0);
    /// bt.insert(-1, 2);
    /// bt.insert(-2, 2);
    /// bt.insert(-3, 3);
    /// assert_eq!(bt.range_search(&-2, &1), vec![&-2, &-1]);
    /// ```
    pub fn range_search(&self, low: &T, high: &T) -> Vec<&T> {
        // returns the keys between low (included) and high (excluded)
        self.tree
            .range(low..high)
            .into_iter()
            .map(|item| item.0)
            .collect::<Vec<&T>>()
    }
    /// Returns the number of keys in the tree that are between two keys (low included, high excluded).
    /// # Example
    /// ```
    /// use algods::data_structure::BTreeTable;
    /// let mut bt = BTreeTable::<isize, usize>::init(1, 0);
    /// bt.insert(-1, 2);
    /// bt.insert(-2, 2);
    /// bt.insert(-3, 3);
    /// assert_eq!(bt.range_count(&-3, &-1), 2);
    /// ```
    pub fn range_count(&self, low: &T, high: &T) -> usize {
        // counts the keys between low (included) and high (excluded)
        self.range_search(low, high).len()
    }
}

#[derive(Clone, Debug, PartialEq)]
struct Node<T, U> {
    key: T,
    value: U,
    left: Option<Box<Node<T, U>>>,
    right: Option<Box<Node<T, U>>>,
}
impl<T, U> Node<T, U> {
    pub fn init(_key: T, _value: U) -> Self {
        Self {
            key: _key,
            value: _value,
            left: None,
            right: None,
        }
    }
}

/// Implementation of a binary search tree
/// # Example
/// ```
/// use algods::data_structure::BSearchTree;
/// let mut bt = BSearchTree::new();
/// bt.insert(0,"1");
/// bt.insert(1,"2");
/// bt.insert(2,"3");
/// assert_eq!(bt.len(), 3);
/// assert!(bt.contains(&0));
/// assert_eq!(bt.get(&2), Some(&"3"));
/// ```
#[derive(Debug, Clone)]
pub struct BSearchTree<T, U> {
    root: Option<Box<Node<T, U>>>,
    len: usize,
}
impl<T, U> Default for BSearchTree<T, U> {
    fn default() -> Self {
        Self::new()
    }
}
impl<T, U> BSearchTree<T, U> {
    /// Creates an empty tree instance.
    /// # Example
    /// ```
    /// use algods::data_structure::BSearchTree;
    /// let bt = BSearchTree::<usize, isize>::new();
    /// assert_eq!(bt.len(), 0);
    /// ```
    pub fn new() -> Self {
        Self { root: None, len: 0 }
    }
    /// Creates a new tree with an initial (key, value) pair.
    /// # Example
    /// ```
    /// use algods::data_structure::BSearchTree;
    /// let bt = BSearchTree::init("btree", 0);
    /// assert_eq!(bt.len(), 1);
    /// ```
    pub fn init(key: T, value: U) -> Self {
        Self {
            root: Some(Box::new(Node::init(key, value))),
            len: 1,
        }
    }
    /// Gives the number of (key, value) pairs in the tree.
    /// # Example
    /// ```
    /// use algods::data_structure::BSearchTree;
    /// let bt = BSearchTree::<usize, usize>::new();
    /// assert_eq!(bt.len(),0);
    /// ```
    pub fn len(&self) -> usize {
        self.len
    }
    /// Tests whether or not the tree is empty.
    /// # Example
    /// ```
    /// use algods::data_structure::BSearchTree;
    /// let mut bt = BSearchTree::new();
    /// bt.insert(1,1);
    /// assert!(!bt.is_empty());
    /// ```
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
}
impl<T: Eq + Ord, U: Eq> BSearchTree<T, U> {
    /// Tests whether or not the tree contains a given key.
    /// # Example
    /// ```
    /// use algods::data_structure::BSearchTree;
    /// let bt = BSearchTree::init("btree", "one");
    /// assert!(bt.contains(&"btree"));
    /// ```
    pub fn contains(&self, key: &T) -> bool {
        self.get(key).is_some()
    }
    /// Returns a reference of the value associated to a key if any exists in the tree.
    /// Returns `None` otherwise.
    /// # Example
    /// ```
    /// use algods::data_structure::BSearchTree;
    /// let bt = BSearchTree::init("btree", "one");
    /// assert_eq!(bt.get(&"no btree"), None);
    /// assert_eq!(bt.get(&"btree"), Some(&"one"));
    /// ```
    pub fn get(&self, key: &T) -> Option<&U> {
        // gets the value associated to key if key is in
        // the tree, otherwise returns None,
        // run time complexity on average O(log(N)), O(N) guaranteed (unbalanced tree)
        let mut node = &self.root;
        while node.is_some() {
            let temp_node = node.as_ref().unwrap();
            match key.cmp(&temp_node.key) {
                Ordering::Less => node = &temp_node.left,
                Ordering::Greater => node = &temp_node.right,
                Ordering::Equal => return Some(&temp_node.value),
            }
        }
        None
    }
}
impl<T: Ord, U> BSearchTree<T, U> {
    fn put(node: &mut Option<Box<Node<T, U>>>, key: T, value: U) -> Option<&U> {
        match node {
            None => *node = Some(Box::new(Node::init(key, value))),
            Some(ref mut nod) => match key.cmp(&nod.key) {
                Ordering::Less => {
                    return Self::put(&mut nod.left, key, value);
                }
                Ordering::Greater => {
                    return Self::put(&mut nod.right, key, value);
                }
                Ordering::Equal => {
                    nod.value = value;
                    return Some(&nod.value);
                }
            },
        }
        None
    }
    /// Inserts a (key, value) pair in the tree.
    /// # Example
    /// ```
    /// use algods::data_structure::BSearchTree;
    /// let mut bt = BSearchTree::<isize, usize>::new();
    /// bt.insert(-1, 2);
    /// bt.insert(-2, 3);
    /// bt.insert(-1, 4);
    /// assert_eq!(bt.len(), 2);
    /// assert_eq!(bt.get(&-2), Some(&3));
    /// ```
    pub fn insert(&mut self, key: T, value: U) {
        if Self::put(&mut self.root, key, value).is_none() {
            self.len += 1;
        }
    }
}
impl<T: Eq + Ord, U: Ord> BSearchTree<T, U> {
    /// Returns the smallest key in the tree.
    /// # Example
    /// ```
    /// use algods::data_structure::BSearchTree;
    /// let mut bt = BSearchTree::<isize, usize>::init(1, 0);
    /// bt.insert(-1, 2);
    /// assert_eq!(bt.min(), Some(&-1));
    /// ```
    pub fn min(&self) -> Option<&T> {
        // finds the minimum key
        let mut node = &self.root;
        let mut result = None;
        while node.is_some() {
            // go to the left as long as you do not encounter
            // a None Node
            let temp_node = node.as_ref().unwrap();
            result = Some(&temp_node.key);
            node = &temp_node.left;
        }
        result
    }
    /// Returns the largest key in the tree.
    /// # Example
    /// ```
    /// use algods::data_structure::BSearchTree;
    /// let mut bt = BSearchTree::<isize, usize>::init(0, 0);
    /// bt.insert(-1, 2);
    /// assert_eq!(bt.max(), Some(&0));
    /// ```
    pub fn max(&self) -> Option<&T> {
        // finds the maximum key
        let mut node = &self.root;
        let mut result = None;
        while node.is_some() {
            // go to the right as long as you do not encounter
            // a None Node
            let temp_node = node.as_ref().unwrap();
            result = Some(&temp_node.key);
            node = &temp_node.right;
        }
        result
    }
    fn recursive_floor<'a>(
        node: &'a Option<Box<Node<T, U>>>,
        key: &T,
    ) -> &'a Option<Box<Node<T, U>>> {
        if node.is_none() {
            return &None;
        }
        let current_node = node.as_ref().unwrap();
        if key == &current_node.key {
            return node;
        }
        if key < &current_node.key {
            return Self::recursive_floor(&current_node.left, key);
        }
        let temp_node = Self::recursive_floor(&current_node.right, key);
        if temp_node.is_some() {
            temp_node
        } else {
            node
        }
    }
    /// Returns for the largest key in the tree smaller or equal to the input key.
    /// # Example
    /// ```
    /// use algods::data_structure::BSearchTree;
    /// let mut bt = BSearchTree::<isize, usize>::init(1, 0);
    /// bt.insert(-1, 2);
    /// bt.insert(-2, 3);
    /// assert_eq!(bt.floor(&1), Some(&1));
    /// assert_eq!(bt.floor(&0), Some(&-1));
    /// ```
    pub fn floor(&self, key: &T) -> Option<&T> {
        // the largest key in the tree smaller or equal to key
        // run time complexity O(log(N)) on average, O(N) (guaranteed)
        let node = Self::recursive_floor(&self.root, key);
        if node.is_none() {
            return None;
        }
        return Some(&node.as_ref().unwrap().key);
    }
}

/// Implementation of a tree map based on an ordered `Vec`.
/// # Example
/// ```
/// use algods::data_structure::OrdVecTable;
/// let mut table = OrdVecTable::new();
/// table.insert(0,"1");
/// table.insert(1,"2");
/// table.insert(2,"3");
/// assert_eq!(table.len(), 3);
/// assert!(table.contains(&0));
/// assert_eq!(table.get(&2), Some(&"3"));
// ###########################################
#[derive(Default, Clone, Debug)]
pub struct OrdVecTable<T, U> {
    // collection of key-value pair (no duplicate keys)
    vec: Vec<Pair<T, Option<U>>>,
}
impl<T, U> OrdVecTable<T, U> {
    /// Creates an empty tree instance.
    /// # Example
    /// ```
    /// use algods::data_structure::OrdVecTable;
    /// let tree = OrdVecTable::<usize, isize>::new();
    /// assert_eq!(tree.len(), 0);
    /// ```
    pub fn new() -> Self {
        Self { vec: Vec::new() }
    }
    /// Creates a new tree with an initial (key, value) pair.
    /// # Example
    /// ```
    /// use algods::data_structure::OrdVecTable;
    /// let table = OrdVecTable::init("table", 0);
    /// assert_eq!(table.len(), 1);
    /// ```
    pub fn init(key: T, value: U) -> Self {
        let mut symbol_table = Self::new();
        symbol_table.vec.push(Pair::init(key, Some(value)));
        symbol_table
    }
    /// Gives the number of (key, value) pairs in the tree.
    /// # Example
    /// ```
    /// use algods::data_structure::OrdVecTable;
    /// let table = OrdVecTable::<usize, usize>::new();
    /// assert_eq!(table.len(), 0);
    /// ```
    pub fn len(&self) -> usize {
        self.vec.len()
    }
    /// Tests whether or not the tree is empty.
    /// # Example
    /// ```
    /// use algods::data_structure::OrdVecTable;
    /// let mut table = OrdVecTable::new();
    /// table.insert(1,1);
    /// assert!(!table.is_empty());
    /// ```
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
    /// Returns the smallest key in the tree.
    /// # Example
    /// ```
    /// use algods::data_structure::OrdVecTable;
    /// let mut table = OrdVecTable::<isize, usize>::init(1, 0);
    /// table.insert(-1, 2);
    /// assert_eq!(table.min(), Some(&-1));
    /// ```
    pub fn min(&self) -> Option<&T> {
        // smallest key O(1)
        if self.is_empty() {
            None
        } else {
            Some(self.vec[0].first())
        }
    }
    /// Returns the largest key in the tree.
    /// # Example
    /// ```
    /// use algods::data_structure::OrdVecTable;
    /// let mut table = OrdVecTable::<isize, usize>::init(1, 0);
    /// table.insert(-1, 2);
    /// assert_eq!(table.max(), Some(&1));
    /// ```
    pub fn max(&self) -> Option<&T> {
        // largest key O(1)
        if self.vec.is_empty() {
            None
        } else {
            Some(self.vec[self.vec.len() - 1].first())
        }
    }
}
impl<T: Ord + Clone, U: Eq> OrdVecTable<T, U> {
    /// Tests whether or not the tree contains a given key.
    /// # Example
    /// ```
    /// use algods::data_structure::OrdVecTable;
    /// let table = OrdVecTable::init("table", "one");
    /// assert!(table.contains(&"table"));
    /// ```
    pub fn contains(&self, key: &T) -> bool {
        // run time complexity O(log(N))
        self.get(key).is_some()
    }
    /// Returns a reference of the value associated to a key if any exists in the tree.
    /// Returns `None` otherwise.
    /// # Example
    /// ```
    /// use algods::data_structure::OrdVecTable;
    /// let table = OrdVecTable::init("tree", "one");
    /// assert_eq!(table.get(&"no tree"), None);
    /// assert_eq!(table.get(&"tree"), Some(&"one"));
    /// ```
    pub fn get(&self, key: &T) -> Option<&U> {
        // run time complexity O(log(N))
        if let Ok(index) = self.vec.binary_search(&Pair::init(key.clone(), None)) {
            return self.vec[index].second().as_ref();
        } else {
            None
        }
    }
    /// Returns for the largest key in the tree smaller or equal to the input key.
    /// # Example
    /// ```
    /// use algods::data_structure::OrdVecTable;
    /// let mut table = OrdVecTable::<isize, usize>::init(1, 0);
    /// table.insert(-1, 2);
    /// table.insert(-2, 3);
    /// assert_eq!(table.floor(&1), Some(&1));
    /// assert_eq!(table.floor(&0), Some(&-1));
    /// ```
    pub fn floor(&self, key: &T) -> Option<&T> {
        // largest key smaller or equal to key O(log(N))
        if self.is_empty() {
            None
        } else {
            let index = self.vec.binary_search(&Pair::init(key.clone(), None));
            match index {
                Ok(ind) => Some(self.vec[ind].first()),
                Err(ind) => {
                    if ind > 0 {
                        Some(self.vec[ind - 1].first())
                    } else {
                        // all keys in the table are > keys
                        None
                    }
                }
            }
        }
    }
    /// Returns for the smallest key in the tree larger or equal to the input key.
    /// # Example
    /// ```
    /// use algods::data_structure::OrdVecTable;
    /// let mut table = OrdVecTable::<isize, usize>::init(1, 0);
    /// table.insert(-1, 2);
    /// table.insert(-2, 3);
    /// assert_eq!(table.ceil(&1), Some(&1));
    /// assert_eq!(table.ceil(&2), None);
    /// assert_eq!(table.ceil(&-3), Some(&-2));
    /// ```
    pub fn ceil(&self, key: &T) -> Option<&T> {
        // smallest key larger or equal to key, O(log(N))
        if self.is_empty() {
            None
        } else {
            let index = self.vec.binary_search(&Pair::init(key.clone(), None));
            match index {
                Ok(ind) => Some(self.vec[ind].first()),
                Err(ind) => {
                    if ind < self.vec.len() - 1 && ind > 0 {
                        Some(self.vec[ind + 1].first())
                    } else if ind == 0 {
                        // key is < to all keys
                        Some(self.vec[0].first())
                    } else {
                        // all keys in the table are < key
                        None
                    }
                }
            }
        }
    }
}
impl<T: Ord + Clone, U: Eq + Clone> OrdVecTable<T, U> {
    fn put(&mut self, key: T, value: Option<U>) -> Option<U> {
        // run time complexity O(N) due to insertion
        let candidate = Pair::init(key.clone(), None);
        let index = self.vec.binary_search(&candidate);
        match index {
            Ok(ind) => {
                // key is found
                let temp_val = self.vec[ind].second().as_ref().cloned();
                let mut_val = self.vec[ind].second_mut();
                *mut_val = value;
                temp_val
            }
            Err(ind) => {
                // index where to insert key to keep self.vec sorted
                if ind < self.vec.len() {
                    self.vec.insert(ind, Pair::init(key, value));
                } else {
                    self.vec.push(Pair::init(key, value))
                }
                None
            }
        }
    }
    /// Inserts a (key, value) pair in the tree.
    /// # Example
    /// ```
    /// use algods::data_structure::OrdVecTable;
    /// let mut table = OrdVecTable::<isize, usize>::new();
    /// table.insert(-1, 2);
    /// table.insert(-2, 3);
    /// table.insert(-1, 4);
    /// assert_eq!(table.len(), 2);
    /// assert_eq!(table.get(&-2), Some(&3));
    /// ```
    pub fn insert(&mut self, key: T, value: U) {
        // run time complexity O(N)
        self.put(key, Some(value));
    }
    /// Deletes a key in the tree using a lazy implementation:
    /// meaning that it replaces the value of the key by `None` if any.
    /// # Example
    /// ```
    /// use algods::data_structure::OrdVecTable;
    /// let mut table = OrdVecTable::<isize, usize>::new();
    /// table.insert(-1, 2);
    /// table.insert(-2, 3);
    /// table.insert(-1, 4);
    /// assert_eq!(table.delete(&-1), Some(4));
    /// assert_eq!(table.delete(&0), None);
    /// ```
    pub fn delete(&mut self, key: &T) -> Option<U> {
        // run time complexity O(N)
        self.put(key.clone(), None) // lazy implementation
    }
}
#[derive(Default, Clone, Debug)]
struct Pair<T, U> {
    tuple: (T, U),
}
impl<T, U> Pair<T, U> {
    pub fn init(key: T, value: U) -> Self {
        Self {
            tuple: (key, value),
        }
    }
    pub fn first(&self) -> &T {
        &self.tuple.0
    }

    pub fn second(&self) -> &U {
        &self.tuple.1
    }
    pub fn first_mut(&mut self) -> &mut T {
        &mut self.tuple.0
    }
    pub fn second_mut(&mut self) -> &mut U {
        &mut self.tuple.1
    }
}
impl<T: Ord, U> Ord for Pair<T, U> {
    fn cmp(&self, other: &Self) -> Ordering {
        self.tuple.0.cmp(&other.tuple.0)
    }
}
impl<T: Ord, U> PartialOrd for Pair<T, U> {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.tuple.0.cmp(&other.tuple.0))
    }
}
impl<T: Ord, U> Eq for Pair<T, U> {}
impl<T: Ord, U> PartialEq for Pair<T, U> {
    fn eq(&self, other: &Self) -> bool {
        self.tuple.0 == other.tuple.0
    }
}

// ###############################################
/// Implementation of a tree map based on an unordered `Vec`.
/// # Example
/// ```
/// use algods::data_structure::UnordVecTable;
/// let mut table = UnordVecTable::new();
/// table.insert(0,"1");
/// table.insert(1,"2");
/// table.insert(2,"3");
/// assert_eq!(table.len(), 3);
/// assert!(table.contains(&0));
/// assert_eq!(table.get(&2), Some(&"3"));
#[derive(Default, Clone, Debug)]
pub struct UnordVecTable<T, U> {
    // collection of key-value pair (no duplicate keys)
    vec: Vec<(T, Option<U>)>,
}
impl<T, U> UnordVecTable<T, U> {
    /// Creates an empty tree instance.
    /// # Example
    /// ```
    /// use algods::data_structure::UnordVecTable;
    /// let tree = UnordVecTable::<usize, isize>::new();
    /// assert_eq!(tree.len(), 0);
    /// ```
    pub fn new() -> Self {
        Self { vec: Vec::new() }
    }
    /// Creates a new tree with an initial (key, value) pair.
    /// # Example
    /// ```
    /// use algods::data_structure::UnordVecTable;
    /// let table = UnordVecTable::init("table", 0);
    /// assert_eq!(table.len(), 1);
    /// ```
    pub fn init(key: T, value: U) -> Self {
        let mut symbol_table = Self::new();
        symbol_table.vec.push((key, Some(value)));
        symbol_table
    }
    /// Gives the number of (key, value) pairs in the tree.
    /// # Example
    /// ```
    /// use algods::data_structure::UnordVecTable;
    /// let table = UnordVecTable::<usize, usize>::new();
    /// assert_eq!(table.len(), 0);
    /// ```
    pub fn len(&self) -> usize {
        self.vec.len()
    }
    /// Tests whether or not the tree is empty.
    /// # Example
    /// ```
    /// use algods::data_structure::UnordVecTable;
    /// let mut table = UnordVecTable::new();
    /// table.insert(1,1);
    /// assert!(!table.is_empty());
    /// ```
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
}
impl<T: Eq, U: Eq> UnordVecTable<T, U> {
    /// Tests whether or not the tree contains a given key.
    /// # Example
    /// ```
    /// use algods::data_structure::UnordVecTable;
    /// let table = UnordVecTable::init("table", "one");
    /// assert!(table.contains(&"table"));
    /// ```
    pub fn contains(&self, key: &T) -> bool {
        // run time complexity O(N)
        self.get(key).is_some()
        // self.list.iter().any(|e| e.0 == key)
    }
}
impl<T: Eq, U> UnordVecTable<T, U> {
    /// Returns a reference of the value associated to a key if any exists in the tree.
    /// Returns `None` otherwise.
    /// # Example
    /// ```
    /// use algods::data_structure::UnordVecTable;
    /// let table = UnordVecTable::init("tree", "one");
    /// assert_eq!(table.get(&"no tree"), None);
    /// assert_eq!(table.get(&"tree"), Some(&"one"));
    /// ```
    pub fn get(&self, key: &T) -> Option<&U> {
        // run time complexity O(N)
        for k in 0..self.vec.len() {
            if &self.vec[k].0 == key {
                return self.vec[k].1.as_ref();
            }
        }
        None
    }
}
impl<T: Eq, U: Clone> UnordVecTable<T, U> {
    fn put(&mut self, key: T, value: Option<U>) -> Option<U> {
        // run time complexity O(N)
        let mut k = 0;
        let mut val = None;
        let length = self.vec.len();
        while k < length {
            if self.vec[k].0 == key {
                val = self.vec[k].1.clone();
                self.vec[k].1 = value.clone();
                break;
            }
            k += 1;
        }
        if self.is_empty() || (k == length && value.is_some()) {
            self.vec.push((key, value));
        }
        val
    }
    /// Inserts a (key, value) pair in the tree.
    /// # Example
    /// ```
    /// use algods::data_structure::UnordVecTable;
    /// let mut table = UnordVecTable::<isize, usize>::new();
    /// table.insert(-1, 2);
    /// table.insert(-2, 3);
    /// table.insert(-1, 4);
    /// assert_eq!(table.len(), 2);
    /// assert_eq!(table.get(&-1), Some(&4));
    /// ```
    pub fn insert(&mut self, key: T, value: U) {
        // run time complexity O(N)
        self.put(key, Some(value));
    }
}
impl<T: Eq + Clone, U: Clone> UnordVecTable<T, U> {
    /// Deletes a key in the tree using a lazy implementation:
    /// meaning that it replaces the value of the key by `None` if any.
    /// # Example
    /// ```
    /// use algods::data_structure::UnordVecTable;
    /// let mut table = UnordVecTable::<isize, usize>::new();
    /// table.insert(-1, 2);
    /// table.insert(-2, 3);
    /// table.insert(-1, 4);
    /// assert_eq!(table.delete(&-1), Some(4));
    /// assert_eq!(table.delete(&0), None);
    /// assert_eq!(table.len(), 2);
    /// ```
    pub fn delete(&mut self, key: &T) -> Option<U> {
        // run time complexity O(N)
        self.put(key.clone(), None) // lazy implementation
    }
}