Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- tree_depth( nil, 0). % the depth of an empty tree is 0.
- tree_depth(t(L,_,R), D) :- % the depth of a non-empty tree is computed thus:
- tree_depth(L,L1), % - compute the depth of the left subtree
- tree_depth(R,R1), % - compute the depth of the right subtree
- D is 1 + max(L1,R1). % - the overall depth is 1 more than the maximum depth in both subtrees. %
- tree_node_diameter(t(L,_,R), D):-
- tree_depth(L,D1), tree_depth(R,D2), D is D1 + D2.
- tree_diameter(T, D):-
- tree_node_diameter(T, D).
- tree_diameter(t(L,_,_), D):-
- tree_node_diameter(L,D).
- tree_diameter(t(_,_,R), D):-
- tree_node_diameter(R,D).
- all_tree_diameters(T, D):-
- tree_diameter(T, D1),
- (tree_diameter(T, D2), D1 >D2), D is D1.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement