0

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.

giasco
  • 1
  • 2
  • Hard to answer without doing the homework for you: But, what data structure are you using for the tree? As you are walking through the data as read from the file, what information about the tree do you need to keep track of, and what operations will you use to update the data structure? – Thomas Bitonti Aug 13 '19 at 16:27
  • I used an array of String splitted by " ", so I stored all the brackets and the numbers. The information I need to track is all the tree, because the exercise ask me to build the tree then print his mirror. – giasco Aug 13 '19 at 16:35
  • I have found only guides to build a binary tree that I think is more simple since I have only root, left child and right child. The program I'm trying to code need to build a tree where roots can have an indefinite number of childs. – giasco Aug 13 '19 at 16:39
  • Yes. What to consider, then, is how to modify a binary tree node to have more children than two. Consider that "left" and "right" as children can be viewed as a direct representation of a two-element array, with "left" being childNodes[0] and "right" being "childNodes[1]" (using an array data structure). About the same, childNodes.get(0) and childNodes.get(1) for a list data structure. – Thomas Bitonti Aug 14 '19 at 15:26

1 Answers1

0

Things to consider:

  1. What does a node of the tree look like? Perhaps a data structure to hold nodes.

  2. As you read the provided string, consider that ( means to go one more level down and ) means one more level up.

  3. When you are at the appropriate level, add the appropriate nodes to your data structure.

When you show your code, we can offer help in debugging it or point out specific problems.

WJS
  • 22,083
  • 3
  • 14
  • 32
  • 1) Can I use a linked list? 2) ok for level up and down but how I can manage the number of children ? in a binary tree I could have created a class child with left and right child, but here I can have an indefinite number of children. – giasco Aug 13 '19 at 16:49
  • Instead of using left and right, you can use an ArrayList to hold the children. – Jamie Aug 13 '19 at 16:54
  • It could be linked list where each level has its own list of nodes and each node could point to another similar linked list. This implies that the root of the tree could potentially have more than a single value. – WJS Aug 13 '19 at 16:55