[Solved] CS161 Homework 1-single Boolean LISP function

$25

File Name: CS161_Homework_1-single_Boolean_LISP_function.zip
File Size: 423.9 KB

SKU: [Solved] CS161 Homework 1-single Boolean LISP function Category: Tag:
5/5 - (1 vote)

An ordered tree is either a number n or a list (L m R), where

  • L and R are ordered trees;
  • m is a number;
  • all numbers appearing in L are smaller than m; all numbers appearing in R are larger than m.

Some examples of ordered trees: 3, (1 2 3), ((1 2 3) 7 8), ((1 2 3) 5 (6 8 (9 10 (11 12 13)))).

  1. Write a single Boolean LISP function, called TREE-CONTAINS, which takes two arguments N and TREE, and checks whether number N appears in the ordered tree TREE.

For example,

(TREE-CONTAINS 3 ((1 2 3) 7 8)) returns T

(TREE-CONTAINS 4 ((1 2 3) 7 8)) returns NIL

  1. Write a single LISP function, called TREE-MIN, which takes one argument TREE, and returns the minimum number appearing in the ordered tree TREE.

For example,

(TREE-MIN ((1 2 3) 7 8)) returns 1

  1. Write a single LISP function, called TREE-ORDER, which takes one argument TREE, and returns an pre-ordered list of the numbers appearing in the ordered tree TREE.

For example,

(TREE-ORDER 3) returns (3)

(TREE-ORDER ((1 2 3) 7 8)) returns (7 2 1 3 8)

  1. Write a single LISP function, called SUB-LIST, that takes a list L and two non-negative integers

START and LEN, and returns the sub-list of L starting at position START and having length LEN. Assume that the first element of L has position 0.

For example,

(SUB-LIST (a b c d) 0 3) returns (a b c)

(SUB-LIST (a b c d) 3 1) returns (d)

(SUB-LIST (a b c d) 2 0) return s NIL

  1. Write a single LISP function, called SPLIT-LIST, that takes a list L, and returns a list of two lists L1 and L2, in that order, such that

L is the result of appending L1 and L2; Length of L1 minus length of L2 is 0 or 1.

For example,

(SPLIT-LIST (a b c d)) returns ((a b) (c d))

(SPLIT-LIST (a b c d e)) returns ((a b c) (d e)) NOTE: ((a b) (c d e)) is incorrect; (SPLIT-LIST (a b c d e f)) returns ((a b c) (d e f))

You can call the function SUB-LIST from SPLIT-LIST.

A binary tree is one in which each node has 0 or 2 children. A node that has 0 child is called a leaf node. A node that has 2 children is called an internal node. A binary tree can be represented as follows:

  • A leaf node N is represented by atom N;
  • An internal node N is represented by a list (L R), where L represents the left child of N and R represents the right child of N.
  1. Write a single LISP function, called BTREE-HEIGHT, which takes a binary tree TREE, and returns the height of TREE. Note that the height of a binary tree is defined as the length of the longest path from the root node to the farthest leaf node.

For example,

(BTREE-HEIGHT 1) returns 0

(BTREE-HEIGHT (1 2)) returns 1

(BTREE-HEIGHT (1 (2 3))) returns 2

(BTREE-HEIGHT ((1 2) (3 4))) returns 2

(BTREE-HEIGHT ((1 (2 3)) ((4 5) (6 7)))) returns 3

(BTREE-HEIGHT (((1 2) (3 4)) ((5 6) (7 8)))) returns 3

  1. Write a single LISP function, called LIST2BTREE, that takes a non-empty list of atoms LEAVES, and returns a binary tree such that
  • The tree leaves are the elements of LEAVES;
  • For any internal (non-leaf) node in the tree, the number of leaves in its left branch minus the number of leaves in its right branch is 0 or 1.

For example,

(LIST2BTREE (1)) returns 1

(LIST2BTREE (1 2)) returns (1 2)

(LIST2BTREE (1 2 3)) returns ((1 2) 3)

(LIST2BTREE (1 2 3 4)) returns ((1 2) (3 4))

(LIST2BTREE (1 2 3 4 5 6 7)) returns (((1 2) (3 4)) ((5 6) 7))

(LIST2BTREE (1 2 3 4 5 6 7 8)) returns (((1 2) (3 4)) ((5 6) (7 8)))

You can call the function SPLIT-LIST from LIST2BTREE.

  1. 8. Write a single LISP function, called BTREE2LIST, that takes a binary tree TREE as input, and returns a list of atoms (assume TREE follows the constraints we defined earlier).
  • As the input is a binary tree, each node has at most 2 children;
  • This function is the inverse of LIST2BTREE. That is, (BTREE2LIST (LIST2BTREE X)) = X for all lists of atoms X.

For example,

(BTREE2LIST 1) returns (1)

(BTREE2LIST (1 2)) returns (1 2)

(BTREE2LIST ((1 2) 3)) returns (1 2 3)

(BTREE2LIST ((1 2) (3 4))) returns (1 2 3 4)

(BTREE2LIST (((1 2) (3 4)) ((5 6) 7))) returns (1 2 3 4 5 6 7)

(BTREE2LIST (((1 2) (3 4)) ((5 6) (7 8)))) returns (1 2 3 4 5 6 7 8)

  1. 9. Write a single Boolean LISP function, called IS-SAME, that takes two LISP expressions E1 and E2 whose atoms are all numbers, and checks whether the expressions are identical. In this question, you can only use = to test equality (you cannot use equal). Recall that a LISP expression is either an atom or a list of LISP expressions.

For example,

(IS-SAME ((1 2 3) 7 8) ((1 2 3) 7 8)) returns T

(IS-SAME (1 2 3 7 8) ((1 2 3) 7 8)) returns NIL

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[Solved] CS161 Homework 1-single Boolean LISP function
$25