MST

星途 面试题库

面试题:Rust函数指针在树状数据结构中的高效应用

设计一个Rust的二叉搜索树(BST)数据结构,其中插入、删除和查找操作使用函数指针来实现自定义的比较逻辑。这样,用户可以根据不同的需求(例如按不同字段比较)灵活调整树的行为。要求给出完整的`BST`结构体定义及相关方法实现代码,并简要说明函数指针在该设计中的优势。
39.3万 热度难度
编程语言Rust

知识考点

AI 面试

面试题答案

一键面试
// 定义BST节点
struct BSTNode<T> {
    value: T,
    left: Option<Box<BSTNode<T>>>,
    right: Option<Box<BSTNode<T>>>,
}

// 定义BST结构体
struct BST<T> {
    root: Option<Box<BSTNode<T>>>,
    compare: fn(&T, &T) -> std::cmp::Ordering,
}

impl<T> BST<T> {
    // 初始化BST
    fn new(compare: fn(&T, &T) -> std::cmp::Ordering) -> Self {
        BST {
            root: None,
            compare,
        }
    }

    // 插入节点
    fn insert(&mut self, value: T) {
        let mut current = &mut self.root;
        while let Some(node) = current {
            match (self.compare)(&value, &node.value) {
                std::cmp::Ordering::Less => current = &mut node.left,
                std::cmp::Ordering::Greater => current = &mut node.right,
                std::cmp::Ordering::Equal => return,
            }
        }
        *current = Some(Box::new(BSTNode {
            value,
            left: None,
            right: None,
        }));
    }

    // 查找节点
    fn find(&self, value: &T) -> bool {
        let mut current = &self.root;
        while let Some(node) = current {
            match (self.compare)(value, &node.value) {
                std::cmp::Ordering::Less => current = &node.left,
                std::cmp::Ordering::Greater => current = &node.right,
                std::cmp::Ordering::Equal => return true,
            }
        }
        false
    }

    // 删除节点
    fn delete(&mut self, value: &T) {
        self.root = self.delete_node(self.root.take(), value);
    }

    fn delete_node(&self, mut node: Option<Box<BSTNode<T>>>, value: &T) -> Option<Box<BSTNode<T>>> {
        if let Some(mut node) = node {
            match (self.compare)(value, &node.value) {
                std::cmp::Ordering::Less => {
                    node.left = self.delete_node(node.left.take(), value);
                    Some(node)
                }
                std::cmp::Ordering::Greater => {
                    node.right = self.delete_node(node.right.take(), value);
                    Some(node)
                }
                std::cmp::Ordering::Equal => {
                    if node.left.is_none() {
                        node.right
                    } else if node.right.is_none() {
                        node.left
                    } else {
                        let mut min_node = node.right.take().unwrap();
                        while let Some(ref mut left) = min_node.left {
                            min_node = left.take().unwrap();
                        }
                        node.value = min_node.value;
                        node.right = self.delete_node(node.right.take(), &node.value);
                        Some(node)
                    }
                }
            }
        } else {
            None
        }
    }
}

函数指针在该设计中的优势:

  1. 灵活性:用户可以根据不同的需求提供不同的比较逻辑,例如按不同字段比较,从而实现多种排序和查找策略。
  2. 代码复用BST的基本结构和插入、删除、查找逻辑可以复用,仅通过改变比较函数来适应不同的数据类型和比较需求,减少重复代码。
  3. 扩展性:方便在后续开发中根据新的业务需求添加新的比较逻辑,而无需修改BST的核心实现代码。