btree
Some checks failed
Rust CI / Test Suite (push) Failing after 2s
Rust CI / Clippy (push) Failing after 4s

This commit is contained in:
cscherr 2025-06-04 18:06:01 +02:00
parent dd51d8d1d1
commit 325c491691
Signed by: cscherrNT
GPG key ID: 8E2B45BC51A27EA7
2 changed files with 55 additions and 57 deletions

View file

@ -1,66 +1,61 @@
#[derive(Clone, Debug, Default)] use crate::vec::Vec;
pub struct BTree<K: Ord, V> {
root: Option<Node<K, V>>, #[derive(Clone, Debug)]
len: usize, pub struct BTree<T> {
root: Node<T>,
degree: usize,
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct Node<K, V> { struct Node<T> {
key: K, keys: Vec<T>,
value: V, children: Vec<Node<T>>,
} }
impl<K, V> Node<K, V> { impl<T> Node<T>
pub fn new_root(key: K, value: V) -> Self { where
Node { key, value } T: Ord,
{
fn new(degree: usize, keys: Option<Vec<T>>, children: Option<Vec<Node<T>>>) -> Self {
Node {
keys: match keys {
Some(keys) => keys,
None => Vec::with_capacity(degree - 1),
},
children: match children {
Some(children) => children,
None => Vec::with_capacity(degree),
},
}
}
fn is_leaf(&self) -> bool {
self.children.is_empty()
} }
} }
impl<K: Ord, V> BTree<K, V> { impl<T: Ord> BTree<T> {
pub fn new() -> Self { pub fn new(branch_factor: usize) -> Self {
Self { root: None, len: 0 } let degree = 2 * branch_factor;
Self {
root: Node::new(degree, None, None),
degree,
}
} }
pub fn clear(&mut self) { pub fn clear(&mut self) {
self.root = None; self.root = Node::new(self.degree, None, None);
self.len = 0;
} }
pub fn rebalance() { fn rebalance() {
todo!() todo!()
} }
pub fn insert(&mut self, key: K, value: V) -> Option<V> { pub fn insert(&mut self, value: T) -> Option<T> {
if let Some(root) = &self.root {
todo!()
} else {
let new_node = Node::new_root(key, value);
self.root = Some(new_node);
None
}
}
pub fn remove(&mut self, key: K) -> Option<V> {
todo!() todo!()
} }
pub fn has_key(&self, key: K) -> bool { pub fn has(&self, value: T) -> bool {
todo!()
}
pub fn get(&self, key: K) -> Option<&V> {
todo!()
}
pub fn get_mut(&mut self, key: K) -> Option<&mut V> {
todo!()
}
pub fn get_node(&self, key: K) -> Option<&Node<K, V>> {
todo!()
}
pub fn get_node_mut(&mut self, key: K) -> Option<&mut Node<K, V>> {
todo!() todo!()
} }
} }
@ -70,18 +65,20 @@ mod test {
use super::*; use super::*;
#[test] #[test]
fn test_create() { fn test_create() {
let _tree = BTree::<u32, u32>::default(); let _tree = BTree::<u32>::new();
} }
#[test] #[test]
fn test_insert() { fn test_insert() {
let mut tree = BTree::<u32, &str>::default(); let mut tree = BTree::<u32>::new();
tree.insert(19, "19"); let data = &[19, 125, 25, 16, 2, 73, 384, 435, 12924, 42, 125251, 2548];
tree.insert(9, "09");
tree.insert(14, "14");
assert!(tree.get(19).is_some_and(|s| *s == "19")); for d in data {
assert!(tree.get(9).is_some_and(|s| *s == "09")); assert!(tree.insert(*d).is_none())
assert!(tree.get(14).is_some_and(|s| *s == "14")); }
for d in data {
assert!(tree.has(*d))
}
} }
} }

View file

@ -1,2 +1,3 @@
pub mod btree;
pub mod raw_vec; pub mod raw_vec;
pub mod vec; pub mod vec;