This commit is contained in:
krahets
2023-10-03 03:49:55 +08:00
parent 256ce30a95
commit 49ad0a6422
12 changed files with 166 additions and 16 deletions
+3 -1
View File
@@ -107,7 +107,9 @@ comments: true
=== "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"
+21
View File
@@ -161,7 +161,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 {
/* 构造方法 */
fn new(val: i32) -> Rc<RefCell<Self>> {
Rc::new(RefCell::new(Self {
val,
height: 0,
left: None,
right: None
}))
}
}
```
=== "C"
+37 -3
View File
@@ -62,7 +62,7 @@ comments: true
Left *TreeNode
Right *TreeNode
}
/* 节点初始化方法 */
/* 构造方法 */
func NewTreeNode(v int) *TreeNode {
return &TreeNode{
Left: nil, // 左子节点指针
@@ -135,7 +135,26 @@ comments: true
=== "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"
@@ -359,7 +378,17 @@ comments: true
=== "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"
@@ -502,7 +531,12 @@ comments: true
=== "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"