0

I have a network node map, and I need to build a list of nodes between two points. It sounded simple enough to me at first, but the answer is eluding me :(

Given the (simplified) data structure of:

Id    Name        LeftId  RightId
1     Skagway     3
2     Klukwan     3
3     Haines      2       4
4     Juneau      3       5
5     Petersburg  4       6
6     Wrangell    5       7
7     Kasaan      6       4
8     Portage     4       6

How do I build an algorithm (or can you suggest an algorithm) to build a traverse the nodes and build a list of all the nodes between two points? The tree is mostly linear, except in the few spots where its not.

They would like to be able to identify the nodes starting from either the branch or leaf end.

Dan Morphis
  • 1,708
  • 19
  • 23
  • 2
    This is not a tree, it is a directed (binary) graph. A tree is characterised as having a unique path between any two nodes (or alternatively, no cycles) but there are two paths from 8 to 5: 8-4-5 and 8-6-5. Regardless, the algorithm you want is a **Breadth first search**. – amnn Sep 23 '13 at 20:37

1 Answers1

2

If it is a path from one node to another, I suggest you using preorder on the subtree with a root node of the starting node you want the path to log from. UPDATE: In fact, artificial intelligence algorithms (and searching paths in graphs) may also be of use. For example, the very simple one, BFS could be of use.

netchkin
  • 1,387
  • 1
  • 12
  • 21