Question

Recall that in a binary search tree, at every node, all elements to the left of...

  1. Recall that in a binary search tree, at every node, all elements to the left of the node have a smaller key, and all elements to the right of a node have a larger key. Write a program called that takes two parameters: a pointer to a binary search tree node, and an int parameter called min which will print all the elements bigger than the specified value, min. Your program should allow the user to enter data (integer) from standard input and construct a binary search tree. Make your program as ecient as possible and explain its worst-case asymptotic running time.

         void PrintElements(BinarySearchTree *node, int min)
         Input: A binary search tree node, representing the root of a BST
         Output: Does not return anything, but prints all node values that are bigger than min
    
0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ code is given below:

#include <iostream>
using namespace std;
// Node structure of BST
struct BinarySearchTree
{
int data;
struct BinarySearchTree *left, *right;
};
// function to insert the node into the BST
struct BinarySearchTree* insert(struct BinarySearchTree* node, int val)
{
if (node == NULL) {
struct BinarySearchTree *newNode =
(struct BinarySearchTree *)malloc(sizeof(struct BinarySearchTree));
newNode->data = val;
newNode->left = newNode->right = NULL;
return newNode;
}
if (val < node->data)
node->left = insert(node->left, val);
else if (val > node->data)
node->right = insert(node->right, val);
return node;
}
// function to print the node of the BST with value greater than min
void PrintElements(struct BinarySearchTree *node, int min) {
if(node == NULL) // return if node is null
return;
if(node->data > min) // we go to the left subtree if and only if the node value is greater than min
PrintElements(node->left,min);
if(node->data > min) // we print the node value if it is greater than min
cout<<node->data<<" ";
PrintElements(node->right,min); // otherwise we go to the right subtree
}
int main() {
struct BinarySearchTree *root = NULL;
/* Let's create following BST
5
/ \
3 7
/ \ / \
2 4 6 8 */
root = insert(root,5);
insert(root,5);
insert(root,3);
insert(root,2);
insert(root,4);
insert(root,7);
insert(root,6);
insert(root,8);
// printing all the nodes with value greater than 5 in the above BST
cout<<"Node values greater than 5: ";
PrintElements(root, 5);

return 0;
}

Sample input tree:
  5
/ \
3 7
/ \ / \
2 4  6 8

Sample output:
Node values greater than 5: 6 7 8

worst-case asymptotic running time :
In the worst case, all the nodes of the Binary Search Tree would be traversed. So, the worst time running time would be O(N) where N is the number of nodes present in the Binary search tree.

Screenshot of code:

If the answer helped please upvote, it means a lot and for any query please comment.

Add a comment
Know the answer?
Add Answer to:
Recall that in a binary search tree, at every node, all elements to the left of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Binary Search Tree Part A: The code attached in this document is a sample code to demonstrate ins...

    Binary Search Tree Part A: The code attached in this document is a sample code to demonstrate insert operation in binary search tree. Please fill in the missing part for the insert method to make the program work. The expected output should be as follows. 20 30 40 50 60 70 80 Part B: Find Lowest Common Ancestor (LCA) of a Binary Search Tree. According to WikiPedia definition , The lowest common ancestor is defined between two nodes v and...

  • Consider the partial implementation of a Binary Search Tree (BST) class. For simplicity, each Node stores...

    Consider the partial implementation of a Binary Search Tree (BST) class. For simplicity, each Node stores only the key. Add a public member function to class BST that returns the largest absolute value in the tree. The language is C++ Want the height #4 Coding [6 points] Consider the partial implementation of a Binary Search Tree (BST) class. For simplicity, each Node stores only the key. Add a public member function to class BST that returns the height of the...

  • C++ (Using Binary Search Trees) other methods will result in downvote Implement the binary search tree...

    C++ (Using Binary Search Trees) other methods will result in downvote Implement the binary search tree methods (bst.cpp) for the binary search tree provided in the header file. Test your implementation with the included test. bst.h bst_test.cpp Note: Your implementation must correspond to declarations in the header file, and pass the test. Do not modify these two. I will compile your code against these. If the compilation fails, you will get down vote. bst.h #ifndef BINARY_SEARCH_TREE_H #define BINARY_SEARCH_TREE_H #include <string>...

  • In this assignment, you will add several methods to the Binary Search Tree. You should have compl...

    In this assignment, you will add several methods to the Binary Search Tree. You should have completed the following three methods in the lab: public void insert(Key key, Value value) public Value get(Key key) public void inorder(Node root) For this assignment, you will implement the following: public void remove(Node root, Key key) public Key getMin(Node n) public Key getMax(Node n) public int height(Node n) The main method contains the statements to check whether your implementation works. You need to change...

  • A balanced binary tree is a binary tree structure in which the left and right subtrees of every node differ

    Data structures C++1- A balanced binary tree is a binary tree structure in which the left and right subtrees of every node differ in height by no more than 1 Out of the following choices, which is the minimum set of nodes, if removed, will make the BST balanced?2- Which of the following is true for Red-Black Trees ? Select all choices that apply! Select one or more: a. For each node in the tree, all paths from that node to any leaf nodes contain...

  • e Construct a binary search tree (BST) using the following array elements 60,63,15, 81,30,74,35,38,93,41,53,45,86,90). e For...

    e Construct a binary search tree (BST) using the following array elements 60,63,15, 81,30,74,35,38,93,41,53,45,86,90). e For the above BST, show the use of post-order traversal to delete node 53. . For the above BST, show the path to search the node 86 and to insert a node with key

  • 1) Extend the Binary Search Tree ADT to include a public method leafCount that returns the...

    1) Extend the Binary Search Tree ADT to include a public method leafCount that returns the number of leaf nodes in the tree. 2) Extend the Binary Search Tree ADT to include a public method singleParent-Count that returns the number of nodes in the tree that have only one child. 3) The Binary search tree ADT is extended to include a boolean method similarTrees that receives references to two binary trees and determines whether the shapes of the trees are...

  • Java : This function is to search through a binary tree left and right and return...

    Java : This function is to search through a binary tree left and right and return a count of the nodes above depth k. This is what I have so far, but I'm getting a Null pointer exception. public class MyIntSET {    private Node root;    private static class Node {        public final int key;        public Node left, right;        public Node(int key) { this.key = key; }    }    public int sizeAboveDepth(int...

  • PROMPT: Consider a binary tree (not necessarily a binary search tree) with node structure. QUESTION: Prove that findMax works by mathematical induction. struct Node int val; struct Node * left; struc...

    PROMPT: Consider a binary tree (not necessarily a binary search tree) with node structure. QUESTION: Prove that findMax works by mathematical induction. struct Node int val; struct Node * left; struct Node* right; The following function findMax returns the largest value 'val in the tree; and returns -1 if the tree is empty. You may assume that all the values 'val' in the tree are nonnegative. struct Node * findMax(struct Node root) if (rootNULL) return -1; maxval = root->val; maxval...

  • 1. What is the worst case time complexity of insertion into a binary search tree with...

    1. What is the worst case time complexity of insertion into a binary search tree with n elements? You should use the most accurate asymptotic notation for your answer. 2. A binary search tree is given in the following. Draw the resulting binary search tree (to the right of the given tree) after deleting the node with key value 8. 10 3. You have a sorted array B with n elements, where n is very large. Array C is obtained...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT