Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #поиск глубины дерева
- tree(nil).
- tree(node(_,Left,Right)) :- tree(Left), tree(Right).
- % tree(node(1,node(2,node(4,nil,nil),node(5,nil,nil)),node(3,nil,nil))).
- depth(nil, 0).
- depth(node(_,Left,Right), Depth) :-
- depth(Left, LeftDepth),
- depth(Right, RightDepth),
- Depth is max(LeftDepth, RightDepth) + 1.
- %вызовы
- %depth(node(1,node(2,node(4,node(7, nil, nil),nil),node(5,nil,nil)),node(3,nil,nil)), Depth).
- %depth(node(1,nil,nil), res).
- %depth(nil, res).
- %Защита, тернарное дерево, кол-во вершин:
- tree(nil).
- tree(node(_,Left,Right,Mid)) :- tree(Left), tree(Right), tree(Mid).
- amount(nil, 0).
- amount(node(_,Left,Right,Mid), Count):-
- amount(Left, LeftCount),
- amount(Right, RightCount),
- amount(Mid, MidCount),
- Count is LeftCount+RightCount+MidCount+1.
- % amount(node(1,node(2,nil,nil,nil),node(3,nil,nil,nil),node(4,nil,nil,node(6,nil,nil,nil))),N).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement