Help With Tree Building

Hi, i am currently trying to program a consensus tree program, however i keep encountering the exception Unhandled exception at 0x0021ada8 in TreeBuild.exe: 0xC0000005: Access violation reading location 0x00000014.


I would like to ask for help with it


Here is my code, please help me look through it

// TreeBuild.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <string>
#include <stack>
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>


using namespace std;

struct TreeNode
{
string species;
TreeNode* left;
TreeNode* right;
TreeNode* parent;
};
TreeNode* root = NULL;


TreeNode* rooted(TreeNode* root, string n);
TreeNode* insert(TreeNode* root, char species_symbol);
void inorder(TreeNode* root);

int main()
{
string n;
string s1;
string s2;
char array1[1000];
char array2[1000];

ifstream inFile;
inFile.open("TreeBlock_10Trees_Hominoidea_Cleaned.nex");

inFile >> n; //first line of file tells that there are n trees

//read in 2 trees and build consensus
getline(inFile, s1);
getline(inFile, s1);
istringstream is1(s1);
int i = 0;
while (is1 >> array1[i])
{
cout << array1[i]; //check
if (array1[i] == ';')
i=0;
root = insert(root, array1[i]); //node for each character
i++;
}


//root tree at n
rooted(root, n);
cout << "BAAAA" << endl;
cout << root->species << " ";
cout << " BAAA" << endl;
rooted(root, n);

inFile.close();

//inorder(root1); //check
//inorder(root2); //check

return 0;

}

TreeNode* rooted(TreeNode* root, string n)//root tree at n
{
TreeNode* roots;
if (root != NULL)
{
cout << root->species << endl;
rooted(root->left, n);
if (root->species == n)
{
roots = root;
}
rooted(root->right, n);
}
if (root->species == n)
{
return roots;
}
}


TreeNode* insert(TreeNode* root, char species_symbol)
{
if (root == NULL) //insert root
{
cout << "N "; //check
root = new TreeNode;
root->left = NULL;
root->right = NULL;
root->parent = NULL;
}
else if (species_symbol == '(') //insert internal node
{
if (root->left == NULL) //insert left internal node
{
cout << "L "; //check
root->left = new TreeNode;
root->left->parent = root;
root = root->left;
root->left = NULL;
root->right = NULL;
}
else //insert right internal node
{
cout << "R "; //check
root->right = new TreeNode;
root->right->parent = root;
root = root->right;
root->left = NULL;
root->right = NULL;
}
}
else if (species_symbol == ',' || species_symbol == ')') //go to parent
{
cout << "P "; //check
root = root->parent;
}
else
{
if (root->left == NULL && ((root->species).length() == 0)) //insert species as left child
{
cout << "LC "; //check
root->left = new TreeNode;
root->left->parent = root;
root = root->left;
root->species = root->species + species_symbol;
root->left = NULL;
root->right = NULL;
}
else if (root->right == NULL && ((root->species).length() == 0)) //insert species as right child
{
cout << "RC "; //check
root->right = new TreeNode;
root->right->parent = root;
root = root->right;
root->species = root->species + species_symbol;
root->left = NULL;
root->right = NULL;
}
else //concatenate integer character as part of string identifier
{
root->species = root->species + species_symbol;
}
}
return root;

}
[code] "Please use code tags" [/code]
You are probably dereferencing an invalid pointer.
I don't understand the logic of your program. How is the insertion performed?
Try to encapsulate the operations in the class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
TreeNode* rooted(TreeNode* root, string n)
{
	TreeNode* roots;
	if (root != NULL)
	{
		cout << root->species << endl;
		rooted(root->left, n);
		if (root->species == n)
		{
			roots = root;
		}
		rooted(root->right, n);
	}
	if (root->species == n)
	{
		return roots;
	}
}
What is the purpose of that function?
_The returned value is not being captured in main.
_The returned value could be garbage.
_Some paths don't return anything.
This is a test function used to re root the the tree at a particular node eg

Tree is

1 2 3 4 5


then rooting it at 4
4

5 3 2 1


Im still testing this function out but its giving me a lot of problem
Topic archived. No new replies allowed.