datatypes: fixed v code style
Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>pull/13453/head
parent
0fef547c1d
commit
43e31aa9bd
|
|
@ -250,13 +250,13 @@ fn (bst &BSTree<T>) get_node(node &BSTreeNode<T>, value T) &BSTreeNode<T> {
|
|||
//```v
|
||||
// left_value, exist := bst.to_left(10)
|
||||
//```
|
||||
pub fn (bst &BSTree<T>) to_left(value T) (T, bool) {
|
||||
pub fn (bst &BSTree<T>) to_left(value T) ?T {
|
||||
node := bst.get_node(bst.root, value)
|
||||
if !node.is_init {
|
||||
return node.value, false
|
||||
return none
|
||||
}
|
||||
left_node := node.left
|
||||
return left_node.value, left_node.is_init
|
||||
return left_node.value
|
||||
}
|
||||
|
||||
// to_right return the value of the element to the right of the node with `value` specified, if exist
|
||||
|
|
@ -266,25 +266,31 @@ pub fn (bst &BSTree<T>) to_left(value T) (T, bool) {
|
|||
//```v
|
||||
// left_value, exist := bst.to_right(10)
|
||||
//```
|
||||
pub fn (bst &BSTree<T>) to_right(value T) (T, bool) {
|
||||
pub fn (bst &BSTree<T>) to_right(value T) ?T {
|
||||
node := bst.get_node(bst.root, value)
|
||||
if !node.is_init {
|
||||
return node.value, false
|
||||
return none
|
||||
}
|
||||
right_node := node.right
|
||||
return right_node.value, right_node.is_init
|
||||
return right_node.value
|
||||
}
|
||||
|
||||
// max return the max element inside the BST.
|
||||
// Time complexity O(N) if the BST is not balanced
|
||||
pub fn (bst &BSTree<T>) max() (T, bool) {
|
||||
pub fn (bst &BSTree<T>) max() ?T {
|
||||
max := bst.get_max_from_right(bst.root)
|
||||
return max.value, max.is_init
|
||||
if !max.is_init {
|
||||
return none
|
||||
}
|
||||
return max.value
|
||||
}
|
||||
|
||||
// min return the minimum element in the BST.
|
||||
// Time complexity O(N) if the BST is not balanced.
|
||||
pub fn (bst &BSTree<T>) min() (T, bool) {
|
||||
pub fn (bst &BSTree<T>) min() ?T {
|
||||
min := bst.get_min_from_left(bst.root)
|
||||
return min.value, min.is_init
|
||||
if !min.is_init {
|
||||
return none
|
||||
}
|
||||
return min.value
|
||||
}
|
||||
|
|
|
|||
|
|
@ -67,12 +67,10 @@ fn test_get_left_root() {
|
|||
assert bst.insert(21)
|
||||
assert bst.insert(1)
|
||||
|
||||
left_val, found_left := bst.to_left(10)
|
||||
assert found_left
|
||||
left_val := bst.to_left(10) or { -1 }
|
||||
assert left_val == 1
|
||||
|
||||
right_val, found_right := bst.to_right(10)
|
||||
assert found_right
|
||||
right_val := bst.to_right(10) or { -1 }
|
||||
assert right_val == 20
|
||||
}
|
||||
|
||||
|
|
@ -80,11 +78,11 @@ fn test_get_left_root() {
|
|||
fn test_get_left_on_empty_bst() {
|
||||
mut bst := BSTree<int>{}
|
||||
|
||||
left_val, found_left := bst.to_left(10)
|
||||
assert found_left == false
|
||||
left_val := bst.to_left(10) or { -1 }
|
||||
assert left_val == -1
|
||||
|
||||
right_val, found_right := bst.to_right(10)
|
||||
assert found_right == false
|
||||
right_val := bst.to_right(10) or { -1 }
|
||||
assert right_val == -1
|
||||
}
|
||||
|
||||
// Check the remove operation if it is able to remove
|
||||
|
|
@ -122,8 +120,7 @@ fn test_get_max_in_bst() {
|
|||
assert bst.insert(20)
|
||||
assert bst.insert(21)
|
||||
assert bst.insert(1)
|
||||
max, found := bst.max()
|
||||
assert found
|
||||
max := bst.max() or { -1 }
|
||||
assert max == 21
|
||||
}
|
||||
|
||||
|
|
@ -134,7 +131,6 @@ fn test_get_min_in_bst() {
|
|||
assert bst.insert(20)
|
||||
assert bst.insert(21)
|
||||
assert bst.insert(1)
|
||||
min, found := bst.min()
|
||||
assert found
|
||||
min := bst.min() or { -1 }
|
||||
assert min == 1
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue