//C++ Code //The Code is suppose print In, Pre, Post, and Level order, along wit
ID: 3588294 • Letter: #
Question
//C++ Code
//The Code is suppose print In, Pre, Post, and Level order, along with search parents and childents , but I keep getting errors . Could you explain what am I doing wrong thanks
//Rafat R. Islam
#include<iostream>
#include<cstdio>
using namespace std;
int leftChild(int nodeIndex){
return nodeIndex*2+1;
}
int rightChild(int nodeIndex){
return nodeIndex *2+2;
}
int parent (int nodeIndex){
return(nodeIndex-1)/2;
}
void erase(int tArray[], int index, int size){
for (int i = index-1; i tArray[i] = tArray[i+1];
}
}
void InOrderTraverse(const int tArray, int index, int end){
if(index <= end){
InOrderTraverse(tArray, leftChild(index), end);
cout << tArray[index]<< " ";
InOrderTraverse(tArray, rightChild(index), end);
}
}
void PreOrderTraverse(const int tArray, int index, int end){
if(index<= end){
cout<< tArray[index]<<" ";
PreOrderTraverse(tArray, leftChild(index), end);
PreOrderTraverse(tArray, rightChild(index),end);
}
}
void PostOrderTraverse(const int tArray[], int index, int end){
if(index <=end){
PostOrderTraverse(tArray, leftChild(index), end);
PostOrderTraverse(tArray, rightChild(index), end);
cout<< tArray[index]<<" ";
}
}
void LevelOrderTraverse(const int tArray[], int index, int end ){
if (index <= end){
cout << tArray[index] << " ";
LevelOrderTraverse(tArray, index+1,end);
}
}
void searchParent(const int tArray[], int index, int end){
int num = index-100;
if(num==0){
cout<<"No Parent, this is the root of the tree"< else{
cout << tArray[parent(num)];}
}
void searchChild(const int tArray[], int index, int end){
int num = index - 100;
cout << tArray[leftChild(num)]<<" and "< }
void insert(int tArray[], int &size, int element){
int newSize = size +1;
for(int i = newSize; i > newSize, i--){
tArray[i] = tArray[i-1];
}
tArray[size]= element;
}
int main(){
int treeArray[50] = {100, 101,102, 103,104, 105, 106, 107,108,109,110}
int treeSize=11;
LevelOrderTraverse(treeArray, 0, treeSize-1);
cout<<" ";
InOrderTraverse(treeArray, 0, treeSize-1);
cout<<" ";
PreOrderTraverse(treeArray, 0, treeSize-1);
cout<<" ";
PostOrderTraverse(treeArray, 0, treeSize-1);
cout<<" ";
searchParent(treeArray,107);
cout<<" ";
searchChild(treeArray,103);
return 0;
}
Explanation / Answer
You could be getting segmentation fault because of improper memory access due to using the array format. Sometimes it just keeps giving segmentation fault and when you restart your ide it is solved.
Erase has an extra } for it.
The whole for loop in erase is wrong. Check it very carefully. It is of syntax
for(initialization; condition; iteration)
You have to declare the value of constant at time of declaration in all the three traverses.
In inorder and preorder you are passing array as an integer
In search parent an ending } is missing
In search parent you have put an else in the cout. You cant do that.
There are so many errors even after solving these like 10 more came up. It is much better to write the code from scratch.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.