Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Tree Height in Prolog

Status
Not open for further replies.

Yupar

Programmer
Mar 11, 2004
4
AU
Hi,

How can I write a predicate for tree_height(Tree,Height) that binds Height to the number of nodes on the longest path from the root to a leaf in a binary tree. Trees are represented either "empty" or as "tree(L,Data,R)", where L and R are the left and right are the left and right subtrees. Trees must be instantiated at the time of the call.

Example:

:tree_height(tree(tree(empty,,a,empty),b,tree(empty,c,tree(empty,d,empty))),Z)

Z=3

It is quite complicated for me to solve. Any idea? Thanks.

Kind Regards,
Yupar
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top