Files
hello-algo/codes/rust/chapter_tree/binary_tree.rs
T
Yudong JinandClaude Opus 5.5 849b6a95b9 Cite demo-source regions from doc snippets (pilot) (#2034)
Doc snippets for init_linked_list, init_binary_tree, stack_operations and
five list.md entries now reference marked regions of the demo programs
instead of copying code, so the book shows code that is compiled and run.

- Mark regions in the zh demo files and add the snippet comments that the
  demos lacked, reusing texts that already have reviewed translations.
- Sync ru by hand, regenerate en/ja/ko from the reviewed dictionaries and
  derive zh-hant with OpenCC.
- Convert 97 snippet blocks in six editions to [file]{..}-[region]{..}
  references; init_list and the Rust stack block stay literal until their
  comments have reviewed translations.

Co-authored-by: Claude Opus 5.5 <[email protected]>
2026-11-16 03:36:04 +08:00

41 lines
1.2 KiB
Rust

/**
* File: binary_tree.rs
* Created Time: 2023-02-27
* Author: xBLACKICEx ([email protected])
*/
use std::rc::Rc;
use hello_algo_rust::include::{print_util, TreeNode};
/* Driver Code */
fn main() {
// [region]{init_binary_tree}
/* 初始化二叉树 */
// 初始化节点
let n1 = TreeNode::new(1);
let n2 = TreeNode::new(2);
let n3 = TreeNode::new(3);
let n4 = TreeNode::new(4);
let n5 = TreeNode::new(5);
// 构建节点之间的引用(指针)
n1.borrow_mut().left = Some(Rc::clone(&n2));
n1.borrow_mut().right = Some(Rc::clone(&n3));
n2.borrow_mut().left = Some(Rc::clone(&n4));
n2.borrow_mut().right = Some(Rc::clone(&n5));
// [endregion]{init_binary_tree}
println!("\n初始化二叉树\n");
print_util::print_tree(&n1);
// 插入节点与删除节点
let p = TreeNode::new(0);
// 在 n1 -> n2 中间插入节点 P
p.borrow_mut().left = Some(Rc::clone(&n2));
n1.borrow_mut().left = Some(Rc::clone(&p));
println!("\n插入节点 P 后\n");
print_util::print_tree(&n1);
// 删除节点 P
drop(p);
n1.borrow_mut().left = Some(Rc::clone(&n2));
println!("\n删除节点 P 后\n");
print_util::print_tree(&n1);
}