I'm looking for some guidance in approaching a tree/graph traversal problem with the following specifications:
- There is one root node.
- The root node can potentially have infinite child nodes.
- Each child node can have only one parent, but can also itself be parent to potentially infinite child nodes, and so forth.
- This will, in a real-world scenario, in most cases be a small structure. However, it should still work for large structures, with regards to traversal and deletion.
- I'm most concerned with deletion, as deletion of a node MUST delete any of node's children, grand-children, etc. as well as any reference to the deleted node in the parent.
How would I approach this problem with efficiency in mind. I'm looking to implement something in Javascript/JQuery. I hope this is enough information and any advice is very much appreciated.
Thank you.
* EDIT/UPDATE * I think the structure described above will most closely resemble a directed graph and due to potentially infinite child nodes at each level, will not be a binary tree. Also, I don't know if I'm articulating this properly, but the structure is not laid out according to its node relationships, in the actual HTML code. In other words the visual/code design of the structure does not match the various parent/child relationships that are present in the JSON representation for example. This was a choice made by someone else but regardless, it is my job to implement traversal,insertion, and deletion.
I've looked around some more on SO and found the following link:
How to delete all related nodes in a directed graph using networkx?
The first answer in the link above is the closest I've found so far to a solution. But I don't understand how the "Worklist Algorithm" mentioned in the first answer, goes deeper than the first level?
Again, any help is appreciated.