How JustAnswer Works:

  • Ask an Expert
    Experts are full of valuable knowledge and are ready to help with any question. Credentials confirmed by a Fortune 500 verification firm.
  • Get a Professional Answer
    Via email, text message, or notification as you wait on our site.
    Ask follow up questions if you need to.
  • 100% Satisfaction Guarantee
    Rate the answer you receive.

Ask ATLPROG Your Own Question

ATLPROG
ATLPROG, Computer Software Engineer
Category: Programming
Satisfied Customers: 7260
Experience:  MS in IT.Several years of programming experience in Java C++ C C# Python VB Javascript HTML
44910485
Type Your Programming Question Here...
ATLPROG is online now

, basically I am required to create a binary tree program.

Customer Question

Hi, basically I am required to create a binary tree program. A set column of data is to be read in from a csv file directly into the binary tree node using the stoi string to int conversion parameter. I have done all of this, however my main struggle is now getting the program to a point where it is able to traverse the integers in order, can someone help?

Attachment: 2015-02-05_222233_treeprogram.zip

my code for my program is as follows;

#include <iostream>
#include <fstream>
#include<stdlib.h>
#include<stdio.h>
#include <string>
#include <vector>
#include <sstream>
using namespace std;

struct tree_program { //class for the tree program
int grademark; //declaring the integer based grademark variable
struct tree_program * right, *left; //declaring right and left pointers
};
typedef struct tree_program node; //declaring node which is where the integers will be assigned

void insert(node ** tree, int values)
{
node *data = NULL; //names node as data initialising as NULL = 0
if (!(*tree))
{
data = (node *)malloc(sizeof(node)); //memory allocation of data node
data->left = data->right = NULL; //traversing the left and right subtree
data->grademark = values; //grademark = the values being read in from the Data File
*tree = data;
}

if (values < (*tree)->grademark) //values on left nodes from grademark values
{
insert(&(*tree)->left, values); //inserting values into tree
}
else if (values >(*tree)->grademark) //values on right nodes from grademark values
{
insert(&(*tree)->right, values); //inserting values into tree
}
}

void print_tree(node * tree){ //prints all the items which the root points at
if (tree->left != NULL) //null meaning nothing to print left is free
{
print_tree(tree->left); //prints left appointed values
}
printf("%d\n", tree->grademark); //prints the root item which is the grademark values
if (tree->right)
{
print_tree(tree->right); //prints right appointed values
}

}

void deletetree(node * tree) //deletes tree values
{
if (tree)
{
deletetree(tree->left); //selects left node values
deletetree(tree->right); //selects right node values
free(tree); //clears values from tree memory
}
}

class GradeMark //created new class for reading in grade marks
{
public:
std::string const& operator[](std::size_t index) const
{
return m_data[index];
}
void readNextRow(std::istream& str)
{
std::string line; //string is declared as line
std::getline(str, line); //program then gets line using istream

std::stringstream lineStream(line);
std::string cell; //strings declared as variable cell

m_data.clear();
while (std::getline(lineStream, cell, ',')) //read line by line whilst cells split by commas
{
m_data.push_back(cell); //gets rid of unwanted information by pushing back cells
}
}
private:
std::vector<std::string> m_data;
};

std::istream& operator>>(std::istream& str, GradeMark& data)
{
data.readNextRow(str); //reads next line of data until no data is present looping
return str; //returns string
}

int main()
{
cout << "Tree Data\n";
std::ifstream file("DataFile.csv"); //opens the data file where fstream reads it in CSV file
GradeMark row;
while (file >> row)
{ //where row[0] are student names, row[2] is grademark
int data = stoi(row[2]); //stoi is the function used to convert strings to integers

node *root; //node declared as variable root
root = NULL; //root is NULL = 0

insert(&root, data); //calls data from "int data" calling the converted string

print_tree(root); //Printing nodes of tree
deletetree(root); //Deletes all nodes of tree
}
}

Submitted: 1 year ago.
Category: Programming

What Customers are Saying:

 
 
 
  • My Expert answered my question promptly and he resolved the issue totally. This is a great service. I am so glad I found it I will definitely use the service again if needed. One Happy Customer
< Previous | Next >
  • My Expert answered my question promptly and he resolved the issue totally. This is a great service. I am so glad I found it I will definitely use the service again if needed. One Happy Customer
  • Wonderful service, prompt, efficient, and accurate. Couldn't have asked for more. I cannot thank you enough for your help. Mary C.
  • This expert is wonderful. They truly know what they are talking about, and they actually care about you. They really helped put my nerves at ease. Thank you so much!!!! Alex
  • Thank you for all your help. It is nice to know that this service is here for people like myself, who need answers fast and are not sure who to consult. GP
  • I couldn't be more satisfied! This is the site I will always come to when I need a second opinion. Justin
  • Just let me say that this encounter has been entirely professional and most helpful. I liked that I could ask additional questions and get answered in a very short turn around. Esther
  • Wonderful service, prompt, efficient, and accurate. Couldn't have asked for more. I cannot thank you enough for your help. Mary C.
 
 
 

Meet The Experts:

 
 
 
  • ATLPROG

    ATLPROG

    Computer Software Engineer

    Satisfied Customers:

    7260
    MS in IT.Several years of programming experience in Java C++ C C# Python VB Javascript HTML
< Last | Next >
  • http://ww2.justanswer.com/uploads/SP/spatlanta2010/2011-6-23_12450_photo.64x64.gif ATLPROG's Avatar

    ATLPROG

    Computer Software Engineer

    Satisfied Customers:

    7260
    MS in IT.Several years of programming experience in Java C++ C C# Python VB Javascript HTML
  • http://ww2.justanswer.com/uploads/ComputersGuru/2010-02-13_051118_Photo41.JPG LogicPro's Avatar

    LogicPro

    Computer Software Engineer

    Satisfied Customers:

    5514
    Expert in C, C++, Java, DOT NET, Python, HTML, Javascript, Design.
  • http://ww2.justanswer.com/uploads/lifesaver333/2010-10-17_191349_ls.jpeg lifesaver's Avatar

    lifesaver

    Computer Software Engineer

    Satisfied Customers:

    936
    Several years of intensive programming and application development experience in various platforms.
  • http://ww2.justanswer.com/uploads/EH/ehabtutor/2012-8-2_202016_1.64x64.jpg ehabtutor's Avatar

    ehabtutor

    Computer Software Engineer

    Satisfied Customers:

    766
    Bachelor of computer science, 5+ years experience in software development, software company owner
  • http://ww2.justanswer.com/uploads/eljonis/2010-01-06_130406_eljon2.jpg Eljon's Avatar

    Eljon

    Consultant

    Satisfied Customers:

    558
    Founder of StockCanvas.com
  • http://ww2.justanswer.com/uploads/FA/fael/2011-1-15_104041_profile500x500.64x64.jpg Rafael Martins's Avatar

    Rafael Martins

    Computer Software Engineer

    Satisfied Customers:

    446
    Desktop, Mobile and Web Developer. 7+ years of experience. Creative solutions provider.
  • http://ww2.justanswer.com/uploads/CH/chutz747/2011-11-6_14231_me.64x64.jpg The-PC-Guy's Avatar

    The-PC-Guy

    Software Engineer

    Satisfied Customers:

    320
    Extensive Knowledge in PHP, MYSQL, CSS & Javascript