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
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;
}
Please help on c++: Construct a binary search tree based on the numbers given on stdin....
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...
You are given the root of a Binary Search Tree. Print the leaf elements of the tree starting from right to left. We have defined the following node C++ Node class for you: class Node { public: int name; Node* left = NULL; Node* right = NULL; }; Function to code: void printLeaves(Node* root); The first input in test cases are nodes of a tree which are inserted in that order. You don't need to implement insert. You have access...
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, 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...
Binary Search Tree (C++) Input Two lines. The first line is a integer n; the second line are n numbers. Output Two lines. First line is the in-order traversal; second line is the post-order traversal. Sample Input 8 23 45 12 6 7 89 13 47 Sample Output 6 7 12 13 23 45 47 89 7 6 13 12 47 89 45 23 Hint If meet equal number, go through the right 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 are inserted in the tree in the given order 12 15 3 35 21 42 14 13 66 41 2
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...
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...
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...