Question

// 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
Add a comment Improve this question Transcribed image text
Answer #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 a comment
Know the answer?
Add Answer to:
// File: main.cpp #include <iostream> #include <fstream> #include <iomanip> usi...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

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
  • Thanks in advance: // File: main.cpp #include <iostream> #include <fstream> #include <iomanip> using namespace std; int...

    Thanks in advance: // File: main.cpp #include <iostream> #include <fstream> #include <iomanip> using namespace std; int recursiveCount = 0; void triangle(const char drawChar, const int maxHeight, const int currentHeight); int main() { char drawChar = '*'; recursiveCount = 0;    cout.fill(drawChar); triangle(drawChar, 5, 1); cout.fill(' '); return 0; }// end main() void triangle(const char drawChar, const int maxHeight, const int currentHeight) { /* DO NOT edit code */ cout.fill(drawChar); recursiveCount += 1; /* END of do not edit */ /*...

  • /* BEGIN - DO NOT EDIT CODE */ // File: main.cpp #include "LoginAccount.h" #include "RegisteredLoginAccount.h" #include...

    /* BEGIN - DO NOT EDIT CODE */ // File: main.cpp #include "LoginAccount.h" #include "RegisteredLoginAccount.h" #include "GuestLoginAccount.h" #include <iostream> #include <fstream> #include <string> #include <sstream> using namespace std; //prototypes and constants const int ARRAY_SIZE = 20; using number_of_records_t = int; //Function Declarations. void readAccountsFromFile(ifstream& fin, RegisteredLoginAccount registeredAccounts[], number_of_records_t& registeredLength, GuestLoginAccount guestAccounts[], number_of_records_t& guestLength); bool openFileForInput(ifstream& ifs, const string& filename); bool openFileForOutput(ofstream& ofs, const string& filename); void writeAccountsToFile(ofstream& ofs, RegisteredLoginAccount registeredAccounts[], const number_of_records_t registeredLength, GuestLoginAccount guestAccounts[], const number_of_records_t guestLength); void writeAccountToFile(ofstream&...

  • You are to write three functions for this lab: mean, remove, display. Download the main.cpp file...

    You are to write three functions for this lab: mean, remove, display. Download the main.cpp file provided to get started. Read the comments in the main function to determine exactly what the main function should do. //include any standard libraries needed // - Passes in an array along with the size of the array. // - Returns the mean of all values stored in the array. double mean(const double array[], int arraySize); // - Passes in an array, the size...

  • 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);...

  • #include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool...

    #include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool openFile(ifstream &); void readData(ifstream &, int [], int &); void printData(const int [], int); void sum(const int[], int); void removeItem(int[], int &, int); int main() { ifstream inFile; int list[CAP], size = 0; if (!openFile(inFile)) { cout << "Program terminating!! File not found!" << endl; return -1; } //read the data from the file readData(inFile, list, size); inFile.close(); cout << "Data in file:" <<...

  • 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....

  • //main.cpp #include <iostream> #include <iomanip> #include "deck-of-cards.hpp" void RunAllTests() { int count; std::cin >> count; DeckOfCards...

    //main.cpp #include <iostream> #include <iomanip> #include "deck-of-cards.hpp" void RunAllTests() { int count; std::cin >> count; DeckOfCards myDeckOfCards; for (int i = 0; myDeckOfCards.moreCards() && i < count; ++i) { std::cout << std::left << std::setw(19) << myDeckOfCards.dealCard().toString(); if (i % 4 == 3) std::cout << std::endl; } } int main() { RunAllTests(); return 0; } //card.hpp #ifndef CARD_HPP_ #define CARD_HPP_ #include <string> class Card { public: static const int totalFaces = 13; static const int totalSuits = 4; Card(int cardFace, int...

  • in c++ please program for this code #include <iostream> #include <fstream> #include <string> #include <cstring> //...

    in c++ please program for this code #include <iostream> #include <fstream> #include <string> #include <cstring> // for string tokenizer and c-style string processing #include <algorithm> // max function #include <stdlib.h> #include <time.h> using namespace std; // Extend the code here as needed class BTNode{ private: int nodeid; int data; int levelNum; BTNode* leftChildPtr; BTNode* rightChildPtr; public: BTNode(){} void setNodeId(int id){ nodeid = id; } int getNodeId(){ return nodeid; } void setData(int d){ data = d; } int getData(){ return data;...

  • 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;...

  • vector.h: #ifndef VECTOR_H #define VECTOR_H #include <algorithm> #include <iostream> #include <cassert> template <typename T> class Vector...

    vector.h: #ifndef VECTOR_H #define VECTOR_H #include <algorithm> #include <iostream> #include <cassert> template <typename T> class Vector {     public:         Vector( int initsize = 0 )         : theSize( initsize ),          theCapacity( initsize + SPARE_CAPACITY )         { objects = new T[ theCapacity ]; }         Vector( const Vector & rhs )         : theSize( rhs.theSize),          theCapacity( rhs.theCapacity ), objects( 0 )         {             objects = new T[ theCapacity ];             for( int k = 0; k < theSize; ++k)                 objects[ k ] = rhs.objects[ k...

ADVERTISEMENT
Free Homework Help 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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT