Read a text file into a binary tree

I am working on an assignment where I need to read a file into a binary tree, sort it, and use it to encode a message. I know how to write code to sort and a loop to encode the message but I'm not sure how to read the file into a tree. This is what I have tried but the only output I get is a zero from the end of my file. What is the correct way to read into a tree?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  struct node
	{
		node *lchild;
		int val;
		node *rchild;
	};
	node *root;

	ifstream infile2;
	infile2.open("D:\\CS318_CSII\\BinaryTree\\BinaryTree\\ASCII_Morse.txt");	//file showing ascii and morse chars

	if (!infile2) {
		cout << "Error opening file 2.";
	}

	node table[36];
	int counter = 0;
	while (infile2 && counter < 36)		//attempting to read file into a tree
	{
		infile2 >> table[counter].val;
		cout << table[counter].val;
	}
node table[36];
Do you know the difference between an array and a tree?

Or, are you purposefully making an array of nodes, that you will then have your tree nodes point to?
Notice that you are not currently using your root variable.

At some point, you should write an "insert" function that inputs a value into a tree (given the tree's root node).
When you insert a value into a tree, if you branch left of the value is less than that of the current node's, and you branch right if the value is greater (and if the current node is null, you can create the new node right there).

I think your use of table[36] is going to make things a lot harder than necessary. I would just use new/delete for purposes of this exercise. In the future, an array could be done for optimization, but no need to worry about that now.
Last edited on
I thought I knew the difference but I may not. I see that I'm reading into an array of nodes but that's how I thought I got it into a tree...

When I open my files, I usually do infile>>(something) to read in the values from the file. What is that something when I'm working with trees?
Separate out the two concepts; separate reading the file from inserting into the tree itself.

Your file will still have a sequence of numbers in it. That's fine. You can still read in each number with infile >> my_int_var;

The difference comes when actually building your tree.
I would forget about the file for now, and just do a small test code, structured in a way similar to the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
struct node
{
	node *lchild;
	int val;
	node *rchild;
};

//*& means "reference of pointer-to-node", meaning you can re-assign the pointer value itself.
void tree_insert(node*& pNode, int insert_value)
{
    if (pNode == nullptr)
    {
        pNode = new node;
        pNode->val = insert_value;
    }
    else if (pNode->val < insert_value)
    {
         // TODO: (hint, recursion)
    }
    else
    {
        // TODO ...
    }
}

void tree_print( /* ... */)
{
    // ... for actually testing the output of the tree
}

int main()
{
    node * root = nullptr;
    tree_insert(root, 42);
    tree_insert(root, 25);
    tree_insert(root, 52);
    tree_print(root);
}


(You might also consider making a class call Tree, which would contain the above functionality (insert, print, etc.) and have a root node pointer as a member.)
Last edited on
Thank you this is a huge help. I'm working on it now and will let you know the outcome.
Topic archived. No new replies allowed.