Advertisement
Guest User

Untitled

a guest
Sep 21st, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. tree_depth( nil, 0). % the depth of an empty tree is 0.
  2. tree_depth(t(L,_,R), D) :- % the depth of a non-empty tree is computed thus:
  3. tree_depth(L,L1), % - compute the depth of the left subtree
  4. tree_depth(R,R1), % - compute the depth of the right subtree
  5. D is 1 + max(L1,R1). % - the overall depth is 1 more than the maximum depth in both subtrees. %
  6.  
  7.  
  8. tree_node_diameter(t(L,_,R), D):-
  9. tree_depth(L,D1), tree_depth(R,D2), D is D1 + D2.
  10.  
  11. tree_diameter(T, D):-
  12. tree_node_diameter(T, D).
  13.  
  14. tree_diameter(t(L,_,_), D):-
  15. tree_node_diameter(L,D).
  16.  
  17. tree_diameter(t(_,_,R), D):-
  18. tree_node_diameter(R,D).
  19.  
  20. all_tree_diameters(T, D):-
  21. tree_diameter(T, D1),
  22. (tree_diameter(T, D2), D1 >D2), D is D1.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement