# Prolog code dating service Adult nudist cam chat A node of a binary tree is at level N if the path from the root to the node has length N-1. Write a predicate atlevel/3 to collect all nodes at a given level in a list.% atlevel(T, L, S) :- S is the list of nodes of the binary tree T at level L Using atlevel/3 it is easy to construct a predicate levelorder/2 which creates the level-order sequence of the nodes. binary tree with height H is defined as follows: The levels 1,2,3,..., H-1 contain the maximum number of nodes (i.e 2**(i-1) at the level i, note that we start counting the levels from 1 at the root).You should also indicate the intended data types of the arguments and the allowed flow patterns. Problems marked with two asterisks (**) are of intermediate difficulty. X = [a, b, c, d, e] Hint: Use the predefined predicates is_list/1 and append/3 If a list contains repeated elements they should be replaced with a single copy of the element. As in problem P11, simplify the result list by replacing the singleton terms [1, X] by X. The objective is to sort the elements of In List according to their length. short lists first, longer lists later, or vice versa. - lsort(o], [d, e], [d, e], [m, n], [a, b, c], [f, g, h], [i, j, k, l b) Again, we suppose that a list (In List) contains elements that are lists themselves.

||

A node of a binary tree is at level N if the path from the root to the node has length N-1. Write a predicate atlevel/3 to collect all nodes at a given level in a list.

% atlevel(T, L, S) :- S is the list of nodes of the binary tree T at level L Using atlevel/3 it is easy to construct a predicate levelorder/2 which creates the level-order sequence of the nodes. binary tree with height H is defined as follows: The levels 1,2,3,..., H-1 contain the maximum number of nodes (i.e 2**(i-1) at the level i, note that we start counting the levels from 1 at the root).

You should also indicate the intended data types of the arguments and the allowed flow patterns. Problems marked with two asterisks (**) are of intermediate difficulty. X = [a, b, c, d, e] Hint: Use the predefined predicates is_list/1 and append/3 If a list contains repeated elements they should be replaced with a single copy of the element. As in problem P11, simplify the result list by replacing the singleton terms [1, X] by X. The objective is to sort the elements of In List according to their length. short lists first, longer lists later, or vice versa. - lsort(a,b,c],[d,e],[f,g,h],[d,e],[i,j,k,l],[m,n],[o, L). L = o], [d, e], [d, e], [m, n], [a, b, c], [f, g, h], [i, j, k, l b) Again, we suppose that a list (In List) contains elements that are lists themselves.

If you are a skilled Prolog programmer it shouldn't take you more than 30-90 minutes to solve them. Transform a list, possibly holding lists as elements into a `flat' list by replacing each list with its elements (recursively). But this time the objective is to sort the elements of In List according to their length frequency; i.e.

]]

Use the predicate add/3, developed in chapter 4 of the course, to write a predicate to construct a binary search tree from a list of integer numbers. % hbal_tree_nodes(N, T) :- T is a height-balanced binary tree with N nodes.

In level H, which may contain less than the maximum possible number of nodes, all the nodes are "left-adjusted".

This means that in a levelorder tree traversal all internal nodes come first, the leaves come second, and empty successors (the nil's which are not really nodes! Particularly, complete binary trees are used as data structures (or addressing schemes) for heaps.

We can assign an address number to each node in a complete binary tree by enumerating the nodes in levelorder, starting at the root with number 1.

In doing so, we realize that for every node X with address A the following property holds: The address of X's left and right successors are 2*A and 2*A 1, respectively, supposed the successors do exist.   This fact can be used to elegantly construct a complete binary tree structure. Given a binary tree as the usual Prolog term t(X, L, R) (or nil).