From 325c491691117f65b2ddb2189f9efa327647d90a Mon Sep 17 00:00:00 2001 From: cscherr Date: Wed, 4 Jun 2025 18:06:01 +0200 Subject: [PATCH] btree --- crates/datastructures/src/btree.rs | 111 ++++++++++++++--------------- crates/datastructures/src/lib.rs | 1 + 2 files changed, 55 insertions(+), 57 deletions(-) diff --git a/crates/datastructures/src/btree.rs b/crates/datastructures/src/btree.rs index 7d64f34..1a50e88 100644 --- a/crates/datastructures/src/btree.rs +++ b/crates/datastructures/src/btree.rs @@ -1,66 +1,61 @@ -#[derive(Clone, Debug, Default)] -pub struct BTree { - root: Option>, - len: usize, +use crate::vec::Vec; + +#[derive(Clone, Debug)] +pub struct BTree { + root: Node, + degree: usize, } #[derive(Clone, Debug)] -pub struct Node { - key: K, - value: V, +struct Node { + keys: Vec, + children: Vec>, } -impl Node { - pub fn new_root(key: K, value: V) -> Self { - Node { key, value } - } -} - -impl BTree { - pub fn new() -> Self { - Self { root: None, len: 0 } - } - - pub fn clear(&mut self) { - self.root = None; - self.len = 0; - } - - pub fn rebalance() { - todo!() - } - - pub fn insert(&mut self, key: K, value: V) -> Option { - if let Some(root) = &self.root { - todo!() - } else { - let new_node = Node::new_root(key, value); - self.root = Some(new_node); - None +impl Node +where + T: Ord, +{ + fn new(degree: usize, keys: Option>, children: Option>>) -> 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), + }, } } - pub fn remove(&mut self, key: K) -> Option { + fn is_leaf(&self) -> bool { + self.children.is_empty() + } +} + +impl BTree { + pub fn new(branch_factor: usize) -> Self { + let degree = 2 * branch_factor; + Self { + root: Node::new(degree, None, None), + degree, + } + } + + pub fn clear(&mut self) { + self.root = Node::new(self.degree, None, None); + } + + fn rebalance() { todo!() } - pub fn has_key(&self, key: K) -> bool { + pub fn insert(&mut self, value: T) -> Option { 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> { - todo!() - } - - pub fn get_node_mut(&mut self, key: K) -> Option<&mut Node> { + pub fn has(&self, value: T) -> bool { todo!() } } @@ -70,18 +65,20 @@ mod test { use super::*; #[test] fn test_create() { - let _tree = BTree::::default(); + let _tree = BTree::::new(); } #[test] fn test_insert() { - let mut tree = BTree::::default(); - tree.insert(19, "19"); - tree.insert(9, "09"); - tree.insert(14, "14"); + let mut tree = BTree::::new(); + let data = &[19, 125, 25, 16, 2, 73, 384, 435, 12924, 42, 125251, 2548]; - assert!(tree.get(19).is_some_and(|s| *s == "19")); - assert!(tree.get(9).is_some_and(|s| *s == "09")); - assert!(tree.get(14).is_some_and(|s| *s == "14")); + for d in data { + assert!(tree.insert(*d).is_none()) + } + + for d in data { + assert!(tree.has(*d)) + } } } diff --git a/crates/datastructures/src/lib.rs b/crates/datastructures/src/lib.rs index 9ff8ce3..b895c26 100644 --- a/crates/datastructures/src/lib.rs +++ b/crates/datastructures/src/lib.rs @@ -1,2 +1,3 @@ +pub mod btree; pub mod raw_vec; pub mod vec;