CP3210 Assignment 1, 1995 [10%]

The Problem

An arbitary shaped tree can be represented in Prolog, using a function of 2 arguments for each node. The first argument holds the node value, the second argument is a list of offspring. For example, the tree :
        a
     /  |  \
    b   c   d
       / \   \
      e   f   g

is represented as :

node(a,[node(b,[]),node(c,[node(e,[]),node(f,[])]),node(d,[node(g,[])])])

You are to write a Prolog program that builds arbitary trees in this representation, prints out the function representation, prints out the node values in preorder search order, and allows the user to search for values in the tree. The values in the nodes are Prolog atoms. The tree must be stored in a variable, not in the Prolog database (i.e., it cannot be asserted and retracted). Initially the tree is empty, and is represented by nil. Your program must repeatedly offer the user a menu with the options :

B Build a new tree (any existing tree is discarded). The program must prompt for the root node value then recursively build the subtrees by asking for lists of offspring values.

F Print out the function representation.

P Print out the node values in preorder search order.

C Check if a value is in the tree, and if it is, print the node values on the path from the root to that node.

X Exit

Submission

You must submit :

Some further testing of programs may take place in a tutorial subsequent to submission. The assignment is due at the start of the 2pm tutorial on Wednesday 12/4/95. Late submissions will be penalised 100% (i.e., late submissions will not be accepted).