mirror of
https://github.com/krahets/hello-algo.git
synced 2025-12-19 07:17:54 +08:00
feat: add rust docs (#815)
* feat: add rust docs * Import std types for built_in_hash doc
This commit is contained in:
@@ -99,7 +99,9 @@
|
||||
=== "Rust"
|
||||
|
||||
```rust title=""
|
||||
|
||||
/* 二叉树的数组表示 */
|
||||
// 使用 None 来标记空位
|
||||
let tree = [Some(1), Some(2), Some(3), Some(4), None, Some(6), Some(7), Some(8), Some(9), None, None, Some(12), None, None, Some(15)];
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
@@ -153,7 +153,28 @@ AVL 树既是二叉搜索树也是平衡二叉树,同时满足这两类二叉
|
||||
=== "Rust"
|
||||
|
||||
```rust title=""
|
||||
use std::rc::Rc;
|
||||
use std::cell::RefCell;
|
||||
|
||||
/* AVL 树节点类型 */
|
||||
struct TreeNode {
|
||||
val: i32, // 节点值
|
||||
height: i32, // 节点高度
|
||||
left: Option<Rc<RefCell<TreeNode>>>, // 左子节点
|
||||
right: Option<Rc<RefCell<TreeNode>>>, // 右子节点
|
||||
}
|
||||
|
||||
impl TreeNode {
|
||||
/* AVL 树节点构造方法 */
|
||||
fn new(val: i32) -> Rc<RefCell<Self>> {
|
||||
Rc::new(RefCell::new(Self {
|
||||
val,
|
||||
height: 0,
|
||||
left: None,
|
||||
right: None
|
||||
}))
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
@@ -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