const LEFT: &str = " ┌─";
const MID: &str = " │ ";
const RIGHT: &str = " └─";
const NULL: &str = "";
const BLANK: &str = " ";
pub trait ShowBinaryTree<P> {
fn get_left(&mut self, ptr: &P) -> Option<P>;
fn get_right(&mut self, ptr: &P) -> Option<P>;
fn get_root(&mut self) -> P;
fn print_node(&mut self, ptr: &P) -> String;
fn print_inner(&mut self, ptr: P, fill: &mut Vec<&'static str>, last: &'static str) {
let mut tmp = None;
if fill.last().is_some_and(|x| x == &last) {
tmp = fill.pop();
fill.push(BLANK);
} else if fill.last().is_some_and(|x| x != &NULL && x != &BLANK) {
tmp = fill.pop();
fill.push(MID);
}
fill.push(last);
if let Some(left) = Self::get_left(self, &ptr) {
self.print_inner(left, fill, LEFT);
}
eprintln!("│{} {}", fill.join(""), self.print_node(&ptr));
if let Some(right) = Self::get_right(self, &ptr) {
self.print_inner(right, fill, RIGHT);
}
fill.pop();
if let Some(tmp) = tmp {
fill.pop();
fill.push(tmp);
}
}
fn print_as_binary_tree(&mut self) {
#[cfg(debug_assertions)]
{
eprintln!("┌───────────────────────────────────────────────────────");
let root = Self::get_root(self);
Self::print_inner(self, root, &mut vec![], NULL);
eprintln!("└───────────────────────────────────────────────────────");
}
}
}