forked from cis198-2016s/homework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
131 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "hw02" | ||
version = "0.1.0" | ||
authors = ["RibhiEl-Zaru <[email protected]>"] | ||
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 = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
use std::mem; | ||
|
||
#[derive(Debug)] | ||
pub struct BST { | ||
head: Link | ||
} | ||
|
||
#[derive(Debug)] | ||
enum Link { | ||
Empty, | ||
More(Box<Node>), | ||
} | ||
|
||
#[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); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#[cfg_attr(feature = "cargo-clippy", allow(needless_lifetimes))] | ||
|
||
pub mod first; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
fn main() { | ||
println!("Hello, world!"); | ||
} |