datatypes: fix bst child access, when .root is 0 (#14080)

master
mjh 2022-04-20 06:49:18 -07:00 committed by GitHub
parent a1342e85c3
commit 1546645f63
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 19 deletions

View File

@ -113,7 +113,7 @@ fn (bst &BSTree<T>) contains_helper(node &BSTreeNode<T>, value T) bool {
// remove removes an element with `value` from the BST.
pub fn (mut bst BSTree<T>) remove(value T) bool {
if bst.root == 0 {
if bst.is_empty() {
return false
}
return bst.remove_helper(mut bst.root, value, false)
@ -153,6 +153,9 @@ fn (mut bst BSTree<T>) remove_helper(mut node BSTreeNode<T>, value T, left bool)
// get_max_from_right returns the max element of the BST following the right branch.
fn (bst &BSTree<T>) get_max_from_right(node &BSTreeNode<T>) &BSTreeNode<T> {
if node == 0 {
return new_none_node<T>(false)
}
right_node := node.right
if right_node == 0 || !right_node.is_init {
return node
@ -162,6 +165,9 @@ fn (bst &BSTree<T>) get_max_from_right(node &BSTreeNode<T>) &BSTreeNode<T> {
// get_min_from_left returns the min element of the BST by following the left branch.
fn (bst &BSTree<T>) get_min_from_left(node &BSTreeNode<T>) &BSTreeNode<T> {
if node == 0 {
return new_none_node<T>(false)
}
left_node := node.left
if left_node == 0 || !left_node.is_init {
return node
@ -251,6 +257,9 @@ fn (bst &BSTree<T>) get_node(node &BSTreeNode<T>, value T) &BSTreeNode<T> {
// left_value, exist := bst.to_left(10)
//```
pub fn (bst &BSTree<T>) to_left(value T) ?T {
if bst.is_empty() {
return none
}
node := bst.get_node(bst.root, value)
if !node.is_init {
return none
@ -267,6 +276,9 @@ pub fn (bst &BSTree<T>) to_left(value T) ?T {
// left_value, exist := bst.to_right(10)
//```
pub fn (bst &BSTree<T>) to_right(value T) ?T {
if bst.is_empty() {
return none
}
node := bst.get_node(bst.root, value)
if !node.is_init {
return none
@ -278,6 +290,9 @@ pub fn (bst &BSTree<T>) to_right(value T) ?T {
// 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 {
if bst.is_empty() {
return none
}
max := bst.get_max_from_right(bst.root)
if !max.is_init {
return none
@ -288,6 +303,9 @@ pub fn (bst &BSTree<T>) max() ?T {
// 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 {
if bst.is_empty() {
return none
}
min := bst.get_min_from_left(bst.root)
if !min.is_init {
return none

View File

@ -116,6 +116,7 @@ fn test_remove_from_bst_two() {
// check if we are able to get the max from the BST.
fn test_get_max_in_bst() {
mut bst := BSTree<int>{}
assert (bst.max() or { -1 }) == -1
assert bst.insert(10)
assert bst.insert(20)
assert bst.insert(21)
@ -127,6 +128,7 @@ fn test_get_max_in_bst() {
// check if we are able to get the min from the BST.
fn test_get_min_in_bst() {
mut bst := BSTree<int>{}
assert (bst.min() or { -1 }) == -1
assert bst.insert(10)
assert bst.insert(20)
assert bst.insert(21)

View File

@ -4,24 +4,24 @@ vlib/v/checker/tests/check_err_msg_with_generics.vv:15:10: error: cannot cast st
15 | println(int(typ))
| ~~~~~~~~
16 | }
vlib/datatypes/bstree.v:190:17: error: cannot append `T` to `[]T`
188 | }
189 | bst.in_order_traversal_helper(node.left, mut result)
190 | result << node.value
vlib/datatypes/bstree.v:196:17: error: cannot append `T` to `[]T`
194 | }
195 | bst.in_order_traversal_helper(node.left, mut result)
196 | result << node.value
| ~~~~~
191 | bst.in_order_traversal_helper(node.right, mut result)
192 | }
vlib/datatypes/bstree.v:210:17: error: cannot append `T` to `[]T`
208 | bst.post_order_traversal_helper(node.left, mut result)
209 | bst.post_order_traversal_helper(node.right, mut result)
210 | result << node.value
197 | bst.in_order_traversal_helper(node.right, mut result)
198 | }
vlib/datatypes/bstree.v:216:17: error: cannot append `T` to `[]T`
214 | bst.post_order_traversal_helper(node.left, mut result)
215 | bst.post_order_traversal_helper(node.right, mut result)
216 | result << node.value
| ~~~~~
211 | }
212 |
vlib/datatypes/bstree.v:226:17: error: cannot append `T` to `[]T`
224 | return
225 | }
226 | result << node.value
217 | }
218 |
vlib/datatypes/bstree.v:232:17: error: cannot append `T` to `[]T`
230 | return
231 | }
232 | result << node.value
| ~~~~~
227 | bst.pre_order_traversal_helper(node.left, mut result)
228 | bst.pre_order_traversal_helper(node.right, mut result)
233 | bst.pre_order_traversal_helper(node.left, mut result)
234 | bst.pre_order_traversal_helper(node.right, mut result)