Need help in c++ with this entire task please. Lab on Trees In class you learned
ID: 3586200 • Letter: N
Question
Need help in c++ with this entire task please.
Lab on Trees In class you learned that the binary tree data-structure could represented in computer memory contiguously or by random access. Using principled object oriented programming, represent a tree using contiguous access. The tree will be inputted into the computer layer-by-layer starting at the root. Input is terminated with-1 For example blue green yellow pink red purple orange black brown -1 Which yield the following tree structure blue green yello pink purple orange black Question 1. Output a pre-order traversal of the input tree. Output should all be on one line. e.g. output: blue green pink black brown red yellow purple orange put a In-order tra should all ttree. Output s e.g. output black pink brown green red blue purple yellow orange Question 3. Output a post-order traversal of the input tree. Output should all be on one line e.g. output: black brown pink red green purple orange yellow blueExplanation / Answer
#include<iostream>
using namespace std;
string tree[10000];
void preorder(int i,int n){
if(i>=n)
return;
cout<<tree[i]<<" ";
preorder(2*i+1,n);
preorder(2*i+2,n);
}
void inorder(int i,int n){
if(i>=n)
return;
inorder(2*i+1,n);
cout<<tree[i]<<" ";
inorder(2*i+2,n);
}
void postorder(int i,int n){
if(i>=n)
return;
postorder(2*i+1,n);
postorder(2*i+2,n);
cout<<tree[i]<<" ";
}
int main(){
string s;
int i=0;
while(true){
cin>>s;
if(s=="-1")
break;
tree[i]=s;
i++;
}
// Question 1
cout<<"output: ";
preorder(0,i);
cout<<endl;
// Question 2
cout<<"output: ";
inorder(0,i);
cout<<endl;
// Question 3
cout<<"output: ";
postorder(0,i);
cout<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.