I'm doing a homework for university. Is the first time I use tree as a data structure and I don't understand how to implement it in Java.
I have to read from a .txt file a string like this
( 1 ( 2 ( 5 ( 13 ) 6 7 ) 3 ( 8 9 ) 4 ( 10 11 12 ) ) )
where the round brackets are delimiter for a level of a tree representing root and sons in this way --> (root (soon1 soon2) )
the structure of this tree is this:
1
2 3 4
5 6 7 8 9 10 11 12
13
I've already implemented a method to read the .txt file but I don't know how to store the value in different levels and marking them as root or child.
Things to consider:
What does a
nodeof thetreelook like? Perhaps a data structure to hold nodes.As you read the provided string, consider that
(means to go one more level down and)means one more level up.When you are at the appropriate level, add the appropriate
nodesto your data structure.When you show your code, we can offer help in debugging it or point out specific problems.