Homework Help Question & Answers

// File: main.cpp #include <iostream> #include <fstream> #include <iomanip> usi...

// File: main.cpp

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

int recursiveCount = 0;

void inorder(const int list[], const int n, const int index);
int leftIndex(const int index);
int rightIndex( const int index);


int main() {
int list[] = {1,2,3,4,5,6,7,8,9,10};

inorder(list, 10, 0);
cout << endl;

return 0;
}// end main()

void inorder(const int list[], const int n, const int index) {
/* START - NO EDIT */ recursiveCount += 1; /* END - NO EDIT */

/* TODO (1):
* Implement the inorder traversal function recursively.
* Print the element visited followed by a white space.
*/
  
  
  
  
  
}// end inorder()

int leftIndex(const int index) {
/* TODO (2):
* Based on the discussion in the instructions, return the 'left child index'
* given the parent 'index'.
*/

}// end leftIndex()

int rightIndex( const int index) {
/* TODO (3):
* Based on the discussion in the instructions, return the 'right child index'
* given the parent 'index'.
*/


}// end rightIndex()

REQUESTING ASSISTANCE WITH THE TRAVERSAL FUNCTION. All examples I've seen have been using pointers, and this only uses an index. I'm not really sure how to begin this particular function.

0 0
Next > < Previous
ReportAnswer #1

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

int recursiveCount = 0;

void inorder( int list[], int n,int index);// n is number of element
int leftIndex(int index,int n,int list[]);
int rightIndex(int index,int n,int list[]);
int main() {
int list[] = {1,2,3,4,5,6,7,8,9,10};
inorder(list,10, 0);//calling inorder
cout << endl;
return 0;
}// end main()
void inorder(int list[],int n,int index)
{
// checking for valid index and null node
if(index>=0 && list[index]!='\0')
{recursiveCount++;
inorder(list,n,leftIndex(index,n,list)); // visiting left subtree
cout<<list[index]<<" "; //visiting root
inorder(list,n,rightIndex(index,n,list)); // visiting right subtree
  
}
}

int leftIndex(int index,int n,int list[]) {
if(list[index]!='\0' && ((2*index)+1)<n) // check left child exit or not if exit return index of leftchild
{
return ((2*index)+1);
}
// left child doesn't exist
return -1;

}// end leftIndex()

int rightIndex( int index,int n,int list[]) {

if(list[index]!='\0' && ((2*index)+2)<n) // check right child exit or not if exit return index of rightchild
{
return (2*index)+2;
}
// right child doesn't exist
return -1;
}// end rightIndex()

/* Output:

8 4 9 2 10 5 1 6 3 7

*/

Add Homework Help Answer
Add Answer of:
// File: main.cpp #include <iostream> #include <fstream> #include <iomanip> usi...
Your Answer: Your Name: What's your source?
Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
More Homework Help Questions Additional questions in this topic.
Need Online Homework Help?
Ask a Question
Get FREE Expert Answers
WITHIN MINUTES
Related Questions