Crates.io | ABtree |
lib.rs | ABtree |
version | 0.8.0 |
source | src |
created_at | 2021-12-05 08:56:19.027477 |
updated_at | 2021-12-26 12:12:01.242818 |
description | AVL and Btree for rust |
homepage | |
repository | https://github.com/GuoHao150/ABtree |
max_upload_size | |
id | 492612 |
size | 108,129 |
This crate named as ABtree but this not means it is a novel data sturcture. It’s just AVL tree and Btree. For the Btree, what makes it different from that of BtreeMap in std is this Btree can accept any number as the maximum number of inner node, as long as the number greater or equal to 3
use ABtree::AVL;
let t = AVL::<i32, i32>::new();
use ABtree::AVL;
let mut t = AVL::<i32, i32>::new();
t.insert(2, 3);
assert_eq!(t.len(), 1);
If the key not exists it will add the key-value pair into the tree
use ABtree::AVL;
let mut t = AVL::<i32, i32>::new();
t.set(2, 2);
t.set(2, 31);
assert_eq!(t.get(&2), Some(&31));
use ABtree::AVL;
let mut t = AVL::<i32, i32>::new();
t.insert(2, 2);
t.insert(3, 3);
assert_eq!(t.len(), 2);
Note the next() and next_back() are two independent operations which means a node can be traversed by both methods
use ABtree::AVL;
let t = AVL::<i32, i32>::new();
use ABtree::AVL;
let mut t: AVL<u32, u32> = AVL::new();
t.insert(0, 0);
t.insert(1, 1);
t.insert(2, 2);
assert!(t.contains(&1));
use ABtree::AVL;
let mut t: AVL<u32, u32> = AVL::new();
t.insert(0, 0);
t.insert(1, 1);
t.insert(2, 2);
assert_eq!(t.remove(&1), Some(1));
assert_eq!(t.len(), 2);
use ABtree::AVL;
let mut t: AVL<u32, u32> = AVL::new();
t.insert(0, 0);
t.insert(1, 1);
t.insert(2, 2);
assert_eq!(t.peek_root(), Some((&1, &1)));
use ABtree::AVL;
let mut t: AVL<u32, u32> = AVL::new();
t.insert(0, 0);
t.insert(1, 1);
t.insert(2, 2);
assert_eq!(t.is_empty(), false);
use ABtree::AVL;
let mut t: AVL<u32, u32> = AVL::new();
t.insert(0, 0);
t.insert(1, 1);
t.insert(2, 2);
t.clear();
assert_eq!(t.len(), 0);
use ABtree::AVL;
let mut t: AVL<u32, u32> = AVL::new();
t.insert(0, 0);
t.insert(1, 1);
t.insert(2, 2);
assert_eq!(t.get(&1), Some(&1));
use std::iter::FromIterator;
use ABtree::AVL;
let data = vec![
(12, 1),
(8, 1),
(17, 1),
];
let a = AVL::from_iter(data);
use std::iter::FromIterator;
use ABtree::AVL;
let data = vec![
(12, 1),
(8, 1),
(17, 1),
];
let a = AVL::from_iter(data);
let iter = a.into_iter();
choose any number as the maximum number for the inner node as long as this number greater or equal to 3
use ABtree::BTree;
let mut b: BTree<i32, i32> = BTree::new(4);
b.insert(1, 1);
use ABtree::BTree;
let mut b: BTree<i32, i32> = BTree::new(4);
b.insert(1, 1);
use ABtree::BTree;
let mut b: BTree<i32, i32> = BTree::new(4);
let data = [(1, 1), (2, 2), (3, 3)];
for (k, v) in data {
b.insert(k, v)
}
assert_eq!(b.get(&2), Some(&2));
use ABtree::BTree;
let mut b: BTree<i32, i32> = BTree::new(3);
let data = [(1, 1), (2, 2), (3, 3)];
for (k, v) in data {
b.insert(k, v)
}
b.set(2, 200);
use ABtree::BTree;
let mut b: BTree<i32, i32> = BTree::new(4);
let data = [(1, 1), (2, 2), (3, 3)];
for (k, v) in data {
b.insert(k, v)
}
assert!(b.contains(&2));
use ABtree::BTree;
let mut b: BTree<i32, i32> = BTree::new(4);
let data = [(1, 1), (2, 2), (3, 3)];
for (k, v) in data {
b.insert(k, v)
}
assert_eq!(b.remove(&2), Some(2));
use ABtree::BTree;
let mut b: BTree<i32, i32> = BTree::new(4);
let data = [(1, 1), (2, 2), (3, 3)];
for (k, v) in data {
b.insert(k, v)
}
assert_eq!(b.remove(&2), Some(2));
use ABtree::BTree;
let mut b: BTree<i32, i32> = BTree::new(4);
let data = [(1, 1), (2, 2), (3, 3)];
for (k, v) in data {
b.insert(k, v)
}
assert_eq!(b.len(), 3);
use ABtree::BTree;
let mut b: BTree<i32, i32> = BTree::new(4);
let data = [(1, 1), (2, 2), (3, 3)];
for (k, v) in data {
b.insert(k, v)
}
assert!(!b.is_empty());
use ABtree::BTree;
let mut b: BTree<i32, i32> = BTree::new(4);
let data = [(1, 1), (2, 2), (3, 3)];
for (k, v) in data {
b.insert(k, v)
}
b.clear();
assert_eq!(b.len(), 0);
If use from_iter() to create b-tree then the maximum number of a inner node size is 3 which makes it a 2-3 tree
use std::iter::FromIterator;
use ABtree::BTree;
let data1 = vec![
(12, 1),
(8, 1),
(17, 1),
];
let b = BTree::from_iter(data1);
b.iter().for_each(|n| println!("{}", n.0));
use std::iter::FromIterator;
use ABtree::BTree;
let data1 = vec![
(12, 1),
(8, 1),
(17, 1),
];
let b = BTree::from_iter(data1);
b.into_iter().for_each(|n| println!("{}", n.0));