fix: update the node before returning it as the root in insert()

This commit is contained in:
nazeh
2023-12-18 19:16:38 +03:00
parent 60ff54a651
commit 44ee478d38
2 changed files with 18 additions and 18 deletions

View File

@@ -12,29 +12,39 @@ mod test {
self.build_graph_string(&root, &mut graph);
}
graph.push_str(&format!(
" classDef null fill:#1111,stroke-width:1px,color:#fff,stroke-dasharray: 5 5;\n"
));
graph
}
fn build_graph_string(&self, node: &Node, graph: &mut String) {
let key = bytes_to_string(node.key());
let node_label = format!("{}({})", key, key);
let node_label = format!("{}(({}))", node.hash(), key);
graph.push_str(&format!(" {};\n", node_label));
if let Some(child) = self.get_node(node.left()) {
let key = bytes_to_string(child.key());
let child_label = format!("{}({})", key, key);
let child_label = format!("{}(({}))", child.hash(), key);
graph.push_str(&format!(" {} --> {};\n", node_label, child_label));
self.build_graph_string(&child, graph);
} else {
graph.push_str(&format!(" {} -.-> {}l((l));\n", node_label, node.hash()));
graph.push_str(&format!(" class {}l null;\n", node.hash()));
}
if let Some(child) = self.get_node(node.right()) {
let key = bytes_to_string(child.key());
let child_label = format!("{}({})", key, key);
let child_label = format!("{}(({}))", child.hash(), key);
graph.push_str(&format!(" {} --> {};\n", node_label, child_label));
self.build_graph_string(&child, graph);
} else {
graph.push_str(&format!(" {} -.-> {}r((r));\n", node_label, node.hash()));
graph.push_str(&format!(" class {}r null;\n", node.hash()));
}
}
}

View File

@@ -25,12 +25,8 @@ impl<'a> HashTreap<'a> {
let value = self.insert_blob(value);
let mut node = Node::new(key, value);
println!(
"\n New insert {:?}",
String::from_utf8(key.to_vec()).unwrap()
);
if self.root.is_none() {
node.update(self.storage);
self.update_root(*node.hash());
return;
}
@@ -97,12 +93,6 @@ impl<'a> HashTreap<'a> {
}
};
}
dbg!((
"Out of the first loop",
&top_path,
&left_unzip_path,
&right_unzip_path
));
// === Updating hashes bottom up ===
@@ -150,7 +140,8 @@ impl<'a> HashTreap<'a> {
// I
node.set_child(&Branch::Left, left_unzip_path.first().map(|n| *n.hash()));
node.set_child(&Branch::Right, left_unzip_path.first().map(|n| *n.hash()));
node.set_child(&Branch::Right, right_unzip_path.first().map(|n| *n.hash()));
// No more updates lower than the new node, save it to storage.
node.update(self.storage);
// Update the rest of the path upwards with the new hashes.
@@ -248,11 +239,11 @@ mod test {
let mut storage = MemoryStorage::new();
let mut treap = HashTreap::new(&mut storage);
// let mut keys = ["A", "C", "D", "F", "G", "H", "M", "P", "X", "Y"];
let mut keys = ["A", "C", "D", "F", "G", "H", "M", "P", "X", "Y"];
let mut keys = [
"D", "N", "P", "X", "F", "Z", "Y", "A", "G", "C", "M", "H", "I", "J",
];
// let mut keys = ["A", "B", "C"];
let mut keys = ["A", "B", "C"];
// keys.reverse();
// keys.reverse(); // Overflowing stack! damn recursion.
@@ -261,7 +252,6 @@ mod test {
}
assert!(treap.verify_ranks());
// dbg!(&tree);
println!("{}", treap.as_mermaid_graph())
}
}