创建可变二叉树时,键入名称T undefined

bl4*_*0ne -1 tree binary-tree pointers rust data-structures

我是Rust的新手,为了练习,我正在构建一个简单的通用二叉树.这就是我在C++中创建一个的方法

template<typename T>
struct Node
{
    T data;
    Node<T>* parent;
    Node<T>* left;
    Node<T>* right;
};

template<typename T>
struct Bintree
{
    Node<T>* root;  
};
Run Code Online (Sandbox Code Playgroud)

但Rust中的相同(ish)代码似乎不起作用:

use std::ptr;

struct Node<T> {
    data: T,
    left: &Node<T>,
    right: &Node<T>,
    parent: &Node<T>,
}

struct Tree<T> {
    root: &Node<T>,
}

impl Tree<T> {
    pub fn new() -> Tree<T> {
        Tree { root: ptr::null() }
    }

    pub fn insert(&self, value: T) {
        if root.is_null() {
            self.root = Node {
                data: value,
                left: ptr::null(),
                right: ptr::null(),
                parent: ptr::null(),
            };
        }
    }
}

fn main() {
    println!("Hello, world!");
}
Run Code Online (Sandbox Code Playgroud)

这是错误:

error[E0412]: type name `T` is undefined or not in scope
  --> src/main.rs:14:15
   |
14 |     impl Tree<T> {
   |               ^ undefined or not in scope
   |
   = help: no candidates by the name of `T` found in your project; maybe you misspelled the name or forgot to import an external crate?

error[E0412]: type name `T` is undefined or not in scope
  --> src/main.rs:15:30
   |
15 |         pub fn new() -> Tree<T> {
   |                              ^ undefined or not in scope
   |
   = help: no candidates by the name of `T` found in your project; maybe you misspelled the name or forgot to import an external crate?

error[E0412]: type name `T` is undefined or not in scope
  --> src/main.rs:19:37
   |
19 |         pub fn insert(&self, value: T) {
   |                                     ^ undefined or not in scope
   |
   = help: no candidates by the name of `T` found in your project; maybe you misspelled the name or forgot to import an external crate?

error[E0425]: unresolved name `root`. Did you mean `self.root`?
  --> src/main.rs:20:16
   |
20 |             if root.is_null() {
   |                ^^^^

error[E0106]: missing lifetime specifier
 --> src/main.rs:5:15
  |
5 |         left: &Node<T>,
  |               ^ expected lifetime parameter

error[E0106]: missing lifetime specifier
 --> src/main.rs:6:16
  |
6 |         right: &Node<T>,
  |                ^ expected lifetime parameter

error[E0106]: missing lifetime specifier
 --> src/main.rs:7:17
  |
7 |         parent: &Node<T>,
  |                 ^ expected lifetime parameter

error[E0106]: missing lifetime specifier
  --> src/main.rs:11:15
   |
11 |         root: &Node<T>,
   |               ^ expected lifetime parameter
Run Code Online (Sandbox Code Playgroud)

我真的不明白这有什么问题.我真的不明白Rust的指针是如何运作的.

Ste*_*nik 7

在这种情况下,您应该有一个基本的语法错误

impl<T> Tree<T>
Run Code Online (Sandbox Code Playgroud)

从那里,你会看到你需要的if self.root.is_null().

然后,数据结构需要生命周期说明符,因为您正在使用引用.使用最直接的语法最终会导致

error[E0309]: the parameter type `T` may not live long enough
Run Code Online (Sandbox Code Playgroud)

所以你T: 'a在那里使用......你最终得到:

use std::ptr;

struct Node<'a, T: 'a> {
    data: T,
    left: &'a Node<'a, T>,
    right: &'a Node<'a, T>,
    parent: &'a Node<'a, T>,
}

struct Tree<'a, T: 'a> {
    root: &'a Node<'a, T>,
}

impl<'a, T> Tree<'a, T> {
    pub fn new() -> Tree<'a, T> {
        Tree { root: ptr::null() }
    }

    pub fn insert(&self, value: T) {
        if self.root.is_null() {
            self.root = Node {
                data: value,
                left: ptr::null(),
                right: ptr::null(),
                parent: ptr::null(),
            };
        }
    }
}

fn main() {
    println!("Hello, world!");
}
Run Code Online (Sandbox Code Playgroud)

这给出了另一个错误

21 |             root: ptr::null(),
   |                   ^^^^^^^^^^^ expected reference, found *-ptr
Run Code Online (Sandbox Code Playgroud)

这是因为ptr::null()返回原始指针,但您已声明您的数据结构使用引用.

好的,那就是我要去的地方.让我们回到你的问题......

我是Rust的新手,为了练习,我正在构建一个简单的通用二叉树.

我建议您除了编写数据结构外还应考虑其他事项.他们在Rust中并不简单.如果您仍想使用此方法,我可以推荐太多列表.