btree
This commit is contained in:
parent
dd51d8d1d1
commit
325c491691
2 changed files with 55 additions and 57 deletions
|
@ -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 {
|
||||||
impl<K: Ord, V> BTree<K, V> {
|
keys: match keys {
|
||||||
pub fn new() -> Self {
|
Some(keys) => keys,
|
||||||
Self { root: None, len: 0 }
|
None => Vec::with_capacity(degree - 1),
|
||||||
}
|
},
|
||||||
|
children: match children {
|
||||||
pub fn clear(&mut self) {
|
Some(children) => children,
|
||||||
self.root = None;
|
None => Vec::with_capacity(degree),
|
||||||
self.len = 0;
|
},
|
||||||
}
|
|
||||||
|
|
||||||
pub fn rebalance() {
|
|
||||||
todo!()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn insert(&mut self, key: K, value: V) -> Option<V> {
|
|
||||||
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> {
|
fn is_leaf(&self) -> bool {
|
||||||
|
self.children.is_empty()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Ord> BTree<T> {
|
||||||
|
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!()
|
todo!()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn has_key(&self, key: K) -> bool {
|
pub fn insert(&mut self, value: T) -> Option<T> {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get(&self, key: K) -> Option<&V> {
|
pub fn has(&self, value: T) -> bool {
|
||||||
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))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,2 +1,3 @@
|
||||||
|
pub mod btree;
|
||||||
pub mod raw_vec;
|
pub mod raw_vec;
|
||||||
pub mod vec;
|
pub mod vec;
|
||||||
|
|
Loading…
Add table
Reference in a new issue