*Do not copy other work!!!! *Please show all the steps as detail as possible....
ID: 3855903 • Letter: #
Question
*Do not copy other work!!!!
*Please show all the steps as detail as possible.... I need help with this....
*****************************************************************
Need Help on C++ Programming. Can you show me and teach me how to make this programming.
*Make sure the programming is working and do not turn in a handwriting.
*Do not separate the file, use main.cpp that make me understand how to do this programming. Use "iostream"
**********************************************************************************************************************************
Program 1 Implement a tree using an array
Program 3 - Convert program 1 to a template
Explanation / Answer
PROGRAM 1
#include <iostream>
using namespace std;
int lftchild(int indexnode) // index of the left child will be returned
{
return indexnode *2 + 1 ;
}
int rgtchild(int indexnode) // index of the right child will be returned
{
return indexnode * 2 +2;
}
int inorder(int* a, int indexnode, int& i, int* arr) //the nodes values are printed using a recursive function and put in array
{
if (a[indexnode] == 0) return 0 ;
inorder(a, lftchild(indexnode), i, arr) ;
arr[i]=a[indexnode];
inorder(a, rgtchild(indexnode), ++i, arr); // pre-incrementing the array index i for the right child
return 0;
}
int main () {
int a[93]={2,3,1,9,8,7,6,5};
int arr[9]={0}; // initializing arr; otherwise last element will be garbage
int i = 0; // added index variable to pass to recursive function
inorder(a, 0, i, arr);
for (int k=0; k<9; k++) // k<9: you were overrunning the end of the array
cout <<"inorder " << arr[k] << endl;
system("pause");
return 0;
}
OUTPUT
inorder 5
inorder 9
inorder 3
inorder 8
inorder 2
inorder 7
inorder 1
inorder 6
inorder 0
PROGRAM 3
#include <iostream>
using namespace std;
template <class T>
T lftchild(T indexnode) // index of the left child will be returned
{
return indexnode *2 + 1 ;
}
T rgtchild(T indexnode) // index of the right child will be returned
{
return indexnode * 2 +2;
}
T inorder(T* a, T indexnode, T& i, T* arr) //the nodes values are printed using a recursive function and put in array
{
if (a[indexnode] == 0) return 0 ;
inorder(a, lftchild(indexnode), i, arr) ;
arr[i]=a[indexnode];
inorder(a, rgtchild(indexnode), ++i, arr); // pre-incrementing the array index i for the right child
return 0;
}
int main () {
int a[93]={2,3,1,9,8,7,6,5};
int arr[9]={0}; // initializing arr; otherwise last element will be garbage
int i = 0; // added index variable to pass to recursive function
inorder(a, 0, i, arr);
for (int k=0; k<9; k++) // k<9: you were overrunning the end of the array
cout <<"inorder " << arr[k] << endl;
system("pause");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.