Page 1 of 1

C programming Solve BST Puzzles: Please help solve the puzzles listed below. The puzzle descriptions and functions are l

Posted: Sun May 15, 2022 1:13 pm
by answerhappygod
C programming
Solve BST Puzzles:
Please help solve the puzzles listed below. The puzzle
descriptions and functions are listed out below as well as the
testing file for the puzzles. The puzzles are:
1. Compute the height of a BST. The first puzzle problem is to
implement the function bst_height() to compute the height of a
given BST. Remember, the height of a tree is the maximum depth of
any node in the tree
2. Checking if a path sum is valid in a BST: The next
puzzle problem involves path sums. A path sum is the sum of all the
keys in a path from the BST root to one of the BST leaves. For this
problem, you'll implement the function bst_path_sum()to determine
whether a given value is a valid path sum within a given BST. In
other words, you should check whether the BST contains any path
from the root to a leaf where the keys sum to the specified
value
3. Compute a range sum in a BST. The last puzzle problem
involves computing the sum of all the keys in a BST within a given
range. Specifically, you should implement the function
bst_range_sum() to compute the sum of all keys in a BST between a
given lower bound and a given upper bound.
//bst puzzle functions:
#include <stdlib.h>
#include "bst.h"
#include "stack.h"
/*
* This function should return the height of a given BST,
which is the maximum
* depth of any node in the tree (i.e. the number of edges
in the path from
* the root to that node). Note that the height of an
empty tree is -1 by
* convention.
* Params: bst - the BST whose height is to be
computed
* Return: Should return the height of bst.
*/
int bst_height(struct bst* bst) {
//FIXHERE:
return 0;
}
/* This function should determine whether a specified value is a
valid path
* sum within a given BST. In other words, this
function should check whether
* the given BST contains any path from the root to a leaf
in which the keys
* sum to the specified value.
* Params:
* bst - the BST whose paths sums to search
* sum - the value to search for among the path sums
of `bst`
* Return: Should return 1 if `bst` contains any path
from the root to a leaf in
* which the keys add up to `sum`. Should
return 0 otherwise.*/
int bst_path_sum(struct bst* bst, int sum) {
// FIX:
return 0;
}
/* This function should compute a range sum in a given BST.
Specifically, it
* should compute the sum of all keys in the BST between a
given lower bound
* and a given upper bound. For full credit, you
should not process any subtree
* whose keys cannot be included in the range sum.
* Params: bst - the BST within which to
compute a range sum
* lower - the lower bound of the range over which
to compute a sum; this
* should be considered an *inclusive* bound;
in other words a key that's
* equal to this bound should be included in
the sum
* upper - the upper bound of the range over which
to compute a sum; this
* should be considered an *inclusive* bound;
in other words a key that's
* equal to this bound should be included in
the sum
* Return: Should return the sum of all keys in `bst`
between `lower` and `upper`.*/
int bst_range_sum(struct bst* bst, int lower, int upper) {
// FIX:
return 0;
}