Question

Please help on c++: Construct a binary search tree based on the numbers given on stdin....

Please help on c++:

Construct a binary search tree based on the numbers given on stdin. Then print out the minimum number and maximum numbers each on their own line. You should keep inserting numbers until you read a -1.

Sample Input:

50

30

35

61

24

58

62

32

-1

Sample Output:

24

62

0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ code

#include<iostream>
using namespace std;

class Node{
   public:
       int data;
       Node *left, *right;
      
       Node(int value){
           data = value;
           left = NULL;
           right = NULL;
       }
};

class BST{
   private:
       Node* root;
      
       Node* insertRec(Node* root , int value){
           if(!root) return new Node(value);

           if(value > root->data)
               root->right = insertRec(root->right, value);
           else
               root->left = insertRec(root->left, value);
      
           return root;
       }
      
      
   public:
       BST(){
           root = NULL;
       }
      
       void insert(int value){
           root = insertRec(root , value);
       }
      
       int findMin(){
           Node* temp = root;
          
           while(temp->left != NULL){
               temp = temp->left;
           }
           return temp->data;
       }
       int findMax(){
           Node* temp = root;
          
           while(temp->right != NULL){
               temp = temp->right;
           }
           return temp->data;
       }
};

int main(){
   BST bst;
   int value;
  
   while(1){
       cin>>value;
       if(value == -1) break;
       bst.insert(value);
   }
  
   cout<<bst.findMin()<<endl;
   cout<<bst.findMax()<<endl;
  
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
Please help on c++: Construct a binary search tree based on the numbers given on stdin....
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 Trees

    Construct a binary search tree based on the numbers given on stdin. Then print out the pre- order, in-order, and post-order traversals each on their own lines. You should print out the numbers separated by spaces and it is acceptable to have a space at the end of each line. Sample Input: 50                        30                        35                       61                       24                       58                       62                       32                        -1 Sample Output: 50 30 24 35 32 61 58 62                           24 30 32 35 50...

  • Q8 - Construct a Binary Search Tree by inserting the following sequence of numbers. 5,6,3,2,10,4,1,7,9,8. Write...

    Q8 - Construct a Binary Search Tree by inserting the following sequence of numbers. 5,6,3,2,10,4,1,7,9,8. Write down the level ordered traversal sequence of the final tree after insert. Delete node 10, 8, and 6 step by step using in-order successor. Write down the level ordered traversal sequence after every delete. I want you to write down (1 level ordered traversal for the insert and 3 level-ordered traversals for the deletes). In total there should be 4 level-ordered traversal sequences in...

  • [Python] Construct Tree Using Inorder and Preorder Given Preorder and Inorder traversal of a binary tree,...

    [Python] Construct Tree Using Inorder and Preorder Given Preorder and Inorder traversal of a binary tree, create the binary tree associated with the traversals.You just need to construct the tree and return the root. Note: Assume binary tree contains only unique elements. Input format : Line 1 : n (Total number of nodes in binary tree) Line 2 : Pre order traversal Line 3 : Inorder Traversal Output Format : Elements are printed level wise, each level in new line...

  • in python 11.1 Binary Search Tree In this assignment, you will implement a Binary Search Tree...

    in python 11.1 Binary Search Tree In this assignment, you will implement a Binary Search Tree You will also need to implement a Node class. This class will not be tested, but is needed to implement the BST. Your BST must implement the following methods. You are free to implement additional helper methods. It is recommended you create your own helper methods Constructor: Creates an Empty Tree String Method: Returns the string "Empty Tree" for an empty tree. Otherwise, returns...

  • Any help please Thanks Draw the binary search tree that is created if the following numbers...

    Any help please Thanks Draw the binary search tree that is created if the following numbers are inserted in the tree in the given order 12 15 3 35 21 42 14 13 66 41 2

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

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

  • QUESTION 9 Consider the following binary search tree: If the root node, 50, is deleted, which node will become the new root?   A 15 B 24 C 37 D 62    QUESTION 10 In the...

    QUESTION 9 Consider the following binary search tree: If the root node, 50, is deleted, which node will become the new root?   A 15 B 24 C 37 D 62    QUESTION 10 In the following trees EXCEPT______, the left and right subtrees of any node have heights that differ by at most 1. A complete trees B perfect full trees C balanced binary trees D binary search trees    QUESTION 11 A perfect full binary tree whose height is 5 has...

  • answer number 7 5. Draw the binary search tree that would result from inserting the following...

    answer number 7 5. Draw the binary search tree that would result from inserting the following numbers into an initially empty tree in the order given: 45, 37, 62, 67, 49, 51, 16, 8, 73, 71, 64, 65 6. Given the tree created in problem 5, draw the tree that would result if you deleted the 37 from the tree? 7. Given the tree created in problem 5, draw the tree that would result if you deleted the 62 from...

  • 1.(10 pts) Contrast a heap with a binary search tree by inserting the numbers 60, 30,...

    1.(10 pts) Contrast a heap with a binary search tree by inserting the numbers 60, 30, 40, 50, 20, 10 first in a BST and then in a min-heap. Draw the resulting BST on the left and the heap on the right. You may draw any valid BST or Heap that contain the provided values 2. (5 pts) In section 11.1, the book mentions that heaps are **complete** binary trees, what does that mean? Demonstrate by drawing an example of...

  • Consider the given binary search tree (BST). 1. What is the maximum size of the array...

    Consider the given binary search tree (BST). 1. What is the maximum size of the array required to implement the above BST? 2. Draw the array-based binary tree that represents the given BST (use the Table tool above to draw the array or just write it on one line and explain it) 3. Print the given BST using the in-order traversal pattern (don't write code, just show the output) HTML Editor BIVA -A- IEE311 X, EE Do NVX 12pt Paragraph...

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