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.
the code is implemented with the comments which will help you understand the code.
in the code we are using insert function to create the tree and inorder method to print inorder traversal and postorder method to print postorder traversal
here is the code:
#include<iostream>
using namespace std;
struct
Bstnode{
//this is the tree node
int val;
struct Bstnode *left;
struct Bstnode *right;
}*root;
struct Bstnode* insert(struct Bstnode * root,int
val) //this is the insert function to
insert the values into the tree
{
if(root==NULL)
//whenever we find the node null means we have to insert node
here
{
struct Bstnode *nod=new struct
Bstnode;
nod->val=val;
nod->left=NULL;
nod->right=NULL;
return nod;
}
if(root->val>
val)
//when value is smaller then our node will go into the left
root->left=insert(root->left,val);
else
root->right=insert(root->right,val);
//otherwise value will go to the right
}
void inorder(struct Bstnode*
root)
//this is recursive inorder function
{
if(root==NULL)
return ;
inorder(root->left);
//inorder follows left root fight policy which is implemented
cout<<root->val<<" ";
inorder(root->right);
}
void postorder(struct Bstnode* root) //recursive
post order function
{
if(root==NULL)
return;
postorder(root->left);
//postorder follows left right root policy which is
implemented
postorder(root->right);
cout<<root->val<<" ";
}
int main()
{
int n;
cin>>n;
root=NULL;
//initially root is null as no element in the tre
for(int i=0;i<n;i++)
{
int a;
cin>>a;
root=insert(root,a);

here is the ss of the output

I hope this will help you so please give positive ratings
:)))
}
inorder(root);
cout<<"\n";
postorder(root);
}
here is the ss of the code
Binary Search Tree (C++) Input Two lines. The first line is a integer n; the second...
I need question 9-10 answered. Thank you
Question 1 iShow the resulting binary search tree if we are to insert following elements into the tree in given order, [34, 12, 23, 27,31,9,11,45, 20, 37. i) Show the resulting balanced binary search tree if we are to insert following sorted elements into the tree, [9,12,21, 23, 29, 31, 34, 45, 48, 52, 55] iii What is the pre-order traversal of the balanced binary search tree? v) What is the post-order traversal...
Use the following integer keys and insert into a binary search tree. Display the final binary search tree after all integer keys are inserted. 50, 25, 12, 75, 45, 48, 60, 55, 85, 5, 100, 35, 47, 70, 58, 30, 38, 65, 49, 80
[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...
Tree & Hash Table & Heap Use the following integer keys 73, 58, 91, 42, 60, 130, 64, 87 to perform the followings: a) Binary Search Tree - Draw a binary search tree - Retrieve the integers keys in post-order - Retrieve the integers keys in pre-order - Draw a binary search tree after node 58 is deleted b) Create a Hash Table using the methods described below. Show the final array after all integer keys are inserted. Assumes that...
C++ ONLY Threaded Binary Search Tree Since a binary search tree with N nodes has N + 1 NULL pointers, half the space allocated in a binary search tree for pointer information is wasted. Suppose that if a node has a NULL left child, we make its left child pointer link to its inorder predecessor, and if a node has a NULL right child, we make its right child pointer link to its inorder successor. This is known as a...
Consider an post-order traversal of the binary search tree created from the following values: 6 75 22 62 2 93 19 16 46 77 82 96 What two values remain to be output after the value 93 has been output?
PROBLEM 6: Suppose we insert keys below into an initially empty binary search tree in the given order 6, 9, 2, 1, 5, 7, 10, 8, 3,4 (a) Draw the resulting binary search tree. (b) List the keys according to: A pre-order traversal An in-order traversal A post-order traversal (c) Now we perform some deletions using the "deletion by copying" strategy in which promoted keys are always drawn from a node's right subtree (so that there is only one correct...
PROBLEM 6: Suppose we insert keys below into an initially empty Vanilla binary search tree in the given order: 6, 9, 2, 1, 5, 7, 10, 8, 3, 4 (a) Draw the resulting binary search tree. (b) List the keys according to: A pre-order traversal An in-order traversal A post-order traversal (c) Now we perform some deletions using the “deletion by copying” strategy in which promoted keys are always drawn from a node’s right subtree (so that there is only...
Java problem In this problem, the first input is a positive integer called n that will represent the number of lines to process. The lines to be processed have one or more integers separated by whitespaces. For each of these lines, you must output: The minimum value of the integers The maximum value of the integers The sum of the integers It is worth to mention that the number of integers of each line is not known a priori and...
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...