I've implemented a binary search tree in Java that can search for a particular node. After I find a particular node, I'd also like to retrieve a particular number of nodes (say, 5) that are ordered before the node found. The only way I can think of to achieve this is: Traverse through the entire tree, add each node to an arraylist (or another flat data structure), and then find the node in the arraylist and retrieve the five previous nodes.
But this is inefficient and inelegant. Is there a way to do this just by traversing through the tree, backwards?