其他分享
首页 > 其他分享> > 【Rust】树04-二叉搜索树

【Rust】树04-二叉搜索树

作者:互联网

环境

前言

说明

基于标准库来学习各种数据结构,并不是从头实现数据结构,未考虑实现性能。
相比较二叉树,二叉搜索树的左节点都比父节点小,右节点都比父节点大。
基于二叉树来实现二叉搜索树,先实现插入和检索方法。

示例

引入模块

pub mod binary_search_tree;

结构定义

基于二叉树实现。

use std::cmp::Ordering;

use super::{binary_tree::BinaryTree, Node, NodeRef, Tree};

#[derive(Default)]
pub struct BinarySearchTree<T> {
    tree: BinaryTree<T>,
}

自定义方法

impl<T: Ord> BinarySearchTree<T> {
    pub(crate) fn root_mut(&mut self) -> &mut NodeRef<T> {
        self.tree.root_mut()
    }
    pub(crate) fn root(&self) -> &NodeRef<T> {
        self.tree.root()
    }
}

插入

fn insert(&mut self, value: T) {
    let mut current = self.root_mut();
    while let Some(node) = current {
        current = match value.cmp(&node.value) {
            Ordering::Less => &mut node.left,
            Ordering::Greater => &mut node.right,
            Ordering::Equal => return,
        };
    }
    *current = Node::new_node_ref(value)
}

检索

fn contains(&mut self, value: &T) -> bool {
    let mut current = self.root();
    while let Some(node) = current {
        current = match value.cmp(&node.value) {
            Ordering::Less => &node.left,
            Ordering::Greater => &node.right,
            Ordering::Equal => return true,
        };
    }
    false
}

其它方法

fn pre_order(&self) -> Vec<&T> {
    self.tree.pre_order()
}

fn in_order(&self) -> Vec<&T> {
    self.tree.in_order()
}

fn post_order(&self) -> Vec<&T> {
    self.tree.post_order()
}

总结

基于二叉树,实现了二叉搜索树的插入、检索方法。

附录

源码

use std::cmp::Ordering;

use super::{binary_tree::BinaryTree, Node, NodeRef, Tree};

#[derive(Default)]
pub struct BinarySearchTree<T> {
    tree: BinaryTree<T>,
}

impl<T: Ord> Tree<T> for BinarySearchTree<T> {
    fn pre_order(&self) -> Vec<&T> {
        self.tree.pre_order()
    }

    fn in_order(&self) -> Vec<&T> {
        self.tree.in_order()
    }

    fn post_order(&self) -> Vec<&T> {
        self.tree.post_order()
    }

    fn insert(&mut self, value: T) {
        let mut current = self.root_mut();
        while let Some(node) = current {
            current = match value.cmp(&node.value) {
                Ordering::Less => &mut node.left,
                Ordering::Greater => &mut node.right,
                Ordering::Equal => return,
            };
        }
        *current = Node::new_node_ref(value)
    }

    fn contains(&mut self, value: &T) -> bool {
        let mut current = self.root();
        while let Some(node) = current {
            current = match value.cmp(&node.value) {
                Ordering::Less => &node.left,
                Ordering::Greater => &node.right,
                Ordering::Equal => return true,
            };
        }
        false
    }
}

impl<T: Ord> BinarySearchTree<T> {
    pub(crate) fn root_mut(&mut self) -> &mut NodeRef<T> {
        self.tree.root_mut()
    }
    pub(crate) fn root(&self) -> &NodeRef<T> {
        self.tree.root()
    }
}

标签:node,mut,04,self,tree,value,二叉,current,Rust
来源: https://www.cnblogs.com/jiangbo4444/p/16536136.html