I have a root tree stored in a SQL using materialized path (storing path string for each row).
What is the best way to visit each node node without starting from the root each time? Is materialized path right for my approach?
Harry
├── Jane
│ ├── Mark
│ └── Diane
│ ├── Mary
│ └── George
│ └── Jill
└── Bill
What I expect is that the code first starts at Harry and then visits Jane, Diane, George, Jill. In order to visit Mary, it will need to go back up one level from Jill, and visit Mary. Mary doesn't have any children, and we've visited every node in this level (George, Mary), so we go back up another level to visit Mark. No more children left on this level, so we go back up one level to Jane, but we've no other node on this level, so we go back up again. Finally, we have only Bill on this level, and visit him. When all the nodes have been visited, we are finished.
I thought also about storing each level of the tree in separate tables, and storing references to those tables in another table but this seems a bit inefficient because I'd have to store what level the traversal is currently on, and manipulate this data.
Level_0_table: Harry, Bill
Level_1_table: Jane
Level_2_table: Mark, Diane
Level_3_table: Mary, George
Level_4_table: Jill