Advertisement
Teammasik

Paradigmas_prolog_lr5

Jun 1st, 2023
736
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 0.92 KB | Cryptocurrency | 0 0
  1. #поиск глубины дерева
  2. tree(nil).
  3. tree(node(_,Left,Right)) :- tree(Left), tree(Right).
  4.  
  5.  
  6. % tree(node(1,node(2,node(4,nil,nil),node(5,nil,nil)),node(3,nil,nil))).
  7.  
  8.  
  9. depth(nil, 0).
  10. depth(node(_,Left,Right), Depth) :-
  11. depth(Left, LeftDepth),
  12. depth(Right, RightDepth),
  13. Depth is max(LeftDepth, RightDepth) + 1.
  14.  
  15. %вызовы
  16. %depth(node(1,node(2,node(4,node(7, nil, nil),nil),node(5,nil,nil)),node(3,nil,nil)), Depth).
  17. %depth(node(1,nil,nil), res).
  18. %depth(nil, res).
  19.  
  20. %Защита, тернарное дерево, кол-во вершин:
  21.  
  22. tree(nil).
  23. tree(node(_,Left,Right,Mid)) :- tree(Left), tree(Right), tree(Mid).
  24.  
  25.  
  26. amount(nil, 0).
  27. amount(node(_,Left,Right,Mid), Count):-
  28. amount(Left, LeftCount),
  29. amount(Right, RightCount),
  30. amount(Mid, MidCount),
  31. Count is LeftCount+RightCount+MidCount+1.
  32.  
  33. % amount(node(1,node(2,nil,nil,nil),node(3,nil,nil,nil),node(4,nil,nil,node(6,nil,nil,nil))),N).
  34.  
  35.  
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement