小编bro*_*die的帖子

实现二叉搜索树时不能多次借用节点作为可变节点

我正在尝试在 Rust 中实现二叉搜索树,但在插入元素时遇到了问题。在 Rust 中这样做的惯用方法是什么?

这是我的实现:

use std::cmp::Ordering;

pub struct BinarySearchTree {
    root: OptNode,
    size: u32,
}

type OptNode = Option<Box<Node>>;

struct Node {
    key: i32,
    left: OptNode,
    right: OptNode,
}

impl BinarySearchTree {
    pub fn new() -> Self {
        BinarySearchTree {
            root: None,
            size: 0,
        }
    }

    pub fn is_empty(&self) -> bool {
        self.size == 0
    }

    pub fn size(&self) -> u32 {
        self.size
    }

    pub fn contains(&self, key: i32) -> bool {
        let mut node = &self.root;
        while …
Run Code Online (Sandbox Code Playgroud)

binary-search-tree rust

5
推荐指数
1
解决办法
687
查看次数

标签 统计

binary-search-tree ×1

rust ×1