I need tree traversal algorithms for arbitrary trees in both Depth-first and Breadth-first Traversal order. The tricky part is that I need to be able to start from an arbitrary node and continue until another specific node is traversed.
Now, I can use any of the ordinary algorithms and ignore the traversed nodes until I hit the start node and continue until the end node (which I currently do) but this is ugly and inefficient.
Any suggestions, please.
UPDATE: Each of my nodes have an id associated with them. In some cases, I have start and end node references to start with. In other cases, I am given two Ids, I check whether the given node is the start node or the end node by inspecting their ids. I use depth-first traversal to find the start node. Both the start and end nodes can be anywhere in the hierarchy. I hope someone could come up with an idea for the case where I am already given references to both the start-node and end-node. BTW, the nodes in the tree is actually sorted according to a sort order, which starts from 0 for each of the sub-nodes of a node and there is one root node