| Crates.io | binary-search-tree-visualizer |
| lib.rs | binary-search-tree-visualizer |
| version | 0.1.0 |
| created_at | 2025-05-26 19:07:41.163432+00 |
| updated_at | 2025-05-26 19:07:41.163432+00 |
| description | A crate that provides visualization tools for binary search trees, including ASCII art and SVG generation. Great for educational purposes and debugging. |
| homepage | https://github.com/nyakiomaina/binary-search-tree-visualizer |
| repository | https://github.com/nyakiomaina/binary-search-tree-visualizer |
| max_upload_size | |
| id | 1690147 |
| size | 19,318 |
A Rust crate that provides visualization tools for binary search trees, including both ASCII art and SVG generation. This crate is great for educational purposes and debugging binary search tree implementations.
Ord and Display traitsAdd this to your Cargo.toml:
[dependencies]
binary-search-tree-visualizer = "0.1.0"
Or use:
cargo add binary-search-tree-visualizer
Here's a simple example of how to use the crate:
use binary_search_tree_visualizer::{BinarySearchTree, AsciiVisualizer, SvgVisualizer};
use binary_search_tree_visualizer::visualizer::TreeVisualizer;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a binary search tree
let mut tree = BinarySearchTree::new();
// Insert some values
tree.insert(5);
tree.insert(3);
tree.insert(7);
tree.insert(1);
tree.insert(9);
// Generate ASCII visualization
let ascii_viz = AsciiVisualizer;
let ascii_output = ascii_viz.visualize(&tree)?;
println!("{}", ascii_output);
// Generate SVG visualization
let svg_viz = SvgVisualizer::default();
let svg_output = svg_viz.visualize(&tree)?;
// Save SVG to file
std::fs::write("tree.svg", svg_output)?;
Ok(())
}
└── 5
┌── 7
│ ┌── 9
└── 3
└── 1
The SVG visualization will be saved to a file and can be viewed in any web browser or SVG-compatible viewer.
The SvgVisualizer can be customized with different parameters:
let svg_viz = SvgVisualizer {
node_radius: 25.0, // Size of node circles
level_height: 80.0, // Vertical spacing between levels
horizontal_spacing: 50.0, // Horizontal spacing between nodes
};
This project is licensed under the MIT License - see the LICENSE file for details.