Hello. I'm pretty new to debugging with visual c++. I've tried reading some tutorials but none of them seem to cover what my program is doing. i enter the command arguements for my code which is the file ./input.txt. I then get the following error.
1>c:\documents and settings\jennifer\my documents\visual studio 2008\projects\binarytree\binarytree\input.txt(1) : error C2059: syntax error : 'constant'
here is my code for the program
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
int reconstruct(vector<int> const& inorder, vector<int> const& preorder, vector<pair<int, int> >& result, int l, int r, int& index)
{
if (l > r || index >= preorder.size())
{
if (l > r)
--index;
return 0;
}
if (l == r)
return inorder[l];
int m;
for (m = l; m <= r; ++m)
if (inorder[m] == preorder[index])
break;
int root = preorder[index];
// searches for root's childs
int left = reconstruct(inorder, preorder, result, l, m - 1, ++index);
int right = reconstruct(inorder, preorder, result, m + 1, r, ++index);
result[root] = make_pair(left, right);
return root;
}
// makes result in level traversal
void makeResult(int root, vector<pair<int, int> > const& tree,
vector<vector<int> >& result, int height)
{
if (root == 0)
return;
result[height].push_back(root);
makeResult(tree[root].first, tree, result, height + 1);
makeResult(tree[root].second, tree, result, height + 1);
}
int main(int argc, char* argv[])
{
ifstream input(argv[1]);
if (!input)
{
cout << "Can't open input file: " << argv[1] << "\n";
return 1;
}
int n;
input >> n;
vector<int> inorder(n), preorder(n);
for (int i = 0; i < n; ++i)
input >> inorder[i];
for (int i = 0; i < n; ++i)
input >> preorder[i];
int index = 0;
vector<pair<int, int> > tree(n + 1);
vector<vector<int> > result(n);
// recontsruct tree
int root = reconstruct(inorder, preorder, tree, 0, n - 1, index);
// make level traversal output
makeResult(root, tree, result, 0);
// output final result level by level
for (int i = 0; i < n; ++i)
{
vector<int>& level = result[i];
// output all nodes at level i
for (int j = 0; j < level.size(); ++j)
{
cout << level[j] << " " << tree[level[j]].first << " " << tree[level[j]].second << "\n";
}
}
return 0;
}
and input.txt looks like this
10
1
2
6
5
3
4
8
10
7
9
3
2
1
5
6
4
7
10
8
9