Advertisement
mvujas

Binary Trees

Mar 7th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 0.55 KB | None | 0 0
  1. initStablo(nil).
  2.  
  3. insert(E, nil, s(nil, E, nil)):- !.
  4. insert(E, s(L, D, R), s(L1, D, R)):-
  5.     E < D,
  6.     insert(E, L, L1),
  7.     !.
  8. insert(E, s(L, D, R), s(L, D, R1)):-
  9.     E >= D,
  10.     insert(E, R, R1),
  11.     !.
  12.  
  13. listToTree([], nil).
  14. listToTree([ G | R ], S):-
  15.     listToTree(R, S1),
  16.     insert(G, S1, S).
  17.    
  18. brCvorova(nil, 0).
  19. brCvorova(s(L, _, R), N):-
  20.     brCvorova(L, N1),
  21.     brCvorova(R, N2),
  22.     N is N1 + N2 + 1.
  23.  
  24. ispisStabla(nil):- !.
  25. ispisStabla(s(L, E, R)):-
  26.     ispisStabla(L),
  27.     write(E),
  28.     nl,
  29.     ispisStabla(R),
  30.     !.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement