feat: add rust codes for chapter heap (#612)

Co-authored-by: Yudong Jin <krahets@163.com>
This commit is contained in:
Night Cruising
2023-07-16 15:40:25 +08:00
committed by GitHub
parent ead33ca863
commit a296786b2a
5 changed files with 322 additions and 21 deletions

View File

@@ -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);
}
}