E.saadallah
Technical User
hello everybody i need your help i need a source code for dfs and bfs by prolog i tried many code but i didnt get how the querie should be done so hope if you can help as soon as possible.
DFS:-
dfs_search_node(Tree,Node, N) :-
dfs_search_node_(Tree,Node, 1, _, N).
dfs_search_node_(tree(Node, _, _), Node, FN, _, FN) :- !.
dfs_search_node_(tree(_, L, R), Node, CN, _TN, FN) :-
CN1 is CN + 1,
dfs_search_node_(L, Node, CN1, TN1, FN),
( CN1 \== FN
-> dfs_search_node_(R, Node, TN1, _TN, FN)
; true).
dfs_search_node_(nil, _Node, CN, TN, _FN) :-
TN is CN + 1.
BFS:-
bfs_search_node(Tree,Node, N) :-
bfs_search_node_([Tree],Node, 1, N).
bfs_search_node_([tree(Node, _, _) | _T], Node, FN, FN) :- !.
bfs_search_node_([tree(_, L, R) | T], Node, CN, FN) :-
CN1 is CN + 1,
append(T, [L, R], T1),
bfs_search_node_(T1, Node, CN1, FN).
actualy i found above code in a reply of post in this site and i dont know how query should be done .. thanx alot
DFS:-
dfs_search_node(Tree,Node, N) :-
dfs_search_node_(Tree,Node, 1, _, N).
dfs_search_node_(tree(Node, _, _), Node, FN, _, FN) :- !.
dfs_search_node_(tree(_, L, R), Node, CN, _TN, FN) :-
CN1 is CN + 1,
dfs_search_node_(L, Node, CN1, TN1, FN),
( CN1 \== FN
-> dfs_search_node_(R, Node, TN1, _TN, FN)
; true).
dfs_search_node_(nil, _Node, CN, TN, _FN) :-
TN is CN + 1.
BFS:-
bfs_search_node(Tree,Node, N) :-
bfs_search_node_([Tree],Node, 1, N).
bfs_search_node_([tree(Node, _, _) | _T], Node, FN, FN) :- !.
bfs_search_node_([tree(_, L, R) | T], Node, CN, FN) :-
CN1 is CN + 1,
append(T, [L, R], T1),
bfs_search_node_(T1, Node, CN1, FN).
actualy i found above code in a reply of post in this site and i dont know how query should be done .. thanx alot