In Erlang, how to write a BFS Tree Search to find a specific node -


i want write function find specific node in bfs tree search.

how in erlang?

assuming simple structure:

node() :: {key :: term(), left :: node(), right :: node()} | undefined. 

this function perform breadth-first search on tree , return depth of first element found (returns false if no node found):

find(_key, undefined) -> false; find(key,  tree)      -> find(key, [tree], [], 0).  find(_key, [], [], _depth) -> % no more children     false; find(key, [], children, depth) -> % next level, increase depth     find(key, children, [], depth + 1); find(key, [{key, _, _}|_rest], _children, depth) -> % found it!     depth; find(key, [{_, left, right}|rest], children, depth) -> % add children search     find(key, rest, add(right, add(left, children)), depth).  add(undefined, list) -> list; add(e,         list) -> [e|list]. 

(an empty tree undefined).


Comments