# CS代考 Logic Programming in Prolog – cscodehelp代写

Logic Programming in Prolog
• Datastructures • Trees
– Representation
– Examples

– Binary search tree
– Representation – Graph problems

Logic Programming in Prolog
• Datastructures • Trees
– Representation
– Examples
– Binary search tree
– Representation – Graph problems

Binary Trees
• Treewhereeachelementhasoneparentanduptotwo children
– Common data structure

Binary Trees in Prolog
• Defineafactforanodeinthedatastructure
t(element, left, right)
– element is the value stored at the node
– left is the left subtree
– right is the right subtree
– an empty subtree can be marked with a ‘nil’
• Atreewithonlytherootnodeist(1,nil,nil)
• Abalancedbinarytreewiththreenodes
t(1,t(2,nil,nil),t(3,nil,nil)).

A Binary Tree
treeA(X) :- X= t(73,
t(5,nil,nil),
t(97,nil,nil)),

Inorder Traversal
inorder(nil).
inorder(t(Root,Left,Right)) :-
inorder(Left),
write(Root),
write(‘ ‘),
inorder(Right).
?- treeB(X), inorder(X).
5 31 73 83 97 101
X = t(73, t(31, t(5, nil, nil), nil), t(101, t(83, nil, t(97, nil, nil)), nil)).

Binary Search Tree
• Sortpredicate(assumingnoduplicates)