Hello, I'm having a problem writing the output to 'tree.out' for my binary tree program. What it's supposed to do is to outf it to my tree.out by reading the data from tree.dat. I've already put tree.dat and tree.out in my resource files.
Here is my code.
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
struct treetype
{
treetype *root, *left, *right;
int id;
};
void createtree (treetype *&root)
{
root = new treetype;
root->id = -1;
root->left = NULL;
root->right = NULL;
}
bool emptytree(treetype *root)
{
return root == NULL;
}
void inserttree(treetype *root, int id)
{
treetype *parent, *current, *knew;
int data = id;
if(!emptytree(root))
{
knew = new treetype;
knew->id = data;
knew->left = NULL;
knew->right = NULL;
parent = NULL;
current = root;
while(current != NULL)
{
parent = current;
if(data < current->id)
current = current->left;
else
current = current->right;
}
if(data < parent->id)
parent->left = knew;
else
parent->right = knew;
}
else
root->id = data;
}
void readtree(treetype *root)
{
int id;
ifstream inf;
ofstream outf;
inf.open("tree.dat");
outf.open("tree.out");
outf << "Original Output" << endl << endl;
while (!inf.eof())
{
inf >> id;
}
}
void inorder(treetype *root)
{
if(current != NULL)
{
inorder(current->left);
outf << current->id << endl;
inorder(current->left);
}
}
void main()
{
treetype *root;
createtree(root);
readtree(root);
inorder(root);
}
It is giving me this errors.
Code:
c:\documents and settings\owner\my documents\visual studio 2005\projects\hw4trees\hw4trees\hw4trees.cpp(77) : error C2065: 'current' : undeclared identifier
c:\documents and settings\owner\my documents\visual studio 2005\projects\hw4trees\hw4trees\hw4trees.cpp(79) : error C2227: left of '->left' must point to class/struct/union/generic type
type is ''unknown-type''
c:\documents and settings\owner\my documents\visual studio 2005\projects\hw4trees\hw4trees\hw4trees.cpp(80) : error C2065: 'outf' : undeclared identifier
c:\documents and settings\owner\my documents\visual studio 2005\projects\hw4trees\hw4trees\hw4trees.cpp(80) : error C2227: left of '->id' must point to class/struct/union/generic type
type is ''unknown-type''
c:\documents and settings\owner\my documents\visual studio 2005\projects\hw4trees\hw4trees\hw4trees.cpp(81) : error C2227: left of '->left' must point to class/struct/union/generic type
type is ''unknown-type''