I'm trying to build an incomplete binary tree from data provided in a text file
2
3 8
2 10 0 12
1 8 0 0 4 0
which would form a tree like this:
I know roughly what I need to do, I think:
- read in from the file line-by-line.
- pass an array to the build function.
- As it's building the tree, the tree data type will keep track of the last nodes that it added in an array of pointers (eg, if we just finished adding the 3rd row of the text file, the a previous nodes array would look like pointers to the nodes containing
[2 10 12] - as we start adding the 4th line, we create an array of 3*2=6 length to store pointers to the nodes as we add them.
- we go through the array of pointers from the previous run (containing
[2 10 12]) and create left and right children nodes for any non-zero keys that got passed through. We put pointers to those nodes in our 6-long array. - when we get a blank line, we're done.
The problem I'm running into is, I'm not sure how to store a array of pointers to nodes that changes size each time I call the build function and is a class variable (for the Tree class). Am I going about this the right way? Is there an easier way to approach this?
