ϵ">]>
8. CVIČENIE Z PREDMETU ÚVOD DO DEKLARATÍVNEHO PROGRAMOVANIA
http://ii.fmph.uniba.sk/cl/courses/1-AIN-505-udp/0809ls/ex/ex08.cl
Dátum: 6. a 8. 4. 2009
Odporúčaná verzia CL: 5.81.19
WWW stránka predmetu: http://ii.fmph.uniba.sk/cl/courses/1-AIN-505-udp/0809ls/?lang=sk
The predicate holds if is a code of a binary tree created by constructors and (see above). The predicate is a format. You can use it to display values as binary trees in queries, e.g.:
Nd(0,Nd(1,Nd(3,E,E),Nd(4,E,E)),Nd(2,E,Nd(6,E,E))) = x & x = t:Bt
Predikát platí, ak je kódom binárneho stromu vytvoreným pomocoy a (viď vyššie). Predikát je formátom. Môžete ho použiť na zobrazovanie hodnôt v query vo forme binárnych stromov:
Nd(0,Nd(1,Nd(3,E,E),Nd(4,E,E)),Nd(2,E,Nd(6,E,E))) = x & x = t:Bt
Assignment 1. Define a predicate (displayed as ) holding if is in the tree as a label (value) of some node.
Assignment 2. Define a function (displayed as ) that counts the number of nodes () in the tree .
Assignment 3. Define the following functions:
(a) yielding the number of empty subtrees () in the tree ;
(b) yielding the number of inner nodes in the tree , i.e., nodes () having at least one non-empty subtree;
(c) yielding the number of leaves in the tree , i.e., nodes () having both left and right subtrees empty.
Assignment 5.
(a) Define a function listing values of nodes in the tree in in-order. The function is specified by the following properties:
(b) Define a function possessing the property
and consequently
Do not use concatenation () or any auxiliary function in the definition.
Assignment 6.
(a) Define a function listing values of nodes in the tree in pre-order, i.e., satisfying:
(b) Define a function having the property
Do not use concatenation () or any auxiliary function in the definition.
Assignment 7.
(a) Define a function listing values of nodes in the tree in post-order, i.e., satisfying:
(b) Define a function having the property
Do not use concatenation () or any auxiliary function in the definition.
Assignment 8. * Define a function which takes a list and constructs an balanced binary tree containing all members of the list in the breadth-first order. The tree is balanced in the sense that the left child of each substree may be at most one level deeper than the right child. For example:
Hint. Notice how many elements you have to drop from the list to get from a parent (e.g. ) to its children (, ). Define an auxiliary function , and keep the number of dropped elements in . just calls with suitably initialized . You will need the function from ex04.
Assignment 9. * Define a function which takes a list and constructs an perfectly balanced (Braun) binary tree containing all members of the list. The tree is perfectly balanced in the sense that the left child of each substree may contain at most one more element than the right child. The path to the -th element of the list is determined by the dyadic representation of (reading from the least to most significant digit, selects the left subtree, selects the right one). For example:
Hint. Notice that the -th element of the list goes to the root, the -st, -rd, ..., -th elements go to the left subtree, and -nd, -th, ..., -th elements go to the right subtree. Recall that the function from ex06 splits the list precisely as needed here.