Our task is to write a Binary Search Tree and the data has to be input from a .txt file where in the data is held as such

12345678

Each digit is a new node in the tree to be inserted. My Binary Tree is working because I can just enter raw numbers into the insert functions and it works and prints properly but when I try and draw the data in from the txt file i comes up with random numbers. The following example is a run where the file contained the digits

123

Example:

Please enter a file name: 2l.txt
You entered file name: 2l.txt
File was successfully opened
element = 1
Key:  49 SearchCost = 1
element =

Key:  49 SearchCost = 1
Key:  10 SearchCost = 2
element = 2
Key:  49 SearchCost = 1
Key:  10 SearchCost = 2
Key:  50 SearchCost = 2
element =

Abort (core dumped)

Below I have posted the code for my main.

#include "BinarySearchTree.h"
#include <fstream>
#include <string>

using namespace std;

int main() {
	BinarySearchTree* tree = new BinarySearchTree();

	ifstream istream;
	string fileName;
	
	cout << "Please enter a file name: ";
	cin >> fileName;
	cout << "You entered file name: " << fileName << endl;

	//Test whether file is opened
	istream.open(fileName.c_str());
	if (istream.is_open())
	{
		cout << "File was successfully opened" << endl;
	}
	else
	{
		cout << "Error opening file" << endl;
	}

	char element;
	while(!istream.eof()) {
		element = istream.get();
		cout << "element = " << element << endl;
		tree->insert(element);

		BinaryNode::PrintTreeNode pt;
		tree->getRoot()->preOrderTraversal(pt);
	}

	BinaryNode::PrintTreeNode pt;
	tree->getRoot()->preOrderTraversal(pt);

	return 0;
}

I have also zipped the input file and the 4 code files in case anyone is interested.

Thanks in advance

Recommended Answers

All 7 Replies

No one? Im really perplexed by this one.

Well I don't know about you, but where I live it's a (minor) holiday evening and a Friday night to boot. I feel a little goofy even looking but just got done with some online stuff for work, so here I am.

Given your description I 'd comment everything about the tree out and just focus on getting material from the file, especially as it appears from your output that the file extraction isn't proceeding appropriately. I'd change:

while(!istream.eof()) {
element = istream.get();

to:

while(istream.get(element))

to avoid potential for reading last char before EOF twice, but I don't think that should prevent the code from reading into element on pass two and four before a core dump. I don't download zip files, but I'd look for a file read or some uncommented line inputting into element from within insert() given this statement:

I can just enter raw numbers into the insert functions and it works and prints properly

Also why are you doing a traversal after each insert?

Thanks for the reply. The traversal was for testing purposes I wanted to see what the tree was holding to see what was being brought in. I will try your suggestion tomorrow when I get up.

Thanks again

Ok so after looking at it a little more I think I figured out the problem I just dont know how to work around it. I am trying to bring in ints from the file however istream.get(element) only accepts characters and it wont convert properly back to an int. This is the current version of main I am working on.

int main() {
	BinarySearchTree* tree = new BinarySearchTree();

	ifstream istream;
	string fileName;
	
	cout << "Please enter a file name: ";
	cin >> fileName;
	cout << "You entered file name: " << fileName << endl;

	//Test whether file is opened
	istream.open(fileName.c_str());
	if (istream.is_open())
	{
		cout << "File was successfully opened" << endl;
	}
	else
	{
		cout << "Error opening file" << endl;
	}

	char element;
	
		
	while(istream.get(element)) {
		//element = istream.get();
		cout << "element = " << element << endl;
		tree->insert((int)element);

		BinaryNode::PrintTreeNode pt;				//for Testing
		tree->getRoot()->preOrderTraversal(pt);		//for Testing
	}

	BinaryNode::PrintTreeNode pt;
	tree->getRoot()->preOrderTraversal(pt);

	return 0;
}

Anyone know how to bring in ints and assign them to an integer. Remember that if given something like

123

I want to bring in 1, then 2, then 3 not the number 123.

Thanks

What we see as char are actually ints to the program. Each char is assigned an integer value that is maintained in a character set, such as ASCII or Unicode. In all the character sets I've seen the capital letters are sequential, the lower case letters are sequentail and the digits are sequential (in the order of 0-9). You can use this information to transform a char to an int by subtracting the char zero from the desired char. The difference is then an integer value.

char ch = '8';
int i = ch - '0';

In all the character sets I've seen the capital letters are sequential, the lower case letters are sequentail

FWIW

Alright thanks to everyone. I figured it out using a switch statement to convert the chars. Its not very elegant but it works for now. Thanks Lerner that info will come in handy later.

Thanks to everyone

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.