【发布时间】:2023-01-13 10:57:02
【问题描述】:
我正在学习 Rust 并尝试实现一个简单的二叉搜索树(实际上它正在重写下面的 Java 实现)。这是我所做的:
use std::cmp::Ordering;
// Node of this BST, the two generic types are key and value
struct Node<K:Ord, V> {
key: K,
value: V,
left: Option<Box<Node<K, V>>>,
right: Option<Box<Node<K, V>>>,
number_of_nodes: i32,
}
impl<K: Ord, V> Node<K, V> {
// Create a new node
fn new(key: K, value: V, number_of_nodes: i32) -> Node<K, V>{
Node {
key,
value,
left: None,
right: None,
number_of_nodes,
}
}
}
struct BST<K: Ord ,V> {
root: Option<Box<Node<K, V>>>,
}
impl<K: Ord, V> BST<K, V> {
// Get the size of this BST
fn size(&self) -> i32 {
size(&self.root)
}
// Search for key. Update value if found, otherwise insert the new node
fn put(&self, key: K, value: V) {
self.root = put(&self.root, key, value)
}
}
// Function for recursively get the size of a sub BST
fn size<K: Ord, V>(node: &Option<Box<Node<K, V>>>) -> i32 {
match node {
Some(real_node) => real_node.number_of_nodes,
None => 0,
}
}
// Function for recursively put a new node to this BST
fn put<K: Ord, V>(node: &Option<Box<Node<K, V>>>, key: K, value: V) -> &Option<Box<Node<K, V>>>{
match node {
None => {
let new_node = Some(Box::new(Node::new(key, value, 1)));
return &new_node;
},
Some(real_node) => {
match key.cmp(&real_node.key) {
Ordering::Less => real_node.left = *put(&real_node.left, key, value),
Ordering::Greater => real_node.right = *put(&real_node.right, key, value),
Ordering::Equal => real_node.value = value,
}
real_node.number_of_nodes = size(&real_node.right) + size(&real_node.left) + 1;
node
},
}
}
但是这段代码无法编译,在 self.root = put(&self.root, key, value) 行,我得到一个错误:
不匹配的类型
找到预期的枚举“Option<Box<Node<K, V>>>” 参考 '&Option<Box<Node<K, V>>>'我不知道如何解决这个问题,我尝试将
&self参数更改为self,或将self.root更改为*self.root,但我遇到了更多错误。我对 Rust 中的引用感到很困惑,我只想用 Rust 重写以下 Java 代码。public class BST<Key extends Comparable<Key>, Value> { private Node root; //root of BST private class Node { private Key key; // key private Value val; // associated value private Node right, left; // left and right subtrees private int N; // number of nodes in subtree public Node(Key key, Value val, int N) { this.key = key; this.val = val; this.N = N; } } // Returns the number of key-value pairs in this symbol table. public int size() { return size(root); } // Return number of key-value pairs in BST rooted at x private int size(Node x) { if (x == null) return 0; else return x.N; } public void put(Key key, Value val) { root = put(root, key, val); } private Node put(Node x, Key key, Value val) { if (x == null) return new Node(key, val, 1); int cmp = key.compareTo(x.key); if (cmp < 0) x.left = put(x.left, key, val); else if (cmp > 0) x.right = put(x.right, key, val); else x.val = val; x.N = size(x.left) + size(x.right) + 1; return x; } }在
Java中非常简单,因为我不需要处理引用。所以这是我的问题:
- 我该如何修复不匹配的错误?
- 递归函数
put的正确返回类型是什么,&Option<Box<Node<K, V>>>或Option<Box<Node<K, V>>>?有什么不同?- 我重写
Java代码的方法是否正确? rust-analyzer 只报告了这个不匹配的错误,但我不知道它是否会像我预期的那样工作。老实说我不 完全理解当我处理 Rust 中的引用时我在做什么 特别是当它是 struct 或 enum 的引用时学习 Rust 很难,因为我在系统编程语言方面没有太多经验,感谢你们的帮助 :)
【问题讨论】:
标签: java algorithm rust binary-search-tree