0

If I got it right, a Tree is normally one List with the elements in a specific order. The children are not in sub-lists of their own, they're all in the same list.

So, I'm trying to create a Tree class, that contains TreeNodes (class) using a List in the Tree class.

How do I keep track of parents/children/leaves? If a parent "Parent1", has 2 children "ChildA" and "ChildB", how do I link them together?

user1121487
  • 2,662
  • 8
  • 42
  • 63

1 Answers1

0

No, I don't think you've got it quite right.

Each node normally stores a list of its children.

These children then, in turn, each store lists of their children, and so on.

To then represent the tree, you only have a single node variable to indicate the root.

There are also other representations where you, for example, store the (binary) tree in an array and each index i indicates a node and it's children at at positions 2*i and 2*i+1.

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138