From 03f51120c17d34343ee4b55446cc2ba5d07a8b8b Mon Sep 17 00:00:00 2001 From: RibhiEl-Zaru Date: Thu, 24 Dec 2020 15:33:49 -0500 Subject: [PATCH] finished hw2 --- hw02/Cargo.toml | 13 ++++++ hw02/src/first.rs | 112 ++++++++++++++++++++++++++++++++++++++++++++++ hw02/src/lib.rs | 3 ++ hw02/src/main.rs | 3 ++ hw02/starter | 1 - return | 0 6 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 hw02/Cargo.toml create mode 100644 hw02/src/first.rs create mode 100644 hw02/src/lib.rs create mode 100644 hw02/src/main.rs delete mode 160000 hw02/starter create mode 100644 return diff --git a/hw02/Cargo.toml b/hw02/Cargo.toml new file mode 100644 index 0000000..af4e89e --- /dev/null +++ b/hw02/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "hw02" +version = "0.1.0" +authors = ["RibhiEl-Zaru "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +clippy = { version = "*", optional = true } + +[features] +default = [] diff --git a/hw02/src/first.rs b/hw02/src/first.rs new file mode 100644 index 0000000..997c6e2 --- /dev/null +++ b/hw02/src/first.rs @@ -0,0 +1,112 @@ +use std::mem; + +#[derive(Debug)] +pub struct BST { + head: Link +} + +#[derive(Debug)] +enum Link { + Empty, + More(Box), +} + +#[derive(Debug)] +struct Node { + elem: i32, + left: Link, + right: Link, +} + + +impl BST { + pub fn new() -> Self { + BST { head: Link::Empty } + } + + /* + Insert an element into the BST. Return + true if successful, or false if the element was already in the BST. + */ + pub fn insert(&mut self, num: i32) -> bool { + return self.head.insert(num); + } + + /* + Search for an element in the BST. Return + true iff the element was found. + */ + pub fn search(&self, num: i32) -> bool{ + return self.head.search(num); + } +} + + +impl Link { + pub fn insert(&mut self, num: i32)-> bool { + match self { + Link::Empty => { + *self = Link::More(Box::new(Node{ elem : num, left : Link::Empty, right : Link::Empty})); + return true; + } + Link::More(node) => { + if num == node.elem { + return false; + } + else if num > node.elem { + return node.right.insert(num); + } + else { + return node.left.insert(num); + } + } + } + } + + pub fn search(&self, num: i32) -> bool { + match self { + Link::Empty => { + return false; + } + Link::More(node) => { + if num == node.elem { + return true; + } + else if num > node.elem { + return node.right.search(num); + } + else { + return node.left.search(num); + } + } + } + } + +} + +#[cfg(test)] +mod test { + use super::BST; + + #[test] + fn test_BST() { + // ... + let mut bst = BST::new(); + assert!(bst.insert(15)); + assert!(bst.insert(2)); + assert!(bst.insert(20)); + + assert!(bst.search(2)); + assert!(bst.search(20)); + assert!(bst.search(15)); + assert!(!bst.search(2220)); + + + assert!(bst.insert(2220)); + assert!(bst.search(2220)); + + + println!("{:#?}", bst); + + } +} \ No newline at end of file diff --git a/hw02/src/lib.rs b/hw02/src/lib.rs new file mode 100644 index 0000000..0753587 --- /dev/null +++ b/hw02/src/lib.rs @@ -0,0 +1,3 @@ +#[cfg_attr(feature = "cargo-clippy", allow(needless_lifetimes))] + +pub mod first; \ No newline at end of file diff --git a/hw02/src/main.rs b/hw02/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/hw02/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/hw02/starter b/hw02/starter deleted file mode 160000 index 22cac43..0000000 --- a/hw02/starter +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 22cac43b99d164e9c76266151f689f46daf7ad7d diff --git a/return b/return new file mode 100644 index 0000000..e69de29