Skip to content

Instantly share code, notes, and snippets.

@hutchison
Created June 6, 2016 13:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hutchison/17767d149ab0188729a8a477c9dd2981 to your computer and use it in GitHub Desktop.
Save hutchison/17767d149ab0188729a8a477c9dd2981 to your computer and use it in GitHub Desktop.
% vim:set noet sts=0 sw=8 ts=8 ft=prolog:
n(Wert, _, _) :- integer(Wert).
tree(n(3,
n(1,
n(4,nil,nil),
n(1,nil,nil)),
n(5,
n(9,
n(2,nil,nil),
nil),
n(6,
nil,
n(5,nil,nil))))).
tree(n(20,
n(31,
nil,
n(97,
nil,
n(17, nil, nil))),
n(20,
n(45, nil, nil),
n(22,
nil,
n(7, nil, nil))))).
is_bin_tree(nil).
is_bin_tree(n(Wert, L, R)) :-
integer(Wert),
is_bin_tree(L),
is_bin_tree(R).
max_value_of_tree(nil, 0).
max_value_of_tree(n(Wert, L, R), M) :-
max_value_of_tree(L, ML),
max_value_of_tree(R, MR),
M1 is max(Wert, ML),
M is max(M1, MR).
replace(nil, _, nil).
replace(n(_, L, R), New, n(New, LNew, RNew)) :-
replace(L, New, LNew),
replace(R, New, RNew).
max_tree(nil, nil).
max_tree(T1, T2) :-
max_value_of_tree(T1, N),
replace(T1, N, T2).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment