Modify this code and add a function in the program called “Display_less”. Function should ask user to enter a number then display all elements from the tree that are less than or equal to the number. Note that the number may not be present in the tree. #include using namespace std; /* To represent a node of Binary Search Tree*/ struct node{ int value; struct node *left, *right; node(int value){ this->value = value; // To initialize the node's value of BST with given value this->left = this->right = NULL; // To initialize left and right pointers of this node as NULL } }; /* createBST() to insert value in a Binary Search Tree*/ struct node * createBST(struct node *root, int value){ if (root == NULL){ // If root is NULL return new node(value); // Create a new node and return this node } if (value < root->value) // if given value is less than root's value then Go left in BST root->left = createBST(root->left, value); else // if given value is more than root's value then Go right in BST root->right = createBST(root->right, value); return root; // Returns the root } // To display the elements of the tree in In-order form void inorder(struct node *root){ if(root != NULL){ inorder(root->left); cout << root->value << " "; inorder(root->right); } } // To display the elements of the tree in Post-order form void postorder(struct node *root){ if(root != NULL){ postorder(root->left); postorder(root->right); cout << root->value << " "; } } // To display the elements of the tree in Pre-order form void preorder(struct node *root){ if(root != NULL){ cout << root->value << " "; preorder(root->left); preorder(root->right); } } /* Recursive Search function which will find a particular number entered by user */ void search(struct node *root, int number){ if(root == NULL){ // Base-Case 1 : If the given number is not found in BST cout << "Given number : " << number << " is not found in Binary Search Tree! " << endl; return; // return } if(root->value == number){ // Base-Case 2 : If we found the number in BST cout << "Given number : " << number << " is found in Binary Search Tree! " << endl; return; // return } if (number < root->value) // If given number is less than root's value then search in left subtree search(root->left, number); else // If given number is more than root's value then search in right subtree search(root->right, number); } int main(){ struct node *root = NULL; // Create a root for Binary Search Tree int value = 0, number; while(1){ // Run an infinite loop, it will stop when a user enters any negative number cout << "Please enter the number in BST: "; cin >> value; if(value < 0){ // When a user enters any negative number, just break the infinite loop break; } root = createBST(root, value); // Call createBST to insert the given value in BST } cout << "Displaying the elements of the tree in In-order, Post-order and Pre-order form : " << endl; cout << "IN-ORDER FORM: " << endl; inorder(root); cout << "\nPOST-ORDER FORM: " << endl; postorder(root); cout << "\nPlease enter a number to be searched in the Binary Search Tree: " << endl; cin >> number; search(root, number); // Search the given number in BST } Data structures in C++
Modify this code and add a function in the program called “Display_less”. Function should ask user to enter a number then display all elements from the tree that are less than or equal to the number. Note that the number may not be present in the tree.
#include <iostream>
using namespace std;
/* To represent a node of Binary Search Tree*/
struct node{
int value;
struct node *left, *right;
node(int value){
this->value = value; // To initialize the node's value of BST with given value
this->left = this->right = NULL; // To initialize left and right pointers of this node as NULL
}
};
/* createBST() to insert value in a Binary Search Tree*/
struct node * createBST(struct node *root, int value){
if (root == NULL){ // If root is NULL
return new node(value); // Create a new node and return this node
}
if (value < root->value) // if given value is less than root's value then Go left in BST
root->left = createBST(root->left, value);
else // if given value is more than root's value then Go right in BST
root->right = createBST(root->right, value);
return root; // Returns the root
}
// To display the elements of the tree in In-order form
void inorder(struct node *root){
if(root != NULL){
inorder(root->left);
cout << root->value << " ";
inorder(root->right);
}
}
// To display the elements of the tree in Post-order form
void postorder(struct node *root){
if(root != NULL){
postorder(root->left);
postorder(root->right);
cout << root->value << " ";
}
}
// To display the elements of the tree in Pre-order form
void preorder(struct node *root){
if(root != NULL){
cout << root->value << " ";
preorder(root->left);
preorder(root->right);
}
}
/* Recursive Search function which will find a particular number entered by user */
void search(struct node *root, int number){
if(root == NULL){ // Base-Case 1 : If the given number is not found in BST
cout << "Given number : " << number << " is not found in Binary Search Tree! " << endl;
return; // return
}
if(root->value == number){ // Base-Case 2 : If we found the number in BST
cout << "Given number : " << number << " is found in Binary Search Tree! " << endl;
return; // return
}
if (number < root->value) // If given number is less than root's value then search in left subtree
search(root->left, number);
else // If given number is more than root's value then search in right subtree
search(root->right, number);
}
int main(){
struct node *root = NULL; // Create a root for Binary Search Tree
int value = 0, number;
while(1){ // Run an infinite loop, it will stop when a user enters any negative number
cout << "Please enter the number in BST: ";
cin >> value;
if(value < 0){ // When a user enters any negative number, just break the infinite loop
break;
}
root = createBST(root, value); // Call createBST to insert the given value in BST
}
cout << "Displaying the elements of the tree in In-order, Post-order and Pre-order form : " << endl;
cout << "IN-ORDER FORM: " << endl;
inorder(root);
cout << "\nPOST-ORDER FORM: " << endl;
postorder(root);
cout << "\nPlease enter a number to be searched in the Binary Search Tree: " << endl;
cin >> number;
search(root, number); // Search the given number in BST
}
Data structures in C++
Step by step
Solved in 4 steps with 3 images