Hello, I need help with this assignment: Write a program to do the following: 1.
ID: 661352 • Letter: H
Question
Hello, I need help with this assignment:
Write a program to do the following:
1.Build a binary search tree T1
2. Do a postorder traversal of T1, and while doing the postorder traversal, insert the nodes a second binary search tree T2.
3. Do a preorder traversal of T1, and while doing the preorder traversal, insert the nodes a third binary search tree T3
4. Do an inorder traversal of T3.
5. Output the heights and number of leaves in each of the binary search trees.
I really apreciate any help, thanks!
Explanation / Answer
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
void Design_bst(int[], int, int);
void main()
{
int size=10;
int number[size]; int i; int value; char ch;
for(i=1; i<10;i++)
{
number[i]=-1;
Cout<<"Enter a value";
cin>>value;
Design_bst(number, value, 1);
cout<<"Tree T2 with post order traversal is:"<< postorder(number,1);
cout<<"Tree T3 with pre order traversal is:"<< preorder(number,1);
cout<<"Tree with Inorder traversal is:"<< inorder(number,1);
}
void Design_bst(int number[], int value, int block)
{
if(block>=10)
cout<<"No space available in block";
else
if(number[block]==-1)
number[block]= value;
else
if(value< number[block])
Design_bst(number, value, block*2);
else
Design_bst(number, value,(block*2) +1);
}
Void Postorder(int number[], int block)
{
if(number[block]!=-1)
postorder(number, block*2);
postorder(number,(block*2)+1);
cout<< number[block];
}
Void Preorder(int number[], int block)
{
if(number[block]!=-1)
cout<< number[block];
preorder(number, block*2);
preorder(number,(block*2)+1);
}
Void inorder(int number[], int block)
{
if(number[block]!=-1)
inorder(number, block*2);
cout<< number[block];
inorder(number,(block*2)+1);
}}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.