diff --git a/src/btree.rs b/src/btree.rs index 4da5083..f096ab7 100644 --- a/src/btree.rs +++ b/src/btree.rs @@ -202,6 +202,30 @@ where self.insert_into_parent(new_key, new_internal_loc, path); } } + + pub fn dump_tree(&mut self) { + self.dump_node(self.header.root, 0); + } + + fn dump_node(&mut self, loc: usize, depth: usize) { + let indent = " ".repeat(depth); + + match self.storage.read_node(loc) { + Some(Node::Leaf(leaf)) => { + println!("{}Leaf [{}] keys={:?}", indent, loc, leaf.keys); + } + Some(Node::Internal(internal)) => { + println!("{}Internal [{}] keys={:?}", indent, loc, internal.keys); + + for &child in &internal.children { + self.dump_node(child, depth + 1); + } + } + _ => { + println!("{}", indent, loc); + } + } + } } #[cfg(test)] diff --git a/src/main.rs b/src/main.rs index bdf1e71..7b7b04b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -46,7 +46,8 @@ fn repl(tree: &mut BPlusTree) { break; } - if input == "help" { + if input == "tree" { + tree.dump_tree(); continue; }