The document discusses binary search trees and algorithms for searching within a range of values in a binary search tree. It provides pseudocode for a recursive BST_Find_Range function that takes a node, lower bound, and upper bound as parameters. The function checks if the node's value falls within the range, and recursively searches the left or right subtree depending on if the value is less than or greater than the lower and upper bounds. Examples are given showing insertion and deletion of nodes in binary search trees.
The document discusses binary search trees and algorithms for searching within a range of values in a binary search tree. It provides pseudocode for a recursive BST_Find_Range function that takes a node, lower bound, and upper bound as parameters. The function checks if the node's value falls within the range, and recursively searches the left or right subtree depending on if the value is less than or greater than the lower and upper bounds. Examples are given showing insertion and deletion of nodes in binary search trees.
The document discusses binary search trees and algorithms for searching within a range of values in a binary search tree. It provides pseudocode for a recursive BST_Find_Range function that takes a node, lower bound, and upper bound as parameters. The function checks if the node's value falls within the range, and recursively searches the left or right subtree depending on if the value is less than or greater than the lower and upper bounds. Examples are given showing insertion and deletion of nodes in binary search trees.
Comp171: Data Structure and Algorithm Written Assignment 2 Solution
1. Main idea: We will use the property of binary search tree to do the range search. For a certain node in the binary search tree, we check whether the value v stored in the node n falls into the range and if it does we report it. If (v<i) we know that we dont have to check the left child of n, as all the values under the left child are smaller than v, therefore smaller than i. Similarly, if (v>j), we dont have to check the right child of n. We can use the following figure to show our strategy. Pseudo code: We define a function void BST_Find_Range(BSTnode* bn, int i, int j) to find all the nodes that contain value which falls between the range [i, j] in a binary search tree with the root node bn. void BST_Find_Range(BSTnode* bn, int i, int j) { if (bn= =NULL) return; // if bn is a null node, do nothing. int value=bn->value; // retrieve the value stored in the node bn. if (value < i) BST_Find_Range(bn->right, i, j) // if value<i, we only have to check the right child of bn. if (value > j) BST_Find_Range(bn->left, i, j) // if value>j, we only have to check the left child of bn. else { // the value falls within the range, we report it and we have to check both of its left and right child. output(value); BST_Find_Range(bn->left, i, j); BST_Find_Range(bn->right, i, j) } }