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

*/

Know the 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.
Similar Homework Help Questions
  • The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried t...

    The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried to divide the code in 3 parts (Patient.h, Patient.cpp and Main.cpp), but it is giving me errors. Patient.h #ifndef PATIENT_H #define PATIENT_H #include <string> #include "Patient.cpp" using namespace std; class Patient{ private : string firstname; string lastname; string location; static int cnt; int id; public : Patient(string, string, string);...

  • Having trouble reading data from file. NEED HELP!!

    #include<iostream>#include<fstream>using namespacestd;const int DECLARED_SIZE =30;void readTable (inttable[], int size, int &lentgth){ifstream inputfile;int index(0);int data;inputfile.open("numbersdat.txt");if(inputfile.fail()){cout <<"Error opening and reading file." << endl;exit(0);}inputfile >> data;while((index < size) &&(!inputfile.eof())){table[index] =data;index = index +1;inputfile >>data;}inputfile.close();}void outputTable(inttable[], int length){ofstream outputfile;int index;outputfile.open("numbersdat.txt");while(index < length){outputfile<< table[index] << endl;index = index +1;}outputfile.close();}int main (){int table[DECLARED_SIZE];int length(0);readTable(table,DECLARED_SIZE,length);outputTable(table,length);return 0;}

  • How to Dynamically Allocate a 2D character array (C++)

    Hello!I have a program I am creating and I am having trouble with pointers. The purpose of this program to replace the static arrays with dynamically allocated arrays. Iam having difficulty with the char 2d array. Here is main:int main(){int numCandidates;char **allCandidates;int *votes, index, totalVotes;ifstream infile;infile.open("./Ch9_Ex7Data.txt");if (!infile){cerr << "Cannot open input file. Program terminates!" << endl;return 1;}// read number of candidatesreadVotes (infile, votes, allCandidates, numCandidates);cout << fixed << showpoint << setprecision(2);infile.close();totalVotes = sumVotes(votes, numCandidates);cout << left << setw(40) << "Candidate"...

  • How to turn this file into a main.cpp, a header which contains the function declaration, and...

    How to turn this file into a main.cpp, a header which contains the function declaration, and a implementation fiel containing the function definition ? #include<iostream> #include<string> #include<iomanip> using namespace std; #define NUM 1 #define MULT 4 void getPi(int iter); int main() {    int it;    cout << "Enter the number of iterations needed to find PI: ";    cin >> it;    while (it < 1) {        cout << "Error!!! Iteration input should be positive." << endl;...

  • C++ EXERCISE (DATA STRUCTURES). I just need a code for some functions that are missing. Please...

    C++ EXERCISE (DATA STRUCTURES). I just need a code for some functions that are missing. Please help me figure out. Thanks. C++ BST implementation (using a struct) Enter the code below, and then compile and run the program. After the program runs successfully, add the following functions: postorder() This function is similar to the inorder() and preorder() functions, but demonstrates postorder tree traversal. displayParentsWithTwo() This function is similar to the displayParents WithOne() function, but displays nodes having only two children....

  • Here are the files I've made so far: /********************* * File: check05b.cpp *********************/ #include &lt...

    Here are the files I've made so far: /********************* * File: check05b.cpp *********************/ #include <iostream> using namespace std; #include "money.h" /**************************************************************** * Function: main * Purpose: Test the money class ****************************************************************/ int main() {    int dollars;    int cents;    cout << "Dollar amount: ";    cin >> dollars;    cout << "Cents amount: ";    cin >> cents;    Money m1;    Money m2(dollars);    Money m3(dollars, cents);    cout << "Default constructor: ";    m1.display();    cout << endl;    cout << "Non-default constructor 1: ";    m2.display();    cout << endl;   ...

  • need help on this program on arrays ?

    x.øi5Execute the code in source file TheFirm_Array_Lab.cpp. Add code to the file to complete the following tasks. Make sure to include your name and lab CRN in theappropriate places. To receive full credit, you must write a function for each task.Task 1: Write a function that prompts the user to input data for a new employee; the function should return an employeeType struct to the caller.Declare employeeType variables him and her in the main function. Call your function with actual...

  • write a new test program called RemoveDuplicates.java. The program reads text input from keyboard or a text file and adds the words to a BST. The program then traverses the BST and prints out the word...

    write a new test program called RemoveDuplicates.java. The program reads text input from keyboard or a text file and adds the words to a BST. The program then traverses the BST and prints out the words in order (based on ASCII/UNICODE order) on the screen (or to output text file). Note that you may need to make some changes to BST.java. Sample test: ----jGRASP exec: java -ea removeDuplicates Original Text: a B 2 n w C q K l 0...

  • Requirements: Finish all the functions which have been declared inside the hpp file. Details: st...

    Requirements: Finish all the functions which have been declared inside the hpp file. Details: string toString(void) const Return a visible list using '->' to show the linked relation which is a string like: 1->2->3->4->5->NULL void insert(int position, const int& data) Add an element at the given position: example0: 1->3->4->5->NULL instert(1, 2); 1->2->3->4->5->NULL example1: NULL insert(0, 1) 1->NULL void list::erase(int position) Erase the element at the given position 1->2->3->4->5->NULL erase(0) 2->3->4->5->NULL //main.cpp #include <iostream> #include <string> #include "SimpleList.hpp" using std::cin; using...

  • Computer programming- structs

    x.øi5Execute the code in source file TheFirm_Array_Lab.cpp. Add code to the file to complete the following tasks. Make sure to include your name and lab CRN in theappropriate places. To receive full credit, you must write a function for each task.Task 2 (25%) : Write a function name hireOne whose parameters are the array ofstructs, and the size of the list; the function should prompt the user to input a new entry and an index. Add the new entry to...

Free Homework App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
Share Your Knowledge

Post an Article
Post an Answer
Post a Question with Answer

Self-promotion: Authors have the chance of a link back to their own personal blogs or social media profile pages.