Prolog Binary Tree - prolog

I have an assignment in which I have to design a binary tree in Prolog and then implement some predicates for it(E.g. Insert, traversal, search and height.)
I am confident enough to implement them but as a starting point I was given this:
A binary tree can be defined in terms of 2 predicates:
emptyBT, the empty binary tree
btTree(N,T1,T2) that is true if N is the root of a binary tree with left subtree T1 and right subtree T2, where all the items in T1 are less than or equal to N and all the items in T2 are greater than N.
Could someone point me in the direction of the BT implementation using the above two predicates? I have searched online and through some Prolog books and they all define BTs differently and this has confused me somewhat.

isEmptyBinTree( btEmpty ).
that's one. Right?
isNonEmptyBinTree( btTree(N,Left,Right) ):-
( isEmptyBinTree(Left)
; % or
isNonEmptyBinTree(Left),
getMaxElement(Left,Max),
N >= Max
) , % and
( isEmptyBinTree(Right)
; % or
isNonEmptyBinTree(Right),
.... % can you complete this definition?
).
that's the other one. Now all that's left is to define the missing getMaxElement/2 and getMinElement/2 predicates. Both of them can assume their argument is not an empty binary tree:
getMinElement( btTree(N,Left,_), M ) :-
isEmptyBinTree(Left),
M = N. % right?
getMinElement( btTree(_,Left,_), M ) :-
getMinElement( ... , M ).

btEmpty is not a predicate; it is an atom. btTree/3 is not a predicate either; it is a compound term. If your assignment calls those two predicates then there is something very fishy.
The definitions in different sources might be different, but they are all probably some variation of an atom representing the empty tree and a term with arity 3 representing a non-empty node.
Anyway, this for example is a tree representing the numbers {1, 2, 3}:
btTree(2, btTree(1, btEmpty, btEmpty), btTree(3, btEmpty, btEmpty))
This is a nested compound term. The tree structure should be recognizable: from your example, the N, the actual element at that tree node, is always the first argument of the term, and the left and right sub-trees are T1 and T2.
And just to get you started, this is a very simple predicate to traverse a tree defined like this in-order:
in_order(btTree(_, Left, _), N) :-
in_order(Left, N).
in_order(btTree(N, _, _), N).
in_order(btTree(_, _, Right), N) :-
in_order(Right, N).
This delivers elements upon backtracking.

Related

prolog - counting node in a binary tree

I have a different request than what I saw on stackoverflow. Say my binary tree is of the form tree(Node, Leafs). There is no left or right leafs, only the attribute leafs.
How would you go about counting the node in that tree?
For example the tree could be:
tree(node1, [tree(node2)]), tree(node3, []), tree(node4, [tree(node6,[tree(node2,[])]).
An unordered binary tree is a tree where each node can have up to two children, but the order between the children is not important. Thus, each node in a non-empty unordered binary tree can be represented as a term of the form tree(Node,Children), where Children is an empty set (if Node is a leaf), or a set of one or two trees (if Node is an internal node). An empty unordered tree can be represented as nil. To simplify the implementation, we can represent sets as lists. For example, the unordered binary tree:
a
/ \
b c
/ \ |
d e f
can be represented as:
mytree(tree(a,
[tree(b,
[tree(d,[]),
tree(e,[])]),
tree(c,
[tree(f,[])])])).
To count the nodes in an unordered binary tree, we can use maplist/3 and sum_list/2 as following:
count_nodes(nil, 0).
count_nodes(tree(_Root, Children), N) :-
maplist(count_nodes, Children, Ns), % call count_nodes, recursively, for each child
sum_list([1|Ns], N). % add 1 to count the root
Here are some examples:
?- count_nodes(nil, N).
N = 0.
?- count_nodes(tree(a,[]), N).
N = 1.
?- count_nodes(tree(a,[tree(b,[])]), N).
N = 2.
?- count_nodes(tree(a,[tree(b,[]),tree(c,[])]), N).
N = 3.
?- mytree(T), count_nodes(T, N).
T = tree(a, [tree(b, [tree(d, []), tree(e, [])]), tree(c, [tree(f, [])])]),
N = 6.

Write a predicate is_heap(Tree) which returns true if Tree satisfies the heap property, and false otherwise

A binary tree of numbers is called a heap (or, it is said to satisfy the heap property) if, for every non-leaf node in the tree, the number stored at that node is less than or equal to the number stored at each of its children. For example, the following tree satisfies the heap property, because 3 ≤ 5, 5 ≤ 8 and 5 ≤ 7.
tree(empty,3,tree(tree(empty,8,empty),5,tree(empty,7,empty)))
On the other hand, the following tree does not satisfy the heap property, because 6 is not less than or equal to 5.
tree(tree(tree(empty,4,empty),
3,tree(empty,5,empty)),6,tree(tree(empty,9,empty),7,empty))
Example :
?- is_heap(tree(tree(tree(empty,4,empty),
3,tree(empty,5,empty)),6,tree(tree(empty,9,empty),7,empty))).
false.
?- is_heap(tree(empty,3,tree(tree(empty,8,empty),5,tree(empty,7,empty)))).
true
Any help will be appriciated.
You can start like this. Here tree is tree(Value, Left, Right) but you can change this easily. Then you start:
is_heap(empty).
is_heap(tree(X, L, R)) :-
is_heap(L, X),
is_heap(R, X).
Now you only need to write is_heap/2 and then you are ready. Something like:
is_heap(empty, ...).
is_heap(tree(X, L, R), X0) :-
...,
is_heap(L, X),
....

Prolog find elements in the list with accumulator

I am trying to understand accumulators, so I want this implementation to contain them.
I have figured how to search through a simple list.
This counts the number of occurrences of a given element:
find(Element,[],A,A).
find(Element, [H|Tail], A, N) :- Element = H, A1 is A + 1,
find(Element, Tail, A1, N).
find(Element, [H|Tail], A, N) :- find(Element, Tail, A, N).
Now how can I do I make it work for nested lists?
You should simply add another item to the matcher such that if H is a list as well, the number of occurrences are counted first:
find(Element,[],A,A).
find(Element, [Element|Tail], A, N) :-
!,
A1 is A + 1,
find(Element, Tail, A1, N).
find(Element,[[H|T]|Tail],A,N) :-
!,
find(Element,[H|T],A,R),
find(Element,Tail,R,N).
find(Element, [H|Tail], A, N) :-
find(Element, Tail, A, N).
By placing the equality of Element (second clause) before the list recursion, you can also search lists in lists. In other words:
find([1,2],[[1,2],[3],4,[1,2]])
Will return 2.
You use a cut (!) to prevent Prolog from backtracking.
Hint: don't unify in the body (Element = H) if possible. Simply reuse the same variable in the head (find(Element,[Element|Tail],A,N)). Many Prolog compilers are able to optimize the latter much better than the first case.
Hint: use cuts. You can use cuts more often to prevent Prolog from non-determinism. For instance if you count the occurences of 5 in [5,1,2,5] it can come up with 2, 1 and 0 since you never specify in your last clause that H is not equal to Element. By placing a cut in the previous clause, the Prolog interpreter will never backtrack over this.

Prolog - subtraction in and between trees

This is a problem I encountered. How do you subtract values in a binary tree recursively?
subtractTree(tree(leaf(4), tree(leaf(2), leaf(1))),S).
S = 3.
For this example, it first subtracts the leaves in each tree before subtracting the values between the trees. So for the first tree, it has the value 4. The second tree, it has the value 1 because 2-1=1. Then it subtracts 4-1 between the trees and the answer is 3.
because your tree structure has arity 2 (as it must be), you can do a very simple visit
subtractTree(leaf(N), N).
subtractTree(tree(L, R), S) :-
subtractTree(L, X),
subtractTree(R, Y),
S is X - Y.
test:
?- subtractTree(tree(leaf(4), tree(leaf(2), leaf(1))),S).
S = 3.
You have 4 cases :
Base case : a tree with only leaves
subtractTree(tree(leaf(A), leaf(B)),S) :-
S is A-B.
2 intermediate cases, trees with one leaf and a tree as a leaf
subtractTree(tree(leaf(A), Tree2),S) :-
subtractTree(Tree2, S1),
S is A - S1.
subtractTree(tree(Tree1, leaf(B)),S) :-
subtractTree(Tree1, S1),
S is S1 - B.
A general case, a tree with leaves which are trees :
subtractTree(tree(Tree1, Tree2),S) :-
subtractTree(Tree1, S1),
subtractTree(Tree2, S2),
S is S1 - S2.

How to find the depth of a binary tree using Prolog

I am learning Prolog and am trying to find the depth of a binary tree using Prolog.
I represent a tree like this:
nil is a tree.
tree(1,nil,nil) this is a leaf.
tree(1,tree(1,nil,nil),nil) this is a tree with root 1 and has a left leaf 1.
I want a depth predicate that depth(T, N) that will be true if N is the depth of the tree T.
I assume that I'll use the depth predicate when T isn't a variable but N can be a variable.
Examples:
?- depth(nil,N).
N = 0.
?- depth(tree(1,tree(2,nil,tree(3,nil,nil)),tree(5,tree(6,nil,nil),nil)),N).
N = 3.
?- depth(tree(1,nil,tree(2,nil,nil)),N).
N = 2.
I am not sure how do that N will be the maximum between the 2 subtrees.
Thanks for any help.
Solution:
depth(nil,0).
depth(tree(_,nil,nil),1).
depth(tree(_,Left,Right), D) :-
depth(Left,DLeft),
depth(Right,DRight),
D is max(DLeft, DRight) + 1.
Easy as pie: the depth of nil is 0:
depth(nil,0).
For the tree case, recursively call depth/2 on both branches and max them:
depth(Left,DLeft),
depth(Right,DRight),
D is max(DLeft, DRight) + 1.
% I added Root \= nil, simulating a null node and it works
depth_tree(nil, 0).
depth_tree(node(Root, Left, Right), Depth):-
Root\= nil,
depth_tree(Left, DepthLeft),
depth_tree(Right, DepthRight),
Depth is max(DepthLeft, DepthRight) + 1.

Resources