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++

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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++

Expert Solution
steps

Step by step

Solved in 4 steps with 3 images

Blurred answer
Knowledge Booster
Operations of Linked List
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education