mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-05 11:41:22 +08:00
feat: add rust codes for chapter heap (#612)
Co-authored-by: Yudong Jin <krahets@163.com>
This commit is contained in:
@@ -10,7 +10,7 @@ use std::collections::{HashMap, VecDeque};
|
||||
use std::rc::Rc;
|
||||
|
||||
use crate::list_node::ListNode;
|
||||
use crate::tree_node::TreeNode;
|
||||
use crate::tree_node::{TreeNode, vec_to_tree};
|
||||
|
||||
struct Trunk<'a, 'b> {
|
||||
prev: Option<&'a Trunk<'a, 'b>>,
|
||||
@@ -90,3 +90,12 @@ fn show_trunks(trunk: Option<&Trunk>) {
|
||||
print!("{}", trunk.str.get());
|
||||
}
|
||||
}
|
||||
|
||||
/* Print a heap both in array and tree representations */
|
||||
pub fn print_heap(heap: Vec<i32>) {
|
||||
println!("堆的数组表示:{:?}", heap);
|
||||
println!("堆的树状表示:");
|
||||
if let Some(root) = vec_to_tree(heap.into_iter().map(|val| Some(val)).collect()) {
|
||||
print_tree(&root);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user