mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 12:58:42 +08:00
feat: add rust docs (#815)
* feat: add rust docs * Import std types for built_in_hash doc
This commit is contained in:
@ -126,7 +126,26 @@
|
||||
=== "Rust"
|
||||
|
||||
```rust title=""
|
||||
use std::rc::Rc;
|
||||
use std::cell::RefCell;
|
||||
|
||||
/* 二叉树节点类型 */
|
||||
struct TreeNode {
|
||||
val: i32, // 节点值
|
||||
left: Option<Rc<RefCell<TreeNode>>>, // 左子节点引用
|
||||
right: Option<Rc<RefCell<TreeNode>>>, // 右子节点引用
|
||||
}
|
||||
|
||||
impl TreeNode {
|
||||
/* 二叉树节点构造方法 */
|
||||
fn new(val: i32) -> Rc<RefCell<Self>> {
|
||||
Rc::new(RefCell::new(Self {
|
||||
val,
|
||||
left: None,
|
||||
right: None
|
||||
}))
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
@ -346,7 +365,17 @@
|
||||
=== "Rust"
|
||||
|
||||
```rust title="binary_tree.rs"
|
||||
|
||||
// 初始化节点
|
||||
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(n2.clone());
|
||||
n1.borrow_mut().right = Some(n3);
|
||||
n2.borrow_mut().left = Some(n4);
|
||||
n2.borrow_mut().right = Some(n5);
|
||||
```
|
||||
|
||||
=== "C"
|
||||
@ -487,7 +516,12 @@
|
||||
=== "Rust"
|
||||
|
||||
```rust title="binary_tree.rs"
|
||||
|
||||
let p = TreeNode::new(0);
|
||||
// 在 n1 -> n2 中间插入节点 P
|
||||
n1.borrow_mut().left = Some(p.clone());
|
||||
p.borrow_mut().left = Some(n2.clone());
|
||||
// 删除节点 p
|
||||
n1.borrow_mut().left = Some(n2);
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
Reference in New Issue
Block a user