Note that there are some explanatory texts on larger screens.

plurals
  1. POPrinting Binary Tree per Layer in a List
    text
    copied!<p>The function should print the markings of the argument tree in layers as a list of layers. The node and leaf markings in each layer are listed from left to right, that is, which is the most left node of the layer, the first element of the list, the right-most node is the last element of the list. The argument of type Ord indicates whether the layers in ascending order from the smallest to the largest layer (TopDown) or in descending order from largest to smallest layer (BottomUp) are to be issued</p> <pre><code>data Tree = Leaf Integer | Node Integer Tree Tree type Layer = [Integer] data Ord = BottomUp | TopDown wLayer :: Tree -&gt; Ord -&gt; [Layer] </code></pre> <p>Example 1: We call the function wLayer with arguments<br> wLayer (Node 1 (Node 2 (Leaf 21) (Leaf 22)) (Node 3 (Leaf 31) (Leaf 32))) TopDown the result : [[1],[2,3],[21,22,31,32]]</p> <p>Example 2: wLayer (Node 1 (Node 2 (Leaf 21) (Leaf 22)) (Node 3 (Leaf 31) (Leaf 32))) BottomUp the result: [[21,22,31,32],[2,3],[1]]</p> <p>how can i implement this one ?</p> <p><strong>Edit</strong></p> <pre><code>data Tree = Leaf Integer | Node Integer Tree Tree type Layer = [Integer] data Ord = BottomUp | TopDown writeLayer :: Tree -&gt; Ord -&gt; [Layer] writeLayer Leaf x = [x] writeLayer (Node x lt rt) BottomUp = (writeLayer rt BottomUp) ++ [x] ++ (writeLayer lt BottomUp) writeLayer (Node x lt rt) TopDown = [x] ++ (writeLayer lt TopDown) ++ (writeLayer rt TopDown) </code></pre> <p>this is my program but it isn't work how can i fix it ?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload